Strange link builder errors with a boost?

I’ve been working on this for a while and cannot understand the situation - in part because I don’t quite understand what is happening (that’s why I came here).

I make a kind of greeting to the world:

#include <boost/thread/thread.hpp> #include <cstdio> void helloworld() { std::printf("HELLO FROM A BOOST THREAD!"); } int main(int argc, char **argv) { boost::thread t(&helloworld); t.join(); } 

This is on Windows. I saved the Boost directory in C: \ Boost. I launched bootstrap and bjam, and now I have a stage / lib folder containing all the .lib files. Lib files related to boost / thread library:

 libboost_thread-vc100-mt.lib libboost_thread-vc100-mt-1_46_1.lib libboost_thread-vc100-mt-gd.lib libboost_thread-vc100-mt-gd-1_46_1.lib 

Now I will compile:

 g++ -c main.cpp -I/Boost 

this line is working fine, I get main.o. Then:

 g++ -o test.exe main.o -L/Boost/stage/lib -llibboost_thread-vc100-mt 

And what is where the problem is. First of all, if I hadn't typed the -l argument the way I did, MinGW couldn't even find the file. Meaning if I tried:

 -lboost_thread-vc100-mt 

instead of typing it above (and as I thought it should be done), ld will exit without such a file. In any case, now this is the result that I get from this line:

 main.o:main.cpp:(.text+0x47): undefined reference to `_imp___ZN5boost6thread4joinEv' main.o:main.cpp:(.text+0x55): undefined reference to `_imp___ZN5boost6threadD1Ev' main.o:main.cpp:(.text+0x70): undefined reference to `_imp___ZN5boost6threadD1Ev' main.o:main.cpp:(.text$_ZN5boost6threadC1IPFvvEEET_NS_10disable_ifINS_14is_convertibleIRS4_NS_6detail13thread_move_tIS4_EEEEPNS0_5dummyEE4typeE[boost::thread::thread<void (*)()>(void (*)(), boost::disable_if<boost::is_convertible<void (*&)(), boost::detail::thread_move_t<void (*)()> >, boost::thread::dummy*>::type)]+0x23): undefined reference to `_imp___ZN5boost6thread12start_threadEv' collect2: ld returned 1 exit status 

Now, somewhere out there, I can say that these are apparently the functions that I should get from boost / thread, and apparently it finds the lib file, so why doesn't it link correctly?

Thanks so much for any help!

EDIT:

I updated forcing using the bjam stage option

 bjam toolset=gcc stage 

Now, after the build is complete, I still have the stage / lib folder with the .a files, as you would expect. These are the libraries related to boost / thread:

 libboost_thread-mgw45-mt-1_46_1.a libboost_thread-mgw45-mt-d-1_46_1.a 

However, connecting as follows:

 g++ -o test.exe main.o -L/Boost/stage/lib -lboost_thread-mgw45-mt-1_46_1 

displays the same errors. Also tried:

 g++ -o test.exe main.o -L/Boost/stage/lib -lboost_thread-mgw45-mt-1_46_1 -static 

I'm still at a loss.

+4
source share
2 answers

Solved a problem. Boost headers are configured for dynamic linking, but dynamic libraries (dlls) are not created unless you specify:

 --build-type=complete 

when calling bjam. After that, copy the appropriate dll to the application directory, but use

 -L/BOOST_DIR/stage/lib -lname 

when binding.

+5
source

This set of library files:

 libboost_thread-vc100-mt.lib libboost_thread-vc100-mt-1_46_1.lib libboost_thread-vc100-mt-gd.lib libboost_thread-vc100-mt-gd-1_46_1.lib 

for the Visual Studio 2010 compiler. They will not work with GCC. If you want to use gcc / MinGW, you will need to download / build a set of boost libraries for this compiler. Alternatively, you can install VS 2010 and use this compiler (the free version of VC ++ 2010 Express should work fine if there is a problem).

You can get the MinGW distribution with Boost already in the package from http://nuwen.net/mingw.html (only for a 32-bit target).


To answer the error question using the MinGW libraries:

_imp_ prefixes on characters are a sign that g++ looking for a link to the dll / shared library. The .lib file you have is for static libraries (which is also what I get when doing a simple build of bjam libraries). If you look in boost/thread/detail/config.hpp , you will see that for Win32 build it creates a DLL for the library by default, unless MSVC or Intel compiler is used.

I'm not even sure exactly how to create DLLs - I will have to look for it. In the meantime, you can use the following command to create your own example to link it to a static library. The BOOST_THREAD_USE_LIB macro creates a .cpp file so that it will reference a static library:

 g++ -I/Boost -DBOOST_THREAD_USE_LIB -c main.cpp 
+5
source

All Articles