简体   繁体   中英

CMake can't link external libraries

I have a C++ project that contains source files. For an external project, there are some folders I need to search for included libraries:

/home/data/lib/wisenet
/home/data/lib/wise_log
/home/data/lib/wise_rs_device
/home/data/lib/json
/home/data/lib/wise_versioning

What must I write to include these external libraries in CMake? These folders contain only interface resources (h files and .a libraries).

I tried to include these directories like this:

include_directories(
    /home/data/lib/wisenet
    /home/data/lib/wise_log
    ... etc
)

And I don't understand how to correctly add the lib files such as libwise_rs_device.a .

Include directories only are for... well, include paths for your code. It won't link libraries.

The correct way to use external library is using imported libraries:

add_library(wise_rs_device STATIC IMPORTED GLOBAL)
set_target_properties(wise_rs_device PROPERTIES
    IMPORTED_LOCATION "path/to/static/library"
    INTERFACE_INCLUDE_DIRECTORIES "path/to/headers/of/wise_rs_device"
)

Then, you can simply link the imported target to yours:

# will link to the static library and add include directories.
target_link_libraries(your_executable PRIVATE wise_rs_device)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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