I have created 2 object files hello.o and hi.o, each containing a function with the same name, respectively. Also, greetings.h was created containing the function prototypes of the two object files.
When I link these two object files with main.o in any order, the process succeeds and I get a.out. But if you make a static library from the hello.o and hi.o files, then the linking succeeds only in the order g++ main.o glib.a.
As I understand it, the order should be from the file that declares to the file that contains the definition, but when linking object files alone, it doesn't work. What is the difference between linking a library and object files, and only object files?
Example:
Source code:
greetings.hpp
#ifndef GREETINGS_HPP
#define GREETINGS_HPP
void hello();
void hi();
#endif
hello.cpp
#include <iostream>
void hello() {
std::cout << "Hello!" << std::endl;
}
hi.cpp
#include <iostream>
void hi() {
std::cout << "Hi!" << std::endl;
}
main.cpp
#include "greetings.hpp"
int main () {
hello();
hi();
return 0;
}
Actions:
ar -src glib.a hello.o hi.o
g++ glib.a main.o
Error:
main.o: In function `main':
main.cpp:(.text+0x5): undefined reference to `hello()'
main.cpp:(.text+0xa): undefined reference to `hi()'
collect2: error: ld returned 1 exit status
Related
The following is a simple example for separate compilation:
// mod.cpp
#include <cstdio>
class MyModule {
public:
void print_msg();
};
void MyModule::print_msg() {
printf("hello from module\n");
}
// main.cpp
class MyModule {
public:
void print_msg();
};
int main() {
MyModule a;
a.print_msg();
}
We can compile and run it with
g++ main.cpp -c -o main.o
g++ mod.cpp -c -o mod.o
g++ main.o mod.o -o main
./main
The above works fine, but if I move the definition of MyModule::print_msg inside the class:
// mod.cpp
#include <cstdio>
class MyModule {
public:
void print_msg() { printf("hello from module\n"); }
};
I get an 'undefined reference' error for compiling main:
g++ main.cpp -c -o main.o # OK
g++ mod.cpp -c -o mod.o # OK
g++ main.o mod.o -o main # undefined reference error
/usr/bin/ld: main.o: in function `main':
main.cpp:(.text+0x23): undefined reference to `MyModule::print_msg()'
collect2: error: ld returned 1 exit status
I know that the former is the standard way and the class definition should go to a header file, but I wonder why the second method doesn't work.
Functions defined inside the class are implicitly inline. C++ requires:
The definition of an inline function [or variable (since C++17)] must be reachable in the translation unit where it is accessed.
Since you only defined it in mod.cpp, no definition is reachable in main.cpp, and compilation fails.
Typically, you'd put the definition of the class, and the definition of all functions defined within it, in a header file to be included by all users of the class. The functions defined outside the class then go in a .cpp file. That way a single consistent definition of all the inline functions is available to all users of the class, and you're not repeating the definition of the class in each .cpp file manually.
This question already has answers here:
Undefined reference to pthread_create in Linux
(16 answers)
Closed 3 years ago.
I'm using a class of threads with a header and cpp file.
When I put both of them and empty test file it writes:
g++ -g -pedantic -ansi -Wall -Werror -std=c++03 -I../include -c -o test.o test.cpp
g++ -g test.o thread.o -o test
thread.o: In function `Thread::~Thread()':
/home/tomer/work/mt/hash2/cpp/thread.cpp:15: undefined reference to `pthread_detach'
thread.o: In function `Thread::start()':
/home/tomer/work/mt/hash2/cpp/thread.cpp:40: undefined reference to `pthread_create'
thread.o: In function `Thread::join()':
/home/tomer/work/mt/hash2/cpp/thread.cpp:49: undefined reference to `pthread_join'
thread.o: In function `Thread::cancel()':
/home/tomer/work/mt/hash2/cpp/thread.cpp:58: undefined reference to `pthread_cancel'
thread.o: In function `Thread::detach()':
/home/tomer/work/mt/hash2/cpp/thread.cpp:66: undefined reference to `pthread_detach'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'test' failed
make: *** [test] Error 1
I just tried to comppile Thread.h and Thread.cpp
//Thread.h looks like this:
#ifndef THREAD_H
#define THREAD_H
#include <cstddef>
#include <pthread.h>
#include <string>
class Thread
{
public:
Thread(size_t a_userID = 0);
virtual ~Thread();
bool start();
void join();
void cancel();
void detach();
private:
static void* threadMainFunction(void *);
virtual void run() = 0;
bool isAlive(std::string a_msg);
private:
bool m_joinable;
protected:
pthread_t m_threadID;
size_t m_userID;
};
#endif
//Thread.cpp looks like this:
#include <exception>
#include "Thread.h"
#include <iostream>
Thread::Thread(size_t a_userID)
: m_joinable(true)
, m_threadID(0)
, m_userID(a_userID)
{
}
Thread::~Thread()
{
if(m_joinable)
{
pthread_detach(m_threadID);
}
}
void* Thread::threadMainFunction(void *a_thread)
{
Thread* thread = reinterpret_cast<Thread*>(a_thread);
try
{
thread->run();
}
catch(const std::exception& e)
{
std::cout<<"what exepction\n";
std::cerr << e.what() << '\n';
}
catch(...)
{
throw;
}
return 0;
}
bool Thread::start()
{
int r = pthread_create(&m_threadID, 0, threadMainFunction, this);
return r == 0;
}
void Thread::join()
{
if(isAlive("Thread::join on thread not started"))
{
void *status;
pthread_join(m_threadID, &status);
m_joinable = false;
}
}
void Thread::cancel()
{
if(isAlive("Thread::cancel on thread not started"))
{
pthread_cancel(m_threadID);
}
}
void Thread::detach()
{
if(isAlive("Thread::detach on thread not started"))
{
pthread_detach(m_threadID);
}
}
bool Thread::isAlive(std::string a_msg)
{
if(m_threadID == 0)
{
throw(std::runtime_error(a_msg));
return false;
}
return true;
}
The issue you are facing here is not a build issue, but a link issue. When building thread.o, the compiler knows pthread_create exists and is defined somewhere because declared in the pthread.h header.
If you used nm to look at the symbols used in thread.o, you would see something similar to this:
U _pthread_create
U _pthread_detach
...
This tells you that thread.o references multiple Undefined symbols including pthread_create. In other words, the machine code for pthread_create is unknown at this point. This is perfectly fine until you need to link your object files into an executable file, which is the role of the linker.
At this stage, you must tell the linker where to find these undefined symbols, perhaps from another object file or a static/shared library. For pthread, the symbols are defined in libpthread, which you can likely find in a system directory as libpthread.a. You can tell g++ to link this library by adding -lpthread (note that lib from libpthread is omitted when using -l):
g++ -g test.o thread.o -o test -lpthread
In general, if you use symbols referenced in a static library lib${LIBNAME}.a available in ${LIBDIR} directory, you can tell the linker to use it with:
g++ -g *.o -L$LIBDIR -l${LIBNAME}
I can't seem to get the errors to go away. The errors are below. I have looked on Google Search and still I can't figure it out. It is not like I am new to C++, but I have not fooled around with it in a while.
The weird thing is it worked with g++ on Windows...
Errors using:
g++ main.cpp
Output:
/tmp/ccJL2ZHE.o: In function main': \ main.cpp:(.text+0x11): undefined reference to Help::Help()'
main.cpp:(.text+0x1d): undefined reference to Help::sayName()' \ main.cpp:(.text+0x2e): undefined reference to Help::~Help()'
main.cpp:(.text+0x46): undefined reference to `Help::~Help()'
collect2: ld returned 1 exit status
File main.cpp
#include <iostream>
#include "Help.h"
using namespace std;
int main () {
Help h;
h.sayName();
// ***
// ***
// ***
return 0;
}
File Help.h
#ifndef HELP_H
#define HELP_H
class Help {
public:
Help();
~Help();
void sayName();
protected:
private:
};
#endif // HELP_H
File Help.cpp
#include <iostream>
#include "Help.h"
using namespace std;
Help::Help() { // Constructor
}
Help::~Help() { // Destructor
}
void Help::sayName() {
cout << " ***************" << endl;
cout << " ************************************" << endl;
cout << " ************" << endl;
cout << " *********************" << endl;
}
Use
g++ main.cpp Help.cpp
You have to tell the compiler all the files that you want it to compile, not just the first one.
You should add help.o to your g++ line:
g++ -c help.cpp -o help.o
g++ help.o main.cpp
By splitting it to two lines you can save compilation time (in case of larger projects), because you can compile help.cpp only when it was changed. make and Makefile used well will save you a lot of headache:
#Makefile
all: main
main: help main.cpp
g++ -o main help.o main.cpp
help: help.cpp
g++ -c -o help.o help.cpp
I had the same problem with my Linux Lubuntu distribution and it was creating the problem for my constructor and destructor. It was not recognizing them.
Actually, this goes off if you just compile all of the three files together. So, once you saved all your files, just do this:
g++ main.cpp Help.h Help.cpp
./a.out
./a.out is the executable file for the Linux. Sorry, but I don't know about the Windows. And your program would run smoothly.
I can't seem to get the errors to go away. The errors are below. I have looked on Google Search and still I can't figure it out. It is not like I am new to C++, but I have not fooled around with it in a while.
The weird thing is it worked with g++ on Windows...
Errors using:
g++ main.cpp
Output:
/tmp/ccJL2ZHE.o: In function main': \ main.cpp:(.text+0x11): undefined reference to Help::Help()'
main.cpp:(.text+0x1d): undefined reference to Help::sayName()' \ main.cpp:(.text+0x2e): undefined reference to Help::~Help()'
main.cpp:(.text+0x46): undefined reference to `Help::~Help()'
collect2: ld returned 1 exit status
File main.cpp
#include <iostream>
#include "Help.h"
using namespace std;
int main () {
Help h;
h.sayName();
// ***
// ***
// ***
return 0;
}
File Help.h
#ifndef HELP_H
#define HELP_H
class Help {
public:
Help();
~Help();
void sayName();
protected:
private:
};
#endif // HELP_H
File Help.cpp
#include <iostream>
#include "Help.h"
using namespace std;
Help::Help() { // Constructor
}
Help::~Help() { // Destructor
}
void Help::sayName() {
cout << " ***************" << endl;
cout << " ************************************" << endl;
cout << " ************" << endl;
cout << " *********************" << endl;
}
Use
g++ main.cpp Help.cpp
You have to tell the compiler all the files that you want it to compile, not just the first one.
You should add help.o to your g++ line:
g++ -c help.cpp -o help.o
g++ help.o main.cpp
By splitting it to two lines you can save compilation time (in case of larger projects), because you can compile help.cpp only when it was changed. make and Makefile used well will save you a lot of headache:
#Makefile
all: main
main: help main.cpp
g++ -o main help.o main.cpp
help: help.cpp
g++ -c -o help.o help.cpp
I had the same problem with my Linux Lubuntu distribution and it was creating the problem for my constructor and destructor. It was not recognizing them.
Actually, this goes off if you just compile all of the three files together. So, once you saved all your files, just do this:
g++ main.cpp Help.h Help.cpp
./a.out
./a.out is the executable file for the Linux. Sorry, but I don't know about the Windows. And your program would run smoothly.
I cannot get this simple piece of code to compile without including the TestClass.cpp file explicitly in my main.cpp file. What am I doing wrong? Thanks in advance!
Here is the code:
TestClass.h
#ifndef TESTCLASS_H_
#define TESTCLASS_H_
class TestClass
{
public:
static int foo();
};
#endif
TestClass.cpp
#include "TestClass.h"
int TestClass::foo() { return 42; }
main.cpp
#include <iostream>
#include "TestClass.h"
using namespace std;
int main()
{
cout << TestClass::foo() << endl;
return 0;
}
Here is the error:
g++ main.cpp -o main.app
/tmp/ccCjOhpy.o: In function `main':
main.cpp:(.text+0x18e): undefined reference to `TestClass::foo()'
collect2: ld returned 1 exit status
Include TestClass.cpp into the commandline, so the linker can find the function definition:
g++ main.cpp TestClass.cpp -o main.app
Alternatively, compile each to their own object file, then tell the compiler to link them together (it will forward them to the linker)
g++ -c main.cpp -o main.o
g++ -c TestClass.cpp -o TestClass.o
g++ main.o TestClass.o -o main.app
You're not compiling and linking against TestClass.cpp (where the implementation of foo() is). The compiler is thus complaining that your trying to use an undefined function.