Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
568 views
in Technique[技术] by (71.8m points)

c++ - 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:

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:

/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:

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.

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)

Question:

Why doesn't CMake "convert" the linked target lime_api into -llimeTradingApi.a for my executable?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...