Search code examples
visual-c++cmakeclion

Produce mapfile with CMAKE_CXX_FLAGS and MSVC14


I'am trying to generate a Mapfile with CMake (Clion)

cmake_minimum_required(VERSION 3.13)
project(cmake_testapp)
set(CMAKE_CXX_STANDARD 14)
MESSAGE("TEST_HELLO")
set(MSVC_COVERAGE_COMPILE_FLAGS "/Fm${PROJECT_SOURCE_DIR}/file.map")

set(CMAKE_CXX_FLAGS "${MSVC_COVERAGE_COMPILE_FLAGS} ${CMAKE_CXX_FLAGS}" )
MESSAGE(${CMAKE_CXX_FLAGS})
MESSAGE(${CMAKE_CXX_COMPILER})
add_executable(cmake_testapp loaderstack.cpp)

Output:

"C:\Program Files\JetBrains\CLion 2020.1.1\bin\cmake\win\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - NMake Makefiles" C:\Users\remi\Desktop\OK
TEST_HELLO
/FmC:/Users/remi/Desktop/OK/file.map /DWIN32 /D_WINDOWS /W3 /GR /EHsc
C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.26.28801/bin/Hostx86/x86/cl.exe
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/remi/Desktop/OK/cmake-build-debug

[Finished]

But no file.map is produced even if the CMAKE_CXX_FLAGS contains the flag.


Solution

  • The map file is generated by the linker, and is typically disabled by default. To enable generation of a map file, the /MAP linker option must be provided. When I tested with /MAP, the compiler flag /Fm specifying the map file path name was ignored, and the map file was generated with a default name next to the binary.

    However, you can also specify the map file name by appending it to the /MAP linker option. In CMake, you can specify link options for a specific target using target_link_options:

    cmake_minimum_required(VERSION 3.13)
    project(cmake_testapp)
    set(CMAKE_CXX_STANDARD 14)
    MESSAGE("TEST_HELLO")
    
    MESSAGE(${CMAKE_CXX_COMPILER})
    add_executable(cmake_testapp loaderstack.cpp)
    
    # Set the coverage flags for the cmake_testapp executable only.
    target_link_options(cmake_testapp PRIVATE "/MAP:${PROJECT_SOURCE_DIR}/file.map")