[英]Link to static boost lib with cmake and vs2010 without automatic linking
我有一个链接到boost_program_options的应用程序,其CMakeLists.txt看起来像
FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES( ${SUBPROJECT_NAME} ${Boost_LIBRARIES} )
在我的代码中使用#define BOOST_ALL_NO_LIB
之前包括<boost/program_options.hpp>
来禁用vs2010中boost的自动链接,因为我想通过cmake指定它使它与linux兼容。
在Linux中,这段代码编译得很好(使用cmake,make和gcc)。 但在使用VS2010的Windows中,我得到了一个
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)
请注意,链接器找到了lib - 如果找不到它,我会得到更多未解决的外部错误。
我将问题跟踪到以下内容: http : //lists.boost.org/boost-users/2009/11/54015.php ,它很好地描述了正在发生的事情(这两个是全局变量)。 现在提出的解决方案是启用动态链接并链接到DLL。 但这不是我想要做的,我想链接静态boost lib(我实际上是想做的,在VS中的App属性中Linker->输入它列出D:\\boost\\boost_1_47\\lib\\boost_program_options-vc100-mt-gd-1_47.lib
。
我也尝试过添加
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
到我的CMakeLists.txt但它没有改变任何东西。
任何想法如何解决这个问题?
更新:当与boost_program_options-vc100-mt-sgd-1_47.lib链接时,我得到了一大堆关于已在boost-lib中定义的CRT符号的新链接器错误。 按照panickal的建议更改VS Runtime选项后,这些错误也消失了,并且正在运行。
您必须链接到静态库。 尝试链接到boost_program_options-vc100-mt-sgd-1_47.lib
而不是boost_program_options-vc100-mt-gd-1_47.lib
。
s
表示库的静态版本。 您可以查看Boost Library Naming以获取有关命名约定的更多详细信息。
更新:要修复多定义链接器错误,请将Configuration Properties / C/C++ / Code Generation / Runtime Library
的Visual Studio运行时库选项从Multi-threaded Debug DLL (/MDd)
更改为Multi-threaded Debug (/MTd)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.