简体   繁体   English

使用cmake和vs2010链接到静态boost lib而不使用自动链接

[英]Link to static boost lib with cmake and vs2010 without automatic linking

I got an app that links to boost_program_options, whose CMakeLists.txt looks like 我有一个链接到boost_program_options的应用程序,其CMakeLists.txt看起来像

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. 在我的代码中使用#define BOOST_ALL_NO_LIB之前包括<boost/program_options.hpp>来禁用vs2010中boost的自动链接,因为我想通过cmake指定它使它与linux兼容。

In Linux, this code compiles just fine (with cmake, make and gcc). 在Linux中,这段代码编译得很好(使用cmake,make和gcc)。 But in Windows with VS2010, I get a 但在使用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)

Note that the linker finds the lib - if it didn't find it, I would get many more unresolved external errors. 请注意,链接器找到了lib - 如果找不到它,我会得到更多未解决的外部错误。

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). 我将问题跟踪到以下内容: http//lists.boost.org/boost-users/2009/11/54015.php ,它很好地描述了正在发生的事情(这两个是全局变量)。 Now the proposed solution there is to enable dynamic linking and link to the DLLs. 现在提出的解决方案是启用动态链接并链接到DLL。 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 . 但这不是我想要做的,我想链接静态boost lib(我实际上是想做的,在VS中的App属性中Linker->输入它列出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. 到我的CMakeLists.txt但它没有改变任何东西。

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. 更新:当与boost_program_options-vc100-mt-sgd-1_47.lib链接时,我得到了一大堆关于已在boost-lib中定义的CRT符号的新链接器错误。 After changing the VS Runtime options as suggested by panickal, those errors are also gone and it's working. 按照panickal的建议更改VS Runtime选项后,这些错误也消失了,并且正在运行。

You have to link to the static library. 您必须链接到静态库。 Try linking to boost_program_options-vc100-mt-sgd-1_47.lib instead of boost_program_options-vc100-mt-gd-1_47.lib . 尝试链接到boost_program_options-vc100-mt-sgd-1_47.lib而不是boost_program_options-vc100-mt-gd-1_47.lib

The s indicates the static version of the library. s表示库的静态版本。 You can check Boost Library Naming for more details about the naming conventions. 您可以查看Boost Library Naming以获取有关命名约定的更多详细信息。

Update: To fix the multiple definition linker errors, change the Visual Studio Runtime Library option in Configuration Properties / C/C++ / Code Generation / Runtime Library from Multi-threaded Debug DLL (/MDd) to Multi-threaded Debug (/MTd) . 更新:要修复多定义链接器错误,请将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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM