Such is the code:

#include <iostream> #include <unistd.h> #include <pthread.h> #include <sys/types.h> using namespace std; void* helloWorld(void *args) { cout << "Hello from thread!\n"; } int main() { pthread_t thread; pthread_create(&thread, NULL, helloWorld, NULL); return 0; } 

It produces errors:

undefined reference to `pthread_create '

collect2: error: ld returned 1 exit status

I do not understand the reason for their appearance.

  • one
    This is not “the code gives errors”, and the compiler is not told to link with the libpthread library. - PinkTux
  • If you have g ++, just add -pthread to the compilation keys - g++ -pthread t.cpp - avp

1 answer 1

The corresponding library is not connected.

But if you work with C ++ - why not use standard tools? std::thread , std::async ?

  • And in the crosses already everything that is in pthread_... implemented? - avp
  • @avp Firstly, hardly at this level - when the question of connecting the library arises - some special functionality is needed, secondly, the standard - it is the standard, and thirdly, no one bothers to get a handle to the stream and do him everything your heart desires. - Harry
  • @avp, and what is not in C ++, what is in pthread? - ixSci