我有一个链接到主要目标的OBJECT库.该有一个相关库,说.如果我们使用遗留变量,那么很容易:objlibmaintargetobjlibZLIB<package-name>_*
add_library(objlib OBJECT ...)
target_include_directories(objlib ${ZLIB_INCLUDE_DIRS})
...
add_executable(maintarget $<TARGET_OBJECTS:objlib>)
target_link_libraries(maintarget ${ZLIB_LIBRARIES})
Run Code Online (Sandbox Code Playgroud)
但我想将依赖项用作IMPORTED库,因为它更简洁(创建配置模块的便捷方式,即使用install(EXPORT ...),就是这样).
以下代码不起作用,因为target_link_libraries不能与OBJECT库一起使用:
add_library(objlib OBJECT ...)
target_link_libraries(objlib ZLIB::ZLIB)
Run Code Online (Sandbox Code Playgroud)
链接ZLIB::ZLIB到maintarget也不行,objlib没有得到包括目录:
add_library(objlib OBJECT ...)
...
add_executable(maintarget $<TARGET_OBJECTS:objlib>)
target_link_libraries(maintarget ZLIB::ZLIB)
Run Code Online (Sandbox Code Playgroud)
使用中间INTERFACE库(objlib-wrapper)进行黑客攻击也不起作用.
唯一有效的方法是查询IMPORTED库的属性并重新生成<package-name>_*变量中通常可用的信息.这是一个讨厌的解决方法.
有没有更好的办法?
您有一个启用CMake的库项目.您需要在另一个库或可执行文件中使用它.如何使用CMake查找并链接到库?您可能具有以下首选项:
理想情况下,库的用法应如下所示:
add_executable(myexe ...)
target_link_libraries(myexe mylib::mylib)
Run Code Online (Sandbox Code Playgroud) 我需要执行add_custom_command一个捕获其标准输出的命令.shell重定向到文件>不起作用,add_custom_command并且没有任何相关选项.我该怎么做?
传递std::min给函数不会编译.我将libcpp声明复制std::min到我的源文件中并且它可以工作.
std版本有什么问题?clang和gcc也是如此.在godbolt上测试:https://godbolt.org/g/zwRqUA
#include <thread>
#include <algorithm>
namespace mystd {
// declaration copied verbatim from std::min (libcpp 4.0)
template <class _Tp> inline constexpr const _Tp&
mymin(const _Tp& __a, const _Tp& __b)
{
return std::min(__a, __b);
}
}
int main()
{
std::thread thr1(std::min<int>, 2, 3); // compile error
std::thread thr2(mystd::mymin<int>, 2, 3); // works
return 0;
}
Run Code Online (Sandbox Code Playgroud)
clang和gcc的错误:
[x86-64 clang 5.0.0 #1] error: no matching constructor for initialization of 'std::thread'
[x86-64 gcc 7.2 #1] error: no …Run Code Online (Sandbox Code Playgroud) 有没有办法使用存储在变量中的名称(用于将函数传递给函数等)在CMake中调用函数?
这是我尝试过的:
cmake_minimum_required(VERSION 3.0)
function(doThing)
endfunction()
set(FuncVar doThing)
${FuncVar}()
Run Code Online (Sandbox Code Playgroud)
哪个失败了这个错误:
Parse error. Expected a command name, got unquoted argument with text "${FuncVar}".
-- Configuring incomplete, errors occurred!
Run Code Online (Sandbox Code Playgroud)
我不明白为什么这不起作用,但我又是新来的CMake所以我知道什么.
感谢您的任何帮助!