0

I am working in an applpication in c++ in Linux, basically what I want to do is this, I have two .cpp files one of them is main.cpp which use threads. My problem is that I tried to make an executable like this: g++ -c main.cpp -> I get main.o g++ -c second.cpp -> I get second.o g++ -o executable main.o second.o-> I get : 2ficheros.cpp:(.text+0xa4): undefined reference to `pthread_create'

SO I guess that my mistake is because I have to associate the pthread library in same way but I do not have idea how to do it. I was looking on internet and I find that I have to do something like this : g++ -o executable main.o second.o pthread.o but I do not know if it is correct and I cannot see the pthread.o just the pthread.h.

Sorry if I am speaking without knowledges, if someone can help me please.

user3536692
  • 137
  • 3
  • 10

1 Answers1

0

You need to link pthreads support:

c++ main.o second.o -o executable -pthread

Yes, without -l. For other libraries (the math library for example) use this:

c++ main.o second.o -o executable -pthread -lm

And if you have another 3rd party library in some other directory:

c++ main.o second.o -o executable -pthread -L/opt/coolmolib -lcoolmolib

Note I am not using "g++" directly but a symbolic link, c++. On some systems this might be clang (to install clang on debian apt install clang).

elcuco
  • 8,948
  • 9
  • 47
  • 69