简体   繁体   English

动态链接错误与静态链接的boost库

[英]Dynamic linking error with statically linked boost library

I am on Windows(MSVC 2012) and with Boost v1.54, I compiled Boost.Log (my small project depends on it) with following parameters: 我在Windows(MSVC 2012)上,使用Boost v1.54,我使用以下参数编译了Boost.Log (我的小项目取决于它):

b2.exe link=static variant=release runtime-link=shared --with-log stage

So the log module is compiled and dynamically linked to C Runtime Library. 因此,日志模块被编译并动态链接到C运行时库。

Then my small project uses CMake to manage, and I add option to link boost statically: 然后我的小项目使用CMake进行管理,我添加选项以静态链接boost:

set(Boost_USE_STATIC_LIBS ON)

But make process of generated nmake makefile failed, with link error 2038: 但make生成的nmake makefile进程失败,链接错误2038:

libboost_log-vc110-mt-1_54.lib(attribute_set.obj):-1: error: LNK2038:
  mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't
  match value 'MT_StaticRelease' in http_proxy_server.cpp.obj

But if I compile boost with option runtime-link=static , means linking CRT statically, then I can successfully make my project. 但是,如果我使用选项runtime-link=static编译boost,意味着静态链接CRT,那么我可以成功地创建我的项目。

My question is: Could someone explain why my project failed to link with the CRT dynamically linked version of boost, but succeeded with the CRT statically linked version of boost, what's the difference? 我的问题是:有人可以解释为什么我的项目无法与CRT动态链接的boost版本链接,但成功使用CRT静态链接版本的boost,有什么区别?

BTW: my project is dynamically linked to CRT with /MD option. BTW:我的项目动态链接到带有/MD选项的CRT。


Edit: 编辑:

Finally I figured out the reason, I mixed up CMAKE_CXX_FLAGS_RELEASE with CMAKE_CXX_FLAGS , the former has a /MD option, but the latter does not. 最后我想出了原因,我将CMAKE_CXX_FLAGS_RELEASECMAKE_CXX_FLAGS ,前者有/MD选项,但后者没有。 In fact cmake will pass the latter to compiler, so my program is actually linked statically as the error output shows. 事实上,cmake会将后者传递给编译器,因此我的程序实际上是在错误输出显示时静态链接的。

Solution: run cmake with -DCMAKE_BUILD_TYPE=Release when generating makefile, this will add options of CMAKE_CXX_FLAGS_RELEASE into CMAKE_CXX_FLAGS automatically, then the program will be linked to CRT dynamically. 解决方案:在生成makefile时运行带-DCMAKE_BUILD_TYPE=Release cmake,这会自动将CMAKE_CXX_FLAGS_RELEASE选项添加到CMAKE_CXX_FLAGS ,然后程序将动态链接到CRT。

Oh, a silly question. 哦,这是一个愚蠢的问题。

Sorry, this question is asked by myself, and finally I figured out the reason, and I pasted the solution in the Edit section of the question. 对不起,这个问题是我自己问的,最后我弄明白了原因,并在问题的编辑部分粘贴了解决方案。 But @TobiMcNamobi suggests to give this question an answer even it is asked by myself. 但是@TobiMcNamobi建议给这个问题一个答案,即使是我自己也要问。 So I paste the answer here: 所以我在这里粘贴答案:

I mixed up CMAKE_CXX_FLAGS_RELEASE with CMAKE_CXX_FLAGS , the former has a /MD option, but the latter does not. 我将CMAKE_CXX_FLAGS_RELEASECMAKE_CXX_FLAGS ,前者有/MD选项,但后者没有。 In fact cmake will pass the latter to compiler, so my program is actually linked statically as the error output shows. 事实上,cmake会将后者传递给编译器,因此我的程序实际上是在错误输出显示时静态链接的。

Solution: run cmake with -DCMAKE_BUILD_TYPE=Release when generating makefile, this will add options of CMAKE_CXX_FLAGS_RELEASE into CMAKE_CXX_FLAGS automatically, then the program will be linked to CRT dynamically. 解决方案:在生成makefile时运行带-DCMAKE_BUILD_TYPE=Release cmake,这会自动将CMAKE_CXX_FLAGS_RELEASE选项添加到CMAKE_CXX_FLAGS ,然后程序将动态链接到CRT。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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