简体   繁体   English

cmake:链接到STATIC IMPORTED库失败

[英]cmake: linking against STATIC IMPORTED library fails

I have a vendor provided static library. 我有供应商提供的静态库。

I have added it as a STATIC IMPORTED library target, and set properties on the target: 我已将其添加为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}/.."
    )

elsewhere in my source tree I try to link against lime_api , and I get an error: 我尝试在源代码树的其他位置链接到lime_api ,但出现错误:

 /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

However, when I try linking against my own library which includes the lime_api I get the linker error. 但是,当我尝试链接到我自己的包含lime_api的库时,出现链接器错误。

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)

Question: 题:

Why doesn't CMake "convert" the linked target lime_api into -llimeTradingApi.a for my executable? 为什么CMake不链接的目标lime_api “转换”为我的可执行文件的-llimeTradingApi.a

I suspect that you ran into a visibility problem with the IMPORTED library target. 我怀疑你遇到了可视性问题与IMPORTED库目标。 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.

That's why the correct library path is used for the inner trading_demo target, but not for the outer order_example target. 这就是为什么正确的库路径用于内部trading_demo目标,而不用于外部order_example目标的原因。 To fix the problem, adding the GLOBAL option should be sufficient: 要解决此问题,添加GLOBAL选项就足够了:

add_library(lime_api STATIC IMPORTED GLOBAL)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM