[CMake] How to specify debug version of CRT library for Visual Studio generator?
David Aldrich
david.aldrich.ntml at gmail.com
Tue Jun 18 06:53:17 EDT 2019
I have a simple CMake project that builds an executable using Visual Studio
2017:
#==============================================
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
################### Variables. ####################
# Change if you want modify path or other values. #
###################################################
set(PROJECT_NAME CPlaneTest)
# Output Variables
set(OUTPUT_DEBUG Debug/bin)
set(OUTPUT_RELEASE Release/bin)
# Folders files
set(CPP_DIR_1 ./)
set(CPP_DIR_2 ./)
set(CPP_DIR_3 ./)
set(CPP_DIR_4 ./)
set(HEADER_DIR_1 )
############## CMake Project ################
# The main options of project #
#############################################
project(${PROJECT_NAME} CXX)
# Define Release by default.
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
message(STATUS "Build type not specified: Use Release by default.")
endif(NOT CMAKE_BUILD_TYPE)
# Definition of Macros
add_definitions(
-D_CONSOLE
-DUNICODE
-D_UNICODE
)
############## Artefacts Output #################
# Defines outputs , depending Debug or Release. #
#################################################
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${OUTPUT_DEBUG}")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${OUTPUT_DEBUG}")
set(CMAKE_EXECUTABLE_OUTPUT_DIRECTORY
"${CMAKE_BINARY_DIR}/${OUTPUT_DEBUG}")
else()
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${OUTPUT_REL}")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${OUTPUT_REL}")
set(CMAKE_EXECUTABLE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${OUTPUT_REL}")
endif()
################# Flags ################
# Defines Flags for Windows and Linux. #
########################################
if(MSVC)
#set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /D _DEBUG /W3 /MD
/Od /Zi /EHsc")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /W3 /GL /Od /Oi
/Gy /Zi /EHsc")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_RELEASE} /D _DEBUG /W3 /GL
/Od /Oi /Gy /Zi /EHsc")
endif(MSVC)
if(NOT MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
endif()
endif(NOT MSVC)
################ Files ################
# -- Add files to project. -- #
#######################################
file(GLOB SRC_FILES
${CPP_DIR_1}/*.cpp
${CPP_DIR_2}/*.cpp
${CPP_DIR_3}/*.cpp
${CPP_DIR_4}/*.cpp
${HEADER_DIR_1}/*.h
)
# Add executable to build.
add_executable(${PROJECT_NAME}
${SRC_FILES}
)
if(MSVC)
target_link_libraries(${PROJECT_NAME} ws2_32.lib )
endif(MSVC)
#=====================================
The Release build succeeds but the Debug build fails with linker errors
such as:
[build] CPlaneTest.obj : error LNK2001: unresolved external symbol
__imp___CrtDbgReport
I think this is because the linker is not using the debug version of CRT (C
Run-time Library).
Should CMake select the debug build of CRT automatically or do I need to
specify it manually? If so, should I do so using CMAKE_EXE_LINKER_FLAGS?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20190618/dd88a7f9/attachment-0001.html>
More information about the CMake
mailing list