0

I have a C++ library which I want to compile it using Visual Studio 2017 (CMake/Ninja) with /MT compiler option in Release mode. Here, I asked a similar question some time before. The answer to that question helped but causes the compiler to complain (report warning) about overriding /MD with /MT. Then I used this solution, but setting the CMAKE_CXX_FLAGS_RELEASE has no effect on the compiler command line arguments in Release mode. I mean the following code works well in Debug mode:

set(CompilerFlags
    CMAKE_CXX_FLAGS
    CMAKE_CXX_FLAGS_DEBUG
    CMAKE_CXX_FLAGS_RELEASE)
foreach(CompilerFlag ${CompilerFlags})
    message("before replace: " ${${CompilerFlag}})
    string(REPLACE "/MD" "/MT" ${CompilerFlag} "${${CompilerFlag}}")
    message("after replace: " ${${CompilerFlag}})
endforeach()

The result of running CMake would be:

before replace: /DWIN32 /D_WINDOWS /W3 /GR /EHsc
after replace: /DWIN32 /D_WINDOWS /W3 /GR /EHsc
before replace: /MDd /Zi /Ob0 /Od /RTC1
after replace: /MTd /Zi /Ob0 /Od /RTC1
before replace: /MD /O2 /Ob2 /DNDEBUG
after replace: /MT /O2 /Ob2 /DNDEBUG

The result of build would be:

cl.exe  ... /MTd ...

In Release mode the result of running CMake would be the same; however, the result of build would be:

cl.exe  ... /MD ...

If you know what's the correct way of doing that, I'll be really appreciate to hear that.

wxShayan
  • 288
  • 1
  • 4
  • 12
  • 1
    Are you sure you had used CMAKE_BUILD_TYPE=Release? It didn't seem to work for me at first, but I had used RelWithDebInfo and for that you need to add CMAKE_CXX_FLAGS_RELWITHDEBINFO to the list of patched variables. Fwiw I'm using the Ninja Generator with VS2017. – Dominik Schmidt Oct 29 '18 at 15:19
  • Thank you Dominik for your comment. You are right. At the time I was writing this post, I didn't know that CMake has more build types like RelWithDebInfo. Actually my (default) build type was RelWithDebInfo in CMakeSettings.json and I was not aware of it; therefore, I was manipulating a wrong variable (CMAKE_CXX_FLAGS_RELEASE). – wxShayan Nov 01 '18 at 08:28
  • @Dominik Schmidt, you are a lifesaver – V.V.T Feb 06 '23 at 07:46

1 Answers1

0

it works after long struggling

TARGET_COMPILE_OPTIONS(${library_name} PRIVATE "/MT$<$<CONFIG:Release>:>")
cpuwolf
  • 27
  • 3