<div dir="ltr">> Just a heads up, CMake 3.15 is introducing policy 91 which removes the<br>> runtime library from the default set of flags, and instead has targets<br>> establish what runtime they want. <br><div><br></div><div>Thanks for this.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Jun 18, 2019 at 7:08 PM Robert Maynard <<a href="mailto:robert.maynard@kitware.com">robert.maynard@kitware.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Just a heads up, CMake 3.15 is introducing policy 91 which removes the<br>
runtime library from the default set of flags, and instead has targets<br>
establish what runtime they want.<br>
<br>
For more information see:<br>
<a href="https://cmake.org/cmake/help/v3.15/prop_tgt/MSVC_RUNTIME_LIBRARY.html" rel="noreferrer" target="_blank">https://cmake.org/cmake/help/v3.15/prop_tgt/MSVC_RUNTIME_LIBRARY.html</a><br>
<br>
On Tue, Jun 18, 2019 at 10:06 AM Eric Dönges <<a href="mailto:doenges@mvtec.com" target="_blank">doenges@mvtec.com</a>> wrote:<br>
><br>
> On 18.06.19 12:53, David Aldrich wrote:<br>
> > I have a simple CMake project that builds an executable using Visual<br>
> > Studio 2017:<br>
><br>
><br>
> ><br>
> > ################ Files ################<br>
> > # -- Add files to project. -- #<br>
> > #######################################<br>
> ><br>
> > file(GLOB SRC_FILES<br>
> > ${CPP_DIR_1}/*.cpp<br>
> > ${CPP_DIR_2}/*.cpp<br>
> > ${CPP_DIR_3}/*.cpp<br>
> > ${CPP_DIR_4}/*.cpp<br>
> > ${HEADER_DIR_1}/*.h<br>
> > )<br>
> ><br>
> > # Add executable to build.<br>
> > add_executable(${PROJECT_NAME}<br>
> > ${SRC_FILES}<br>
> > )<br>
> ><br>
> > if(MSVC)<br>
> > target_link_libraries(${PROJECT_NAME} ws2_32.lib )<br>
> > endif(MSVC)<br>
> ><br>
> > #=====================================<br>
> ><br>
> > The Release build succeeds but the Debug build fails with linker errors<br>
> > such as:<br>
> ><br>
> > [build] CPlaneTest.obj : error LNK2001: unresolved external symbol<br>
> > __imp___CrtDbgReport<br>
> ><br>
> > I think this is because the linker is not using the debug version of CRT<br>
> > (C Run-time Library).<br>
> ><br>
> > Should CMake select the debug build of CRT automatically or do I need to<br>
> > specify it manually? If so, should I do so using CMAKE_EXE_LINKER_FLAGS?<br>
> ><br>
><br>
> CMake will select the correct CRT automatically if you let it (unless<br>
> you want the static CRT, in which case you have to override CMake's<br>
> default settings). You are setting your CMAKE_CXX_FLAGS_DEBUG incorrectly:<br>
><br>
> > if(MSVC)<br>
> > #set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /D _DEBUG /W3<br>
> > /MD /Od /Zi /EHsc")<br>
> > set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /W3 /GL /Od<br>
> > /Oi /Gy /Zi /EHsc")<br>
> > set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_RELEASE} /D _DEBUG /W3<br>
> > /GL /Od /Oi /Gy /Zi /EHsc")<br>
> > endif(MSVC)<br>
><br>
> In case of the setting you've commented out, you are explicitly telling<br>
> CMake to use /MD. CMAKE_CXX_FLAGS_DEBUG should already contain /MDd, but<br>
> since you append the /MD, that is what the compiler will actually use.<br>
><br>
> For the setting that is not commented out, you set CMAKE_CXX_FLAGS_DEBUG<br>
> to the contents of CMAKE_CXX_FLAGS_RELEASE - which is certainly not what<br>
> you want (and also specifies /MD).<br>
><br>
> I would suggest looking at what flags CMake sets by default (look at the<br>
> Windows-MSVC.cmake file in CMake's 'Modules/Platform' directory) and<br>
> only setting those flags that CMake doesn't already. For version 3.14,<br>
> CMake should be setting the following flags for CMAKE_CXX_FLAGS_DEBUG by<br>
> default (assuming you are using MVSC >= 1310, no Clang toolset):<br>
><br>
> /MDd /Zi /Ob0 /Od /GR /EHSC<br>
><br>
> So in your case, it would probably be enough to<br>
><br>
> string(APPEND CMAKE_CXX_FLAGS_DEBUG " /D_DEBUG /W3")<br>
><br>
> As a final recommendation, use string(APPEND <var> ...) (or list(APPEND<br>
> <list> ...), if the variable is interpreted as a list) when appending<br>
> values to existing variables. This makes your intent clearer.<br>
><br>
><br>
> - when appending compiler flags, use the "string(APPEND ...)" command<br>
> to make is clearer what you are doing).<br>
> --<br>
><br>
> Powered by <a href="http://www.kitware.com" rel="noreferrer" target="_blank">www.kitware.com</a><br>
><br>
> Please keep messages on-topic and check the CMake FAQ at: <a href="http://www.cmake.org/Wiki/CMake_FAQ" rel="noreferrer" target="_blank">http://www.cmake.org/Wiki/CMake_FAQ</a><br>
><br>
> Kitware offers various services to support the CMake community. For more information on each offering, please visit:<br>
><br>
> CMake Support: <a href="http://cmake.org/cmake/help/support.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/support.html</a><br>
> CMake Consulting: <a href="http://cmake.org/cmake/help/consulting.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/consulting.html</a><br>
> CMake Training Courses: <a href="http://cmake.org/cmake/help/training.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/training.html</a><br>
><br>
> Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
><br>
> Follow this link to subscribe/unsubscribe:<br>
> <a href="https://cmake.org/mailman/listinfo/cmake" rel="noreferrer" target="_blank">https://cmake.org/mailman/listinfo/cmake</a><br>
-- <br>
<br>
Powered by <a href="http://www.kitware.com" rel="noreferrer" target="_blank">www.kitware.com</a><br>
<br>
Please keep messages on-topic and check the CMake FAQ at: <a href="http://www.cmake.org/Wiki/CMake_FAQ" rel="noreferrer" target="_blank">http://www.cmake.org/Wiki/CMake_FAQ</a><br>
<br>
Kitware offers various services to support the CMake community. For more information on each offering, please visit:<br>
<br>
CMake Support: <a href="http://cmake.org/cmake/help/support.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/support.html</a><br>
CMake Consulting: <a href="http://cmake.org/cmake/help/consulting.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/consulting.html</a><br>
CMake Training Courses: <a href="http://cmake.org/cmake/help/training.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/training.html</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="https://cmake.org/mailman/listinfo/cmake" rel="noreferrer" target="_blank">https://cmake.org/mailman/listinfo/cmake</a><br>
</blockquote></div>