2

I'm reading a tutorial about threads in C++ and tested the following code:

#include <iostream>
#include <pthread.h>
#include <cstdlib>

using namespace std;

#define NUM_THREADS     5

void *PrintHello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   cout << "Hello World! Thread ID, " << tid << endl;
   pthread_exit(NULL);
}

int main ()
{
   pthread_t threads[NUM_THREADS];
   int rc;
   int i;
   for( i=0; i < NUM_THREADS; i++ ){
      cout << "main() : creating thread, " << i << endl;
      rc = pthread_create(&threads[i], NULL, 
                      PrintHello, &threads[i]);
      if (rc){
         cout << "Error:unable to create thread," << rc << endl;
         exit(-1);
      }
   }
   pthread_exit(NULL);
}

I've tried to compile this code using both gcc and g++, but I always get compilation errors.

Using gcc -pthread thread_test.c:

/tmp/ccmpQLyp.o: In function PrintHello(void*)': thread_test.cpp:(.text+0x1a): undefined reference tostd::cout' thread_test.cpp:(.text+0x1f): undefined reference to std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' thread_test.cpp:(.text+0x2e): undefined reference tostd::ostream::operator<<(long)' thread_test.cpp:(.text+0x33): undefined reference to std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)' thread_test.cpp:(.text+0x3b): undefined reference tostd::ostream::operator<<(std::ostream& (*)(std::ostream&))' /tmp/ccmpQLyp.o: In function main': thread_test.cpp:(.text+0x63): undefined reference tostd::cout' thread_test.cpp:(.text+0x68): undefined reference to std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' thread_test.cpp:(.text+0x75): undefined reference tostd::ostream::operator<<(int)' thread_test.cpp:(.text+0x7a): undefined reference to std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)' thread_test.cpp:(.text+0x82): undefined reference tostd::ostream::operator<<(std::ostream& (*)(std::ostream&))' thread_test.cpp:(.text+0xcc): undefined reference to std::cout' thread_test.cpp:(.text+0xd1): undefined reference tostd::basic_ostream >& std::operator<< >(std::basic_ostream >&, char const*)' thread_test.cpp:(.text+0xde): undefined reference to std::ostream::operator<<(int)' thread_test.cpp:(.text+0xe3): undefined reference tostd::basic_ostream >& std::endl >(std::basic_ostream >&)' thread_test.cpp:(.text+0xeb): undefined reference to std::ostream::operator<<(std::ostream& (*)(std::ostream&))' /tmp/ccmpQLyp.o: In function__static_initialization_and_destruction_0(int, int)': thread_test.cpp:(.text+0x141): undefined reference to std::ios_base::Init::Init()' thread_test.cpp:(.text+0x150): undefined reference tostd::ios_base::Init::~Init()' /tmp/ccmpQLyp.o:(.eh_frame+0x47): undefined reference to `__gxx_personality_v0' collect2: error: ld returned 1 exit status

Can you help? Do I have to do something so this code runs on Linux and Windows?

4
  • Try -lpthread instead
    – SChepurin
    Commented Apr 23, 2013 at 11:57
  • Using gcc with -lpthread I still get errors: /tmp/ccq3Kk7G.o: In function main': thread_test.cpp:(.text+0xb9): undefined reference to pthread_create' collect2: error: ld returned 1 exit status juliano@juliano-linux:~/Documents/cpp$ gcc -lpthread thread_test.cpp /tmp/ccVu4YcA.o: In function PrintHello(void*)': thread_test.cpp:(.text+0x1a): undefined reference to std::cout' thread_test.cpp:(.text+0x1f): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' Commented Apr 23, 2013 at 12:02
  • gcc -o thread_test.c -Wall -Werror -lpthread
    – SChepurin
    Commented Apr 23, 2013 at 12:19
  • @SChepurin : /usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o: In function _start': (.text+0x20): undefined reference to main' collect2: error: ld returned 1 exit status Commented Apr 23, 2013 at 12:29

3 Answers 3

4

Use g++ instead of gcc, or link -lstdc++ manually.

1
  • Using g++ I'm getting the following error: /tmp/ccq3Kk7G.o: In function main': thread_test.cpp:(.text+0xb9): undefined reference to pthread_create' collect2: error: ld returned 1 exit status Commented Apr 23, 2013 at 11:46
3

For anyone reading this in 2020:

In GCC, when linking against pthread, don't use "-lpthread", but just the "-pthread" CLI options.

1
  • 1
    Thanks for that. Got me past this error on Ubuntu 20.04, g++ 8 Commented Nov 30, 2021 at 16:05
0

It's important to know how the C++ compiler links the library files. Linking is the second step of building a program, the first one being Compilation which requires the header files.

As for linking, the linker program, GNU ld, does the job. When you run gcc or g++ compiler command without -c, the ld program is run and the libc(the C standard library) is automatically searched and linked. As, implementation of pthread is NOT included in libc, we need to explicitly tell the linker(gcc/g++ pass the flags to the linker) to search pthread library. How to do that? Here it's from gnu online docs

-llibrary

-l library

Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)

The -l option is passed directly to the linker by GCC. Refer to your linker documentation for exact details. The general description below applies to the GNU linker.

The linker searches a standard list of directories for the library. The directories searched include several standard system directories plus any that you specify with -L.

Static libraries are archives of object files, and have file names like liblibrary.a. Some targets also support shared libraries, which typically have names like liblibrary.so. If both static and shared libraries are found, the linker gives preference to linking with the shared library unless the -static option is used.

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.

Like: g++ thread.cc -lpthread -o thread

Just prefix the library name with -l

Not the answer you're looking for? Browse other questions tagged or ask your own question.