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 :).

fritzmg
  • 2,494
  • 3
  • 21
  • 51

3 Answers3

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.

tibur
  • 11,531
  • 2
  • 37
  • 39
  • 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 Oct 04 '10 at 07: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 Oct 04 '10 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 Oct 04 '10 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 Oct 04 '10 at 20:12
  • I believe it is $(Configuration) and not $(ConfigurationName) – munsingh Jul 13 '20 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.
user1050755
  • 11,218
  • 4
  • 45
  • 56
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
stijn
  • 34,664
  • 13
  • 111
  • 163
  • 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 Oct 04 '10 at 07: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 Oct 04 '10 at 07: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 Oct 04 '10 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 Oct 04 '10 at 13:51