I got an app that links to boost_program_options, whose CMakeLists.txt looks like
FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES( ${SUBPROJECT_NAME} ${Boost_LIBRARIES} )
I use #define BOOST_ALL_NO_LIB in my code before including <boost/program_options.hpp> to disable the automatic linking of boost in vs2010, because I want to specify that by cmake to make it compatible with linux.
In Linux, this code compiles just fine (with cmake, make and gcc).
But in Windows with VS2010, I get a
2>App.obj : error LNK2001: unresolved external symbol "public: static unsigned int const boost::program_options::options_description::m_default_line_length" (?m_default_line_length@options_description@program_options@boost@@2IB)
2>App.obj : error LNK2001: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > boost::program_options::arg" (?arg@program_options@boost@@3V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A)
Note that the linker finds the lib – if it didn’t find it, I would get many more unresolved external errors.
I tracked the problem down to the following:
http://lists.boost.org/boost-users/2009/11/54015.php which very nicely describes what’s going on (those two are global variables). Now the proposed solution there is to enable dynamic linking and link to the DLLs. But that is not what I’d like to do, I would like to link against the static boost lib (which I’m actually trying to do, in the App properties in VS under Linker->Input it lists D:\boost\boost_1_47\lib\boost_program_options-vc100-mt-gd-1_47.lib.
I also tried adding
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
to my CMakeLists.txt but it doesn’t change anything.
Any ideas how to solve this problem?
Update:
When linking with boost_program_options-vc100-mt-sgd-1_47.lib, I get a whole bunch of new linker errors about CRT-symbols being already defined in the boost-lib.
After changing the VS Runtime options as suggested by panickal, those errors are also gone and it’s working.
You have to link to the static library. Try linking to
boost_program_options-vc100-mt-sgd-1_47.libinstead ofboost_program_options-vc100-mt-gd-1_47.lib.The
sindicates the static version of the library. You can check Boost Library Naming for more details about the naming conventions.Update:
To fix the multiple definition linker errors, change the Visual Studio Runtime Library option in
Configuration Properties / C/C++ / Code Generation / Runtime LibraryfromMulti-threaded Debug DLL (/MDd)toMulti-threaded Debug (/MTd).