How can I generate Visual Studio projects for a specific configuration?

折月煮酒 提交于 2019-12-22 14:51:52

问题


Using CMake's ExternalProject_Add, I automate builds of my dependencies. However, I build configurations don't match in the end even though I pass CMAKE_BUILD_TYPE and BUILD_SHARED_LIBS as described in the Tutorial.

# SFML
include(ExternalProject)
set(SFML_PREFIX ${CMAKE_SOURCE_DIR}/SFML)

# Download, configure, build and install.
ExternalProject_Add(SFML
    # DEPENDS
    PREFIX         ${SFML_PREFIX}
    TMP_DIR        ${SFML_PREFIX}/temp
    STAMP_DIR      ${SFML_PREFIX}/stamp
    #--Download step--------------
    GIT_REPOSITORY https://github.com/LaurentGomila/SFML.git
    GIT_TAG        e2c378e9d1
    #--Update/Patch step----------
    UPDATE_COMMAND ""
    #--Configure step-------------
    SOURCE_DIR     ${SFML_PREFIX}/source
    CMAKE_ARGS     -DCMAKE_INSTALL_PREFIX:PATH=${SFML_PREFIX}/install
                   -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
                   -DBUILD_SHARED_LIBS=${BUILD_SHARED_LIBS}
    #--Build step-----------------
    BINARY_DIR     ${SFML_PREFIX}/build
    #--Install step---------------
    INSTALL_DIR    ${SFML_PREFIX}/install
)

# Set root so that find module knows where to look.
set(SFML_ROOT ${SFML_PREFIX}/install)

How can I generate a Visual Studio project that builds release versions, instead of falling back to debug mode with no command line arguments provided? Is there even a way to generate a project that builds both release and debug versions with one msbuild Project.sln?


回答1:


For multi-configuration generators (e.g., Visual Studio), the actual build configuration (Debug or Release) is specified upon building the project. Setting the CMAKE_BUILD_TYPE at configuration time does not have an effect.

When a Visual Studio generator is used, the ExternalProject_Add command sets up the build process of the external project so that the project uses the build configuration currently selected in the IDE. In CMake terms that means the following code is executed:

cmake --build <BINARY_DIR> --config ${CMAKE_CFG_INTDIR}

For Visual Studio 10 ${CMAKE_CFG_INTDIR} is replaced with $(Configuration) at build time.

To always do a Release build, you have to replace the ExternalProject_Add default build step with a custom one:

ExternalProject_Add(SFML
    ...
    #--Build step-----------------
    BINARY_DIR ${SFML_PREFIX}/build
    BUILD_COMMAND
        ${CMAKE_COMMAND} --build <BINARY_DIR> --config Release
    ...
    )

To build both the Release and Debug versions, add another cmake invocation:

ExternalProject_Add(SFML
    ...
    #--Build step-----------------
    BINARY_DIR ${SFML_PREFIX}/build
    BUILD_COMMAND
        ${CMAKE_COMMAND} --build <BINARY_DIR> --config Release
        COMMAND ${CMAKE_COMMAND} --build <BINARY_DIR> --config Debug
    ...
    )


来源:https://stackoverflow.com/questions/26440988/how-can-i-generate-visual-studio-projects-for-a-specific-configuration

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!