RE: [Boost-Users] Problems linking with Boost 1.29.0 thread lib, Linux/g++ 3.2.2 -r2
subsample
Failing to link a program with libboost_thread, even though I supply the -lboost_thread directive in the makefile. I'm kind of new to makefiles, having only used Visual C++ in Windows untill recently. I 've tried changing things around a bit, doesn't help. A minimal test program I wrote links fine with boost_thread though. My makefile looks
like this: CC=g++ CFLAGS=-g LDFLAGS=-L/home/sub/Libs/alsa/lib -lasound -lboost_thread -lpthread
alsatest: alsatest.o $(CC) $(LDFLAGS) -o alsatest SmallObj.o alsatest.o <snip>
ld doesn't work this way. -l is just a shortcut for finding a library file. The libraries specified with -l are part of the file list, same as the object files and any library files specified by their full file name. The nasty thing is that backwards references are only resolved in object files, not in library files, so library files must be listed after the object files (and any other library files) that reference them. You need to take the libraries from LDFLAGS (perhaps into another variable) and put them at the end of the command line. Also you should be using CXX and CXXFLAGS for compiling C++ code, not CC and CFLAGS. Since you're building a pthreads program, you *must* define the macro _REENTRANT to get a threadsafe C library. As a shortcut, include -pthread in CXXFLAGS (this causes linking with -lpthread, too).
--- In Boost-Users@yahoogroups.com, "Ben Hutchings"
ld doesn't work this way. -l is just a shortcut for finding a library file. The libraries specified with -l are part of the file list, same as the object files and any library files specified by their full file name. The nasty thing is that backwards references are only resolved in object files, not in library files, so library files must be listed after the object files (and any other library files) that reference them.
You need to take the libraries from LDFLAGS (perhaps into another variable) and put them at the end of the command line.
Also you should be using CXX and CXXFLAGS for compiling C++ code, not CC and CFLAGS.
Since you're building a pthreads program, you *must* define the macro _REENTRANT to get a threadsafe C library. As a shortcut, include -pthread in CXXFLAGS (this causes linking with -lpthread, too).
Ok, thanks for clearing this out.
participants (2)
-
Ben Hutchings
-
subsample