4

Possible Duplicate:
Debug and Release Library Linking with CMAKE (VISUAL STUDIO)

cmake newb here, I would like to tell target_link_libraries to link a debug lib when using the debug configuration and link to a release lib when compiling under the release configuration.

How do I tell cmake to link a different lib file for the debug configuration?

Community
  • 1
  • 1
Zack
  • 43
  • 1
  • 3

2 Answers2

9

The solution is:

SET(LINK_LIBRARY optimized Foo debug Foo_d)
target_link_libraries(MyEXE ${LINK_LIBRARY})
Naszta
  • 7,560
  • 2
  • 33
  • 49
  • Thanks! That's exactly what I was looking for. But now I have to figure out why cmake gives me this error: `Cannot specify link libraries for target "HelloWorld" which is not built by this project.` – Zack Jun 16 '11 at 20:11
  • Ah, nvm I was missing `add_library (...)` – Zack Jun 16 '11 at 20:19
  • 2
    And how to link different libraries for RelWithDebugInfo build configuration? – relaxxx Sep 20 '13 at 10:31
7

The target_link_libraries command lets you use keywords which indicate that the library immediately following is to be used only for the corresponding build configuration, e.g.:

target_link_libraries(foo debug bard.lib optimized bar.lib)

If you add multiple libraries with one target_link_libraries statement, the keyword has to be repeated for each library.

sakra
  • 62,199
  • 16
  • 168
  • 151
  • 1
    Wasted my entire day and this line saved me: If you add multiple libraries with one target_link_libraries statement, the keyword has to be repeated for each library. cheers – maxpayne Jan 10 '21 at 22:44