User forums > Using Code::Blocks

Linking the pthread library

(1/3) > >>

NanoGoner:
I am running ubuntu Linux and am developing a program in C++.  In the program I am trying to start a thread with the following code:
-----------------------------------------------------------------------------

#include <pthread.h>
void *updater (unsigned char matrix[8][8]);

int main (int argc, char **argv)
{
   pthread_t mat_thread
   int iret, i, x;
   iret = pthread_create(&mat_thread, NULL, updater, matrix);
....

------------------------------------------------------

When I build the program files I get 1 error which is:


undefined reference to 'pthread_create'

I have installed g++ and gdb and at first I presumed this meant the pthread library file (libpthread.so) was not available.
I am told on another C++ forum that I need the following linking command to link the library into the build:

g++ *.cpp -pthread

This is a command line operation.
Can some one tell me how I accomplish this within CodeBlocks.  I'm brand new to it.

Jenna:
You can add any commandline option to to your projects build options ("{Compiler|Linker} settings -> Other [linker ]options").

The Code::Blocks manual and our wiki might also be helpful to see what is possible.

renebarto:
Dear Jens, I see the same problem, and the documentation is not helping me. When I use the cbp2make tool to create a makefile, the compilation and linkage work perfectly. When building from within cb, the linker fails.  ??? The command it spits out is exacly the same, but for some reason the compiler does not try to link the libraries, resulting in undefined references.
What could be wrong?

renebarto:
Just for information this is part of the build log:

g++ -L../../../bin/Debug  -o ../../../bin/Debug/Core.Test obj/Debug/external/gtest/gtest-all.o .... obj/Debug/src/Main.o   -Wl,-rpath,'$ORIGIN'  -lCore -lpthread -lrt
obj/Debug/external/gtest/gtest-all.o: In function `~ThreadLocal':
/home/developer/workspace/source/product/components/Core.Test/external/gtest/gtest.h:2589: undefined reference to `pthread_getspecific'

The only difference I can see is that cb internally jams in spaces into the command line.

BlueHazzard:
http://wiki.codeblocks.org/index.php?title=FAQ-Compiling_%28general%29#Q:_How_do_I_report_a_compilation_problem_on_the_forums.3F

Navigation

[0] Message Index

[#] Next page

Go to full version