Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug and Release Library Linking with CMAKE (VISUAL STUDIO)

Tags:

There 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!

like image 796
Gabriel Avatar asked Mar 31 '11 08:03

Gabriel


People also ask

How do I set CMake debug mode?

We can use CMAKE_BUILD_TYPE to set the configuration type: cd debug cmake -DCMAKE_BUILD_TYPE=Debug .. cmake --build . cd ../release cmake -DCMAKE_BUILD_TYPE=Release ..

Can you debug CMake?

You can also start a debug session from Solution Explorer. First, switch to CMake Targets View in the Solution Explorer window. Then, right-click on an executable and select Debug. This command automatically starts debugging the selected target based on your active configuration.

Does CMake define debug?

CMake refers to different build configurations as a Build Type. Suggested build types are values such as Debug and Release, but CMake allows any type that is supported by the build tool.

What is RelwithDebInfo?

exerpt: "RelwithDebInfo is quite similar to Release mode. It produces fully optimized code, but also builds the program database, and inserts debug line information to give a debugger a good chance at guessing where in the code you are at any time."


2 Answers

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.

like image 104
scl Avatar answered Oct 23 '22 05:10

scl


The solution is:

SET(LINK_LIBRARY optimized Foo debug Foo_d) target_link_libraries(MyEXE ${LINK_LIBRARY}) 
like image 32
Naszta Avatar answered Oct 23 '22 03:10

Naszta