简体   繁体   中英

CMake include shared library and it's dependencies

I am trying to include this C++ library with CMake. As I understood, the file that I need to include is located at lib/libbinacpp/lib/libbinacpp.so (relative to the library root folder).

So, I created a new folder, with two subfolders

  • src , which only contains one file: src/main.cpp (a simple Hello, World)
  • lib , which contains a folder binacpp , the result of cloning the library

On the top level, I have my CMakeLists.txt . First, I tried to write the following:

cmake_minimum_required(VERSION 3.20)
project(RebalancerBot)

set(CMAKE_CXX_STANDARD 20)

#Include binacpp
add_library(binacpp SHARED IMPORTED)
set_target_properties(binacpp PROPERTIES IMPORTED_LOCATION ${CMAKE_BINARY_DIR}/lib/binacpp/lib/libbinacpp/lib/libbinacpp.so)

add_executable(RebalancerBot src/main.cpp)

#Link everything after adding the executable
target_link_libraries(RebalancerBot PRIVATE binacpp)

Which resulted in the error

make[3]: *** No rule to make target 'lib/binacpp/lib/libbinacpp/lib/libbinacpp.so', needed by 'RebalancerBot'.  Stop.

To my surprise, I got a different error after replacing ${CMAKE_BINARY_DIR} with the actual root path of my folder (ie /home/actual_path/ ). I found this surprising since I thought that ${CMAKE_BINARY_DIR}$ should exactly be the path to the root CMakeLists.txt file. Anyway, after the replacement, I got the following new errors:

/usr/bin/ld: warning: libcrypto.so.1.0.0, needed by ../lib/binacpp/lib/libbinacpp/lib/libbinacpp.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libwebsockets.so.11, needed by ../lib/binacpp/lib/libbinacpp/lib/libbinacpp.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: ../lib/binacpp/lib/libbinacpp/lib/libbinacpp.so: undefined reference to `lws_client_connect_via_info'
/usr/bin/ld: ../lib/binacpp/lib/libbinacpp/lib/libbinacpp.so: undefined reference to `lws_callback_on_writable'
... (the list goes on)

If I understand correctly, these errors are due to the dependencies of libbinacpp.so . How can fix them?

PS: It should be noted that, if I cd into lib/binacpp/src and make inside of this directory, everything runs without errors.

EDIT: Thanks to @AlanBirtles comment, I found that the error with CMAKE_BINARY_DIR was due to the fact that it unfolds to the directory I run cmake in. What I needed was CMAKE_SOURCE_DIR : the directory containing my cmake file.

CMake offers the possibility to have a build directory which differs from the source directory. A offer we should gladly accept, because it keeps our build system from messing with our source and we can have different build directories, depending on build type, compiler, target system, etc.

  • ${CMAKE_BINARY_DIR} is a shortcut to your top-level build directory. CMake will generate all files here during the build.
  • ${CMAKE_SOURCE_DIR} is where CMake is looking for your sources. CMake is not intended to make changes in your source directory.

If you copied the library to your source directory, you need to reference it with the right shortcut.

By the way, are you sure, you want to copy the library to the source directory? Because you don't need to. You can use find_file ( https://cmake.org/cmake/help/latest/command/find_file.html ), find_library ( https://cmake.org/cmake/help/latest/command/find_library.html ), find_path ( https://cmake.org/cmake/help/latest/command/find_path.html ) or find_program ( https://cmake.org/cmake/help/latest/command/find_program.html ) to checkout the location of the item you need and use this path to define your imported library.

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