gpt4 book ai didi

c++ - cmake:链接 STATIC IMPORTED 库失败

转载 作者:搜寻专家 更新时间:2023-10-31 01:35:44 24 4
gpt4 key购买 nike

我有供应商提供的静态库。

我已将其添加为STATIC IMPORTED 库目标,并在目标上设置属性:

add_library(
lime_api
STATIC
IMPORTED
)

set_target_properties(
lime_api
PROPERTIES
IMPORTED_LOCATION "${CMAKE_CURRENT_LIST_DIR}/trading/limeTradingApi.a"
)

# users include "api/trading/limeTradingApi.h"
set_target_properties(
lime_api
PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_CURRENT_LIST_DIR}/.."
)

在我的源代码树的其他地方,我尝试链接到 lime_api,但出现错误:

/usr/bin/ld: cannot find -llime_api

我的源代码树是这样的:

src
|
+--- api
| |
| +--- trading
| | - limeTradingApi.a
| | - limeTradingApi.h
| |
| +--- examples
| |
| +--- trading
|
+--- order
|
+--- example

奇怪的是,有一个供应商提供的示例链接到这个库,并且工作正常:

api/examples/trading/CMakeLists.txt:

add_executable       (trading_demo exampleClient.cc)
target_link_libraries(trading_demo lime_api) <-- this works

但是,当我尝试链接到我自己的包含 lime_api 的库时,我收到链接器错误。

顺序/CMakeLists.txt:

add_library(
order
STATIC
${SRCS}
)
target_link_libraries(order lime_api) <-- this doesn't work

命令/示例/CMakeLists.txt:

add_executable       (order_example main.cpp)
target_link_libraries(order_example order)

问题:

为什么 CMake “转换” 链接目标 lime_api 为我的可执行文件的 -llimeTradingApi.a

最佳答案

我怀疑您遇到了 IMPORTED 库目标的可见性问题。根据documentation :

An IMPORTED library target references a library file located outside the
project. ... The target name has scope in the directory in which it is
created and below, but the GLOBAL option extends visibility.

这就是为什么正确的库路径用于内部 trading_demo 目标,而不用于外部 order_example 目标。要解决此问题,添加 GLOBAL 选项应该就足够了:

add_library(lime_api STATIC IMPORTED GLOBAL)

关于c++ - cmake:链接 STATIC IMPORTED 库失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36846818/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com