3
votes

I am on Windows(MSVC 2012) and with Boost v1.54, I compiled Boost.Log (my small project depends on it) with following parameters:

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.

Then my small project uses CMake to manage, and I add option to link boost statically:

set(Boost_USE_STATIC_LIBS ON)

But make process of generated nmake makefile failed, with link error 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.

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?

BTW: my project is dynamically linked to CRT with /MD option.


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. In fact cmake will pass the latter to compiler, so my program is actually linked statically as the error output shows.

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.

Oh, a silly question.

1
The error message suggests that http_proxy_server.cpp is being compiled to use MT_StaticRelease of the library. Could that be the reason? How is that translation unit being compiled?greatwolf
@greatwolf Compile? Maybe Link you mean? My project is dynamically linked to CRT by /MD option, but statically linked to boost library.Kelvin Hu
Have you tried adding set( Boost_USE_STATIC_RUNTIME OFF ) ?Daarx
@Daarx Yeah, finally I figured out the reason, it is the improper setting of CMAKE_CXX_FLAGS, please see the edit section of this question.Kelvin Hu

1 Answers

1
votes

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. 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. In fact cmake will pass the latter to compiler, so my program is actually linked statically as the error output shows.

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.