3

I'm using OSX Mountain Lion. I just downloaded, unpacked, and built boost 1.52.0 using the instructions supplied from the boost website: http://www.boost.org/doc/libs/1_52_0/more/getting_started/unix-variants.html. I left the default installation prefix at /usr/local, meaning that the libraries are installed in /usr/local/lib and the header files are in /usr/local/include. I have verified that the libraries and headers are present there and recently modified.

I'm attempting to compile the boost asynchronous I/O example found here: http://www.boost.org/doc/libs/1_52_0/doc/html/boost_asio/tutorial/tuttimer5/src.html (source).

Here is my compilation command:

g++ -Wall -c -g -I/usr/local/include src/test1.cpp -o src/test1.o

where src/test1.cpp is the example source file. Here is my linking command (and error):

g++ -Wall -L/usr/local/lib -lboost_thread -lboost_system  src/test1.o -o bin/test1
Undefined symbols for architecture x86_64:
  "boost::thread::~thread()", referenced from:
      _main in test1.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

I have tried using both Apple's clang++ 4.0 and g++ 4.6.0. I get the same undefined symbol error from both programs.

Other people seem to have had trouble compiling this code sample as well. I am aware of this question: C++ / Boost: Undefined Symbols in example? and this one: Linker error when compiling boost.asio example. However, each of these problems seems to have been fixed by adding the appropriate switches (-lboost_thread and -lboost_system) to the command line. I already have those. I have also tried adding -lpthread without luck.

Thank you for taking time to look at my question! Any help is appreciated. :)

Community
  • 1
  • 1

2 Answers2

0

Put the libraries you link with last on the command line.

The GNU linker uses kind of reverse lookup of dependencies, so if file A depends on library B, B should come after A on the command line.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Thanks for the input. I moved the libraries to the end of the linker command, and re-ordered them (all six different orders of the three arguments -lpthread, -lboost_thread, and -lboost_system), but to no avail. I received the same error every time. Perhaps this is relevant: when I add the flag `-static`, I get a different error: `ld: library not found for -lcrt0.o`. I never mentioned that file on the command-line. I can confirm that both the .a and .dylib versions of each library are installed under `/usr/local/lib`. –  Dec 12 '12 at 07:47
0

Just ran into this same problem, and I found that reverting to Boost 1.49 fixes the problem. Download links for Boost 1.49 are here:

http://www.boost.org/users/history/version_1_49_0.html

Before installing 1.49, I removed 1.52 by deleting /usr/local/include/boost and /usr/local/lib/*boost*. Not sure what changed between 1.49 and 1.52 to cause this problem, or whether Boost 1.50 or 1.51 will work.

msridhar
  • 2,846
  • 4
  • 22
  • 23