cmake:链接到STATIC IMPORTED库失败

时间:2016-04-25 17:05:21

标签: c++ cmake

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

我已将其添加为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的库时,我收到链接器错误。

order/CMakeLists.txt

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

order/example/CMakeLists.txt

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

问题:

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

1 个答案:

答案 0 :(得分:3)

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

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)