CMake中的项目构建配置 [英] Project build configuration in CMake
问题描述
我的问题与 CMake:根据CMake生成的项目中的配置,更改Visual Studio和Xcode可执行文件的名称。在那篇文章中,输出文件名将根据项目配置(Debug,Release等)而改变。我想走得更远。当我知道项目的配置时,我想告诉可执行程序根据项目配置链接不同的库名称。我想知道CMake中是否有一个变量可以告诉项目配置。如果存在这样的变量,我的任务将变得更容易:
My question is very similar to CMake : Changing name of Visual Studio and Xcode exectuables depending on configuration in a project generated by CMake. In that post the output file name will change according to the project configuration (Debug, Release and so on). I want to go further. When I know the configuration of the project, I want to tell the executable program to link different library names depending on project configurations. I was wondering whether there is a variable in CMake that can tell the project configuration. If there exists such a variable, my task will become easier:
if (Project_Configure_Name STREQUAL "Debug")
#do some thing
elseif (Project_Configure_Name STREQUAL "Release")
#do some thing
endif()
推荐答案
根据 http://cmake.org/cmake/help/v2.8.8/cmake.html#command:target_link_libraries ,您可以根据配置指定库,例如:
According to http://cmake.org/cmake/help/v2.8.8/cmake.html#command:target_link_libraries, you can specify libraries according to the configurations, for example:
target_link_libraries(mytarget
debug mydebuglibrary
optimized myreleaselibrary
)
请注意,优化
模式意味着所有非调试的配置。
Be careful that the optimized
mode means every configuration that is not debug.
以下是一个更复杂但更可控制的解决方案:
Following is a more complicated but more controllable solution:
假设您要链接到导入的库(未在你的c制作项目),您可以使用以下方式添加它:
Assuming you are linking to an imported library (not compiled in your cmake project), you can add it using:
add_library(foo STATIC IMPORTED)
set_property(TARGET foo PROPERTY IMPORTED_LOCATION_RELEASE c:/path/to/foo.lib)
set_property(TARGET foo PROPERTY IMPORTED_LOCATION_DEBUG c:/path/to/foo_d.lib)
add_executable(myexe src1.c src2.c)
target_link_libraries(myexe foo)
请参见 http://www.cmake.org/Wiki/CMake/Tutorials/Exporting_and_Importing_Targets 了解更多详情。
这篇关于CMake中的项目构建配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!