Project Build Configuration in CMake

My question is very similar to CMake: changing the name of Visual Studio and Xcode exectuables depending on the configuration in the project created by CMake . In this message, the name of the output file will change in accordance with the configuration of the project (Debug, Release, etc.). I want to go further. When I know the configuration of the project, I want to tell the executable to link different library names depending on the configuration of the project. I was wondering if there is a variable in CMake that can talk about the configuration of the project. If such a variable exists, my task will become simpler:

if (Project_Configure_Name STREQUAL "Debug") #do some thing elseif (Project_Configure_Name STREQUAL "Release") #do some thing endif() 
+4
source share
2 answers

According to http://cmake.org/cmake/help/v2.8.8/cmake.html#command:target_link_libraries you can specify libraries according to configurations, for example:

 target_link_libraries(mytarget debug mydebuglibrary optimized myreleaselibrary ) 

Be careful that optimized mode means every configuration that is not debugged.

The following is a more complex but more manageable solution:

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) 

See http://www.cmake.org/Wiki/CMake/Tutorials/Exporting_and_Importing_Targets for more details.

+8
source

There is always another way:

  if(CMAKE_BUILD_TYPE MATCHES "release") SET(CMAKE_BUILD_TYPE ${CMAKE_BUILD_TYPE}) else(CMAKE_BUILD_TYPE MATCHES "debug") SET(CMAKE_BUILD_TYPE "debug") endif(CMAKE_BUILD_TYPE MATCHES "release") 

We can use the variable CMAKE_BUILD_TYPE. We can also change this variable at the start of a CMAKE call:

 cmake .. -DCMAKE_BUILD_TYPE:STRING=debug 

Then we can use this variable as an indicator of the assembly configuration.

0
source

Source: https://habr.com/ru/post/1485846/


All Articles