Compiling&Linking Error
Hello everyone,
I wrote a program that uses posix threads.i include header files to my project and i compile with gcc -c piThreads.c that's ok i have a piThreads.o file but when i want to link this file with gcc -o piThreads.o piThreads.c i have an error like this : /tmp/ccgNQ6AM.o(.text+0x14d): In function `main': : undefined reference to `pthread_create' /tmp/ccgNQ6AM.o(.text+0x15d): In function `main': : undefined reference to `pthread_join' collect2: ld returned 1 exit status i included pthread.h file. i tried to compile and link my project in redhat, it succesfully compile and link also i have no problem at execution time... when i look at pkgtool for libraries, i can see these libs for gcc glib-1.2.10.i386-2 glib2-2.6.2-i486-1 glib2-2.3.4-i486-1 also i cannot add math.h library to my project too... i really need help for this. Thanx to all.. |
Did you try :
gcc -o piThreads.o piThreads.c -lpthread And if you want math.h library: gcc -o piThreads.o piThreads.c -lpthread -lm |
yep, that's worked.
is there other parameters like (-lpthread, -lm ..) and how can i learn them? and why am i need to add these? i mean, isnt it enough to add .h ? Thank you keefaz.. |
You need to add these to tell to the linker what shared library to link, eg
-lm : link math library to access math function when you tell #include math.h, you just include the math functions definition, not the math function code You learn them by experience, there is not specific method to learn, just work but for pthread, the lib is /usr/lib/libpthread.so and to link it you remove the 'lib' prefix so -lpthread |
Thanks.
|
All times are GMT -5. The time now is 02:20 PM. |