cmake:与STATIC IMPORTED库的链接失败 [英] cmake: linking against STATIC IMPORTED library fails
问题描述
我有一个供应商提供的静态库。
I have a vendor provided static library.
我添加了它作为 STATIC IMPORTED
并在目标上设置属性:
I have added it as a STATIC IMPORTED
library target, and set properties on the target:
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
,我得到一个错误:
elsewhere in my source tree I try to link against lime_api
, and I get an error:
/usr/bin/ld: cannot find -llime_api
我的源代码树看起来像这样:
My source tree looks like this:
src
|
+--- api
| |
| +--- trading
| | - limeTradingApi.a
| | - limeTradingApi.h
| |
| +--- examples
| |
| +--- trading
|
+--- order
|
+--- example
奇怪的是有一个供应商提供的示例链接到此库,并且工作正常:
The strange thing is that there is a vendor provided example which links against this library, and that works fine:
api / examples / trading / CMakeLists.txt
:
api/examples/trading/CMakeLists.txt
:
add_executable (trading_demo exampleClient.cc)
target_link_libraries(trading_demo lime_api) <-- this works
$ b < 我收到链接器错误。
order / CMakeLists.txt
order/CMakeLists.txt
:
add_library(
order
STATIC
${SRCS}
)
target_link_libraries(order lime_api) <-- this doesn't work
order / example / CMakeLists.txt
:
order/example/CMakeLists.txt
:
add_executable (order_example main.cpp)
target_link_libraries(order_example order)
问题: / h2>
为什么CMake 将链接的目标 lime_api
转换为 -llimeTradingApi.a
我的可执行文件?
Question:
Why doesn't CMake "convert" the linked target lime_api
into -llimeTradingApi.a
for my executable?
推荐答案
可视性问题与 IMPORTED
库目标。根据文档:
I suspect that you ran into a visibility problem with the IMPORTED
library target. According to the 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
target,但不适用于外部 order_example
目标。要解决此问题,添加 GLOBAL
选项应该足够了:
That's why the correct library path is used for the inner trading_demo
target, but not for the outer order_example
target. To fix the problem, adding the GLOBAL
option should be sufficient:
add_library(lime_api STATIC IMPORTED GLOBAL)
这篇关于cmake:与STATIC IMPORTED库的链接失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!