Debug and Release Library Linking with CMAKE (VISUAL STUDIO)
Solution 1
target_link_libraries takes a list, you don't need to call it twice. The following will work:
target_link_libraries(MyEXE debug Foo_d optimized Foo)
And to answer a question asked in the comments of another answer, working with multiple libraries works like so:
target_link_libraries(MyEXE
debug Foo1_d optimized Foo1
debug Foo2_d optimized Foo2)
Note that if you also build the library as part of the CMake project, you don't need to specify debug or optimized. CMake will choose the right one for you.
Solution 2
The solution is:
SET(LINK_LIBRARY optimized Foo debug Foo_d)
target_link_libraries(MyEXE ${LINK_LIBRARY})
Solution 3
If you have debug/release libs that follow a certain pattern, like _d on the debug ones, you can avoid repeating yourself with:
set (MY_LIBS
foo
bar
baz
)
# Generate the list of files to link, per flavor.
set (LINK_LIST "")
foreach(x ${MY_LIBS})
list (APPEND LINK_LIST debug ${x}_d optimized ${x})
endforeach()
target_link_libraries (mytarget
commonlib1
commonlib2
${LINK_LIST}
)
This will generate the appropriate
debug foo_d optimized foo
debug bar_d optimized bar
lines that target_link_libraries expects.
Related videos on Youtube
Gabriel
Working in CAD field for the dental industry. I was working with rigid body dynamics at Institute for Mechanical Systems at ETH Zurich.
Updated on August 24, 2020Comments
-
Gabriel over 2 yearsThere was already a Thread which did not help really. I want to be able to link for example Foo.lib for Release Config and Foo_d.lib for Debug Config , how can I achieve this? If I do this:
target_link_libraries(MyEXE debug Foo_d) target_link_libraries(MyEXE optimized Foo)then I have both libraries in my project for the debug config? Why is there no Release option?
Thanks alot!
-
Naszta over 11 yearsI am always looking for solution in pre-made modules. E.g.: in FindQt4.cmake. -
user1036908 over 8 yearshow will this work if I have multiple debug and release libraries. For example I have Foo1_d,Foo2_d & Foo1,Foo2. How can I club them under single variable? Currently it is only able to detect the keywords for the first associated lib(debug or release) and does not recognize for the next