News:

Accounts with zero posts and zero activity during the last months will be deleted periodically to fight SPAM!

Main Menu

Linking the pthread library

Started by NanoGoner, December 23, 2012, 08:55:08 PM

Previous topic - Next topic

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

#1
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

don't enter pthread under libraries, but in additional linker options as -pthread

renebarto

One more post, as I think I've figured out what is going wrong: The command buffer is too small. Only part of the command line is sent to sh. I've added an echo to the linker command, and it leaves out part of the command line. I am currently trying out cb 10.05, on Ubuntu 12.04 with gcc.

-------------- Build: Debug in Core.Test ---------------

echo g++ -L../../../bin/Debug  -o ../../../bin/Debug/Core.Test obj/Debug/external/gtest/gtest-all.o obj/Debug/src/CoreActiveObjectTest.o obj/Debug/src/CoreArrayTest.o obj/Debug/src/CoreAutoEventTest.o obj/Debug/src/CoreByteArrayTest.o obj/Debug/src/CoreCyclicBufferTest.o obj/Debug/src/CoreManualEventTest.o obj/Debug/src/CoreMutexTest.o obj/Debug/src/CoreStreamTest.o obj/Debug/src/CoreTextReaderTest.o obj/Debug/src/CoreTextWriterTest.o obj/Debug/src/CoreThreadTest.o obj/Debug/src/CoreTimeStampTest.o obj/Debug/src/CoreTimerTest.o obj/Debug/src/CoreWorkerThreadTest.o obj/Debug/src/Main.o   -Wl,-rpath,'$ORIGIN'  -lCore -lpthread -lrt
g++ -L../../../bin/Debug -o ../../../bin/Debug/Core.Test obj/Debug/external/gtest/gtest-all.o obj/Debug/src/CoreActiveObjectTest.o obj/Debug/src/CoreArrayTest.o obj/Debug/src/CoreAutoEventTest.o obj/Debug/src/CoreByteArrayTest.o obj/Debug/src/CoreCyclicBufferTest.o obj/Debug/src/CoreManualEventTest.o obj/Debug/src/CoreMutexTest.o obj/Debug/src/CoreStreamTest.o obj/Debug/src/CoreTextReaderTest.o obj/Debug/src/CoreTextWriterTest.o obj/Debug/src/CoreThreadTest.o obj/Debug/src/CoreTimeStampTest.o obj/Debug/src/CoreTimerTest.o obj/Debug/src/CoreWorkerThreadTest.o obj/Debug/src/Main.o -Wl,-rpath,
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings


BlueHazzard

look at my previous post.

(c::b 10.05 is really old, don't expect many support for this old version, it has changed a lot since them)
please post logs/code in code tags...

greetings

renebarto

Thx I was not aware of that. I will download the current version for Ubuntu.

mloutris

#9
Quote from: BlueHazzard on January 16, 2014, 11:45:36 PM
don't enter pthread under libraries, but in additional linker options as -pthread

I ran into this as well. To be more precise, in CB 16.01, from the top menu bar, select 'Settings->Compiler'
Then select the 'Linker Settings' tab, and near the bottom of the window, select the 'Add' button,
enter 'pthread' (without the single quotes!), as Code::Blocks will prepend the '-l' automatically.
Click on the 'OK' button and rebuild.

oBFusCATed

mloutris: This solution is wrong on many levels.

1. You need to pass -pthread not -lpthread.
2. You need to pass it both to the compiler and linker.
3. Putting such options in the global settings is wrong, you need to put these in the project, so the code could be moved from one machine to the next...

The original problem in the topic is related to the use of rpath=@ORIGIN by the looks of it...
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]