PDA

View Full Version : Boost C++ libraries


Rij
November 20th, 2007, 01:59 PM
Hello,

I am trying to use Boost Thread libraries. I followed the usual download and build instructions.

So now I have all my Thread header files in:
.../boost_1_34_1/boost/thread/

I have the following libraries in .../boost_1_34_1/stage/lib:
libboost_thread-gcc34-mt-1_34_1.a libboost_thread-gcc34-mt-d.a
libboost_thread-gcc34-mt-d-1_34_1.a libboost_thread-gcc34-mt.a

Now I am trying to compile the standard hello world which looks like this:
#include <boost/thread/thread.hpp>
#include <iostream>

void hello() {
std::cout << "Hello world, I'm a thread!" << std::endl;
}

int main(int argc, char* argv[]) {
boost::thread thrd(&hello);
thrd.join();
return 0;
}

I compile with gcc 3.4.1 on SunOS. I specified the header files with -I option and the libraries with -L option.

But ld is unable to resolve the thread symbols. So I get the error:

Undefined first referenced
symbol in file
boost::thread::join() /var/tmp//ccrK5p0W.o
boost::thread::~thread() /var/tmp//ccrK5p0W.o
boost::thread::thread(boost::function0<void, std::allocator<boost::function_base> > const&)/var/tmp//ccrK5p0W.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

Can someone please help?

Thanks, Rij

aks44
November 20th, 2007, 02:17 PM
What is the *exact* command line you use? IIRC g++ uses -I (upper-case i) for include paths and -l (lower-case L) for library paths.

Better post your command line inside [ code ] tags to avoid any confusion...

iharrold
November 20th, 2007, 02:37 PM
[I'm assuming Ubuntu]first type:
sudo apt-get install libboost*

To ensure you have all components of the library. Then, this should be the command line g++ compile procedure. Use [-l] boost_thread for the library.

g++ boostthreadtest.cpp -o boostthreadtest -lboost_thread
$ ./boostthreadtest

aks44
November 20th, 2007, 02:41 PM
I'm assuming Ubuntu

I compile with gcc 3.4.1 on SunOS.

Morality: never assume anything, and always read the posts thoroughly... ;)

Rij
November 20th, 2007, 03:04 PM
I used -I and tried with -L for the above mentioned error

g++ -I/home/research/code/boost_1_34_1 -L /home/research/code/boost_1_34_1/stage/lib/libboost_thread-gcc34-mt.a main.cc


But if I use -l, then I get the error:
ld: fatal: library -l/home/research/code/boost_1_34_1/stage/lib/libboost_thread-gcc34-mt.a: not found

I DO have the library in that location.

aks44
November 20th, 2007, 03:18 PM
g++ -I/home/research/code/boost_1_34_1 -L /home/research/code/boost_1_34_1/stage/lib/libboost_thread-gcc34-mt.a main.cc

-L should be a path (directory), use -l (lower-case L) to specify the library name (without "lib" prefix and without ".a"/".so" postfix).


If I'm not mistaken, this should work:

g++ -I/home/research/code/boost_1_34_1 -L/home/research/code/boost_1_34_1/stage/lib/ -lboost_thread-gcc34-mt main.cc

iharrold
November 20th, 2007, 03:24 PM
Are you sure you have the library? Here's the install help from boost.

http://www.boost.org/more/getting_started/unix-variants.html

Rij
November 20th, 2007, 05:27 PM
If I'm not mistaken, this should work:

g++ -I/home/research/code/boost_1_34_1 -L/home/research/code/boost_1_34_1/stage/lib/ -lboost_thread-gcc34-mt main.cc

I tried that. Now it finds the libraries, but still throws the same error: undefined symbol ... blah ... blah ... blah

Incidentally, there seems to be a mismatch between my g++ and ld.
/usr/ccs/bin/ld
/opt/gcc-3.4.1/bin/g++

Can that be an issue?

Rij
November 20th, 2007, 05:29 PM
Are you sure you have the library? Here's the install help from boost.

http://www.boost.org/more/getting_started/unix-variants.html

I do have the library. If you see my first message, I listed the libraries that I have. Is there any missing?

aks44
November 20th, 2007, 05:58 PM
I tried that. Now it finds the libraries, but still throws the same error: undefined symbol ... blah ... blah ... blah

Did you try linking (-l switch, lower-case L) with other libraries than boost_thread-gcc34-mt, and/or linking with several of them at the same time?

What comes to my mind is:
- both boost_thread-gcc34-mt-d-1_34_1 and boost_thread-gcc34-mt-d if you're building a debug binary, or
- both boost_thread-gcc34-mt-1_34_1 and boost_thread-gcc34-mt if you're building a release binary


Not sure this helps though... linking to boost_thread-* should *theoretically* have gotten rid of boost::thread* missing references, even though it may have given new missing references.
As it seems this is not the case, so maybe we're just heading the wrong way?


Incidentally, there seems to be a mismatch between my g++ and ld.
/usr/ccs/bin/ld
/opt/gcc-3.4.1/bin/g++

Can that be an issue?
Now I must admit, I have not the slightest clue about that. :oops:

dwhitney67
November 20th, 2007, 09:43 PM
I've looked into the program you wrote, and it works on my system, however that system is Fedora 8; not the SunOS (Solaris) that you are using.

I compiled using this statement:

g++ SimpleThread.cpp -L/usr/lib -lboost_thread-mt -o simple

Of course, on Fedora, the boost libraries are installed in /usr/lib, thus I do not have to specifically provide the -L option above, but nevertheless I did so for the OP's benefit.

By the way, I have this version of Boost: 1.34.1-5.fc8.i386. It would appear that the OP has a very similar version. And I am running gcc-4.1.2.