与CMAKE (VISUAL STUDIO)链接的调试和发布库

时间:2022-09-18 12:30:12

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:

已经有一条线起作用了。比如我想要链接Foo。lib用于发布配置和Foo_d。lib用于调试配置,如何实现这一点?如果我这样做:

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!

谢谢很多!

4 个解决方案

#1


25  

The solution is:

解决方案是:

SET(LINK_LIBRARY optimized Foo debug Foo_d)
target_link_libraries(MyEXE ${LINK_LIBRARY})

#2


19  

target_link_libraries takes a list, you don't need to call it twice. The following will work:

target_link_libraries获取一个列表,您不需要调用它两次。以下工作:

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.

注意,如果您还将库作为CMake项目的一部分构建,您不需要指定调试或优化。CMake将为您选择合适的。

#3


8  

There is no problems when your library is a part of the project or you're importing it using config mode of find_package command (see documentation and example). In case you can't modify 3rd party so it will produce <package>Config.cmake (it may not use cmake tool or you don't want to do it) the answer is to emulate such process:

当您的库是项目的一部分或者您正在使用find_package命令的配置模式导入它时,没有问题(请参阅文档和示例)。如果您不能修改第三方,那么它将生成 配置。cmake(它可能不使用cmake工具或您不想使用它)的答案是模拟这样的过程:

add_library(foo STATIC IMPORTED)
set_target_properties(foo PROPERTIES IMPORTED_LOCATION_DEBUG "/path/to/foo-d.lib")
set_target_properties(foo PROPERTIES IMPORTED_LOCATION_RELEASE "/path/to/foo.lib")

target_link_libraries(MyEXE foo)

note that unlike "debug"/"optimized" feature such approach is not limited to Debug/Release configs:

注意,与“调试”/“优化”特性不同,这种方法不仅限于调试/发布配置:

set_target_properties(foo PROPERTIES IMPORTED_LOCATION_MINSIZEREL "/path/to/foo-small.lib")

also you've got some goodies like INTERFACE_INCLUDE_DIRECTORIES:

还有一些好东西,比如interface_include_directory:

set_target_properties(foo PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "/path/to/foo/includes")

include_directories("/path/to/foo/includes") # this line not needed
target_link_libraries(MyEXE foo) # this command will add "/path/to/foo/includes" for you

and transitive linking:

和过渡连接:

add_library(boo STATIC IMPORTED)
set_target_properties(boo PROPERTIES IMPORTED_LOCATION_DEBUG "/path/to/boo-d.lib")
set_target_properties(boo PROPERTIES IMPORTED_LOCATION_RELEASE "/path/to/boo.lib")

add_library(foo STATIC IMPORTED)
set_target_properties(foo PROPERTIES IMPORTED_LOCATION_DEBUG "/path/to/foo-d.lib")
set_target_properties(foo PROPERTIES IMPORTED_LOCATION_RELEASE "/path/to/foo.lib")

set_target_properties(foo PROPERTIES INTERFACE_LINK_LIBRARIES boo) # foo depends on boo

target_link_libraries(MyEXE foo) # boo will be linked automatically

Of course you can use regular cmake commands like find_library and find_package(... MODULE) to estimate locations instead of hardcoding them.

当然,您可以使用常规的cmake命令,如find_library和find_package(……)模块)估计位置而不是硬编码。

#4


6  

If you have debug/release libs that follow a certain pattern, like _d on the debug ones, you can avoid repeating yourself with:

如果您有遵循某种模式的调试/发布libs,比如调试的_d,您可以避免重复以下内容:

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.

行,target_link_libraries预计。

#1


25  

The solution is:

解决方案是:

SET(LINK_LIBRARY optimized Foo debug Foo_d)
target_link_libraries(MyEXE ${LINK_LIBRARY})

#2


19  

target_link_libraries takes a list, you don't need to call it twice. The following will work:

target_link_libraries获取一个列表,您不需要调用它两次。以下工作:

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.

注意,如果您还将库作为CMake项目的一部分构建,您不需要指定调试或优化。CMake将为您选择合适的。

#3


8  

There is no problems when your library is a part of the project or you're importing it using config mode of find_package command (see documentation and example). In case you can't modify 3rd party so it will produce <package>Config.cmake (it may not use cmake tool or you don't want to do it) the answer is to emulate such process:

当您的库是项目的一部分或者您正在使用find_package命令的配置模式导入它时,没有问题(请参阅文档和示例)。如果您不能修改第三方,那么它将生成 配置。cmake(它可能不使用cmake工具或您不想使用它)的答案是模拟这样的过程:

add_library(foo STATIC IMPORTED)
set_target_properties(foo PROPERTIES IMPORTED_LOCATION_DEBUG "/path/to/foo-d.lib")
set_target_properties(foo PROPERTIES IMPORTED_LOCATION_RELEASE "/path/to/foo.lib")

target_link_libraries(MyEXE foo)

note that unlike "debug"/"optimized" feature such approach is not limited to Debug/Release configs:

注意,与“调试”/“优化”特性不同,这种方法不仅限于调试/发布配置:

set_target_properties(foo PROPERTIES IMPORTED_LOCATION_MINSIZEREL "/path/to/foo-small.lib")

also you've got some goodies like INTERFACE_INCLUDE_DIRECTORIES:

还有一些好东西,比如interface_include_directory:

set_target_properties(foo PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "/path/to/foo/includes")

include_directories("/path/to/foo/includes") # this line not needed
target_link_libraries(MyEXE foo) # this command will add "/path/to/foo/includes" for you

and transitive linking:

和过渡连接:

add_library(boo STATIC IMPORTED)
set_target_properties(boo PROPERTIES IMPORTED_LOCATION_DEBUG "/path/to/boo-d.lib")
set_target_properties(boo PROPERTIES IMPORTED_LOCATION_RELEASE "/path/to/boo.lib")

add_library(foo STATIC IMPORTED)
set_target_properties(foo PROPERTIES IMPORTED_LOCATION_DEBUG "/path/to/foo-d.lib")
set_target_properties(foo PROPERTIES IMPORTED_LOCATION_RELEASE "/path/to/foo.lib")

set_target_properties(foo PROPERTIES INTERFACE_LINK_LIBRARIES boo) # foo depends on boo

target_link_libraries(MyEXE foo) # boo will be linked automatically

Of course you can use regular cmake commands like find_library and find_package(... MODULE) to estimate locations instead of hardcoding them.

当然,您可以使用常规的cmake命令,如find_library和find_package(……)模块)估计位置而不是硬编码。

#4


6  

If you have debug/release libs that follow a certain pattern, like _d on the debug ones, you can avoid repeating yourself with:

如果您有遵循某种模式的调试/发布libs,比如调试的_d,您可以避免重复以下内容:

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.

行,target_link_libraries预计。