7

I have a vs2008 project here. Its project files are generated by CMake. What I want to do is define libraries and library directories for the Debug and Release target independently, i.e. release libs for the release target and debug libs for the debug target of course.

So far I have no idea how to do this. I know I can define different compiler arguments with CMAKE_CXX_FLAGS_DEBUG and CMAKE_CXX_FLAGS_RELEASE for example (or however the build targets are named), but I have no idea how to do this for link dirs and libs.

A colleague who created the CMakeLists file with all the definitions tried it with

IF( CMAKE_BUILD_TYPE MATCHES "Debug" )

for example, but that doesn't work. According to some CMake wiki, the variable CMAKE_BUILD_TYPE is not defined at configuration time, only at run time, depending on what target you are running, naturally.

Currently I am sort of at a dead end and would appreciate any hints or directions :).

3 Answers 3

14

There is a target_link_libraries option that helps you to do that. But you will need to expand your library name to full path.

target_link_libraries(foo
  debug c:/path/to/debug/lib/blah.lib 
  optimized c:/path/to/optimized/lib/blah.lib)

If your library location is named the way CMake do it (Debug/MinSizeRel/RelWithDebInfo/Release), you can use the VS $(ConfigurationName) variable:

link_directories(c:/path/to/all/libs/$(ConfigurationName)/)

Beware, $(ConfigurationName) is not a cmake variable: it will only be expanded by VS during the build/link step.

5
  • Hm, what exactly is CMake creating, if I use TARGET_LINK_LIBRARIES? Is CMake breaking the path apart and writing the information into the project file separately to the library folder and library files?
    – fritzmg
    Commented Oct 4, 2010 at 7:33
  • If you specify a full path for a library in the target_link_libraries command, it will add the library name with its full path into the library files field. I usually don't use link_directories command. My answer was: use target_link_libraries with debug and optimized keywords or use link_directories.
    – tibur
    Commented Oct 4, 2010 at 11:35
  • I see. However, I do have another problem with TARGET_LINK_LIBRARIES. I don't exactly know what to put into the first argument. It may be hard to describe for me what exactly the problem is, since I still do not fully understand, how CMake is set up for this project (someone else made it and now I have to work with it/change something). I think the problem is, that one CMakeLists.txt file is used by 2 projects, which are named differently, and thus I can't use one TARGET_LINK_LIBRARIES( foo debug ... ) call.
    – fritzmg
    Commented Oct 4, 2010 at 13:47
  • The first argument of target_link_libraries(MyProgram ...) (the standard is to use lower case for cmake functions, now) is the name of the program (not the project) you are compiling using add_executable(MyProgram ...).
    – tibur
    Commented Oct 4, 2010 at 20:12
  • I believe it is $(Configuration) and not $(ConfigurationName)
    – munsingh
    Commented Jul 13, 2020 at 11:59
2
include_directories("${DEVINSTPREFIX}$<$<CONFIG:Debug>:/debug>/include")
link_directories("${DEVINSTPREFIX}$<$<CONFIG:Debug>:/debug>/lib")

include_directories("${DEVINSTPREFIX}$<$<CONFIG:Release>:/release>/include")
link_directories("${DEVINSTPREFIX}$<$<CONFIG:Release>:/release>/lib")

This assumes:

  • prefix/ contains mixed stuff where you use find_package to select the correct stuff.
  • prefix/debug contains "manually" configured debug builds
  • prefix/release contains "manually" configured release builds.
0

you can simply invoke cmake with the configuration set already:

cmake -DCMAKE_BUILD_TYPE="Debug"

or what I do is specify link and platform on command line, then adjust configuration type manually:

cmake -dMyConfigType="DebugStaticX86"

#CMakeLists.txt
if( ${MyConfigType} STREQUAL "DebugStaticX86" )
  set( CMAKE_BUILD_TYPE Debug )
  set( MyLinkType Static )
  set( MyPlatform x86 )
  #now include other files that set the actual compile/link options
  #depending on values of MyLinkType and MyPlatform
  ....
endif
4
  • Well, but I want to create one the .vcproj files with the Release and Debug configurations in it. If I do it that way, I will have a project file with only either the debug or the release configuration in it?
    – fritzmg
    Commented Oct 4, 2010 at 7:32
  • as far as I know, yes. Don't think it;s possible to have cmake generate more than one configuration at a time, and that affects all ouputs not only vcproj.
    – stijn
    Commented Oct 4, 2010 at 7:56
  • Well, CMake is capable of doing that. For instance, I can use CMAKE_CXX_FLAGS_DEBUG and CMAKE_CXX_FLAGS_RELEASE (or however the configuration is named) in order to specify compiler flags for each configuration. I thought that there would be a way for other options too, for the linker for instance.
    – fritzmg
    Commented Oct 4, 2010 at 13:46
  • the way tibur shows should do that, but you're still left with a vcproj containing only debug or release, not both.
    – stijn
    Commented Oct 4, 2010 at 13:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.