简体   繁体   中英

CMake find_* not searching subdirectories

I have been trying for quite some time to get my cross-compile toolchain from my WSL Ubuntu to RasberryPi working. Since the GCC Version of my RaspberryPi 4B is 8.3.0, I have most recently tried using the Buster toolchain from here . My current folder structure looks like this:

Test
  -> CMakeLists.txt
  -> main.cpp
Toolchain
  -> arm-linux-gnueabihf
  -> bin
  -> include
  -> lib
  -> libexec
  -> share
raspi_root
  -> lib
  -> usr

The Test folder contains a simple test project to get the toolchain working. The real target project is a bit larger. The toolchain is the cross-compiler toolchain downloaded from the repository and finally the raspi_root folder is a copy from the lib and usr folders from the Raspberry Pi. The main.cpp looks pretty basic:

#include <iostream>
#include <bluetooth>

int main(int argc, char** argv)
{
    std::cout << "Hello World" << std::endl;
}

Note that it includes the bluetooth header as this is one of the libraries I am having troubles with.

My CMakeLists.txt looks like this:

set(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})

SET(CMAKE_SYSTEM_NAME Linux)
set(COMPILER_PREF "/home/user/workspace/toolchain/bin/arm-linux-gnueabihf-")
SET(CMAKE_C_COMPILER "${COMPILER_PREF}gcc")
set(CMAKE_CXX_COMPILER "${COMPILER_PREF}g++")

set(SYSROOT "${SOURCE_DIR}/../raspi_root")
set(CMAKE_FIND_ROOT_PATH "${SYSROOT}")
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

cmake_minimum_required(VERSION 3.0) # setting this is required

set(project_target Test)
project(${project_target})            # this sets the project name

set(SOURCES main.cpp)

find_library(BLUETOOTH bluetooth REQUIRED)

add_executable(${project_target} ${SOURCES})
set_property(TARGET ${project_target} PROPERTY CXX_STANDARD 17)

target_link_libraries(${project_target} PUBLIC
    # ${SYSROOT}/usr/lib/arm-linux-gnueabihf/libbluetooth.so
    ${BLUETOOTH}
)

I tried to keep it as simple as possible to only get it to compile my hello world program with the toolchain and link the bluetooth library to get it working on my Raspberry Pi.

Now the following problem occurs: Wenn I do cmake . it does not find the bluetooth library files:

Please set them or make sure they are set and tested correctly in the CMake files:
BLUETOOTH
    linked by target "Test" in directory /home/user/workspace/Test

They are there right under ${SYSROOT}/usr/lib/arm-linux-gnueabihf Only libraries in traditional directories like ${SYSROOT}/usr/lib or ${SYSROOT}/lib are found. When I create a symlink from one of those directories to libbluetooth.so, cmake appears to find the library although later on when linking I get additional errors. I also tried replacing find_library by putting the complete library path under target_link_libraries but then the library is found but the headers of course are missing. Those would have to be included seperately I suppose.

However, my question is, why is CMake's find_* function not browsing through further subdirectories? This also happens when linking boost-libraries. find_package(boost) finds the headers but does not link the libraries.

___ EDIT: ___

Thanks to Tsyvarev I could successfully compile and execute my hello world programm. Now for the next step I wanted to include the boost thread library which caused the most problems in my other project. So I added #include <boost/thread.hpp> to my main.cpp and find_package(Boost COMPONENTS thread REQUIRED) , ${Boost_INCLUDE_DIRS} to the target_include_directories and ${Boost_LIBRARIES} to the target_link_libraries in my CMakeLists.txt. Now it finds my libraries with cmake . -DCMAKE_TOOLCHAIN_FILE=Toolchain cmake . -DCMAKE_TOOLCHAIN_FILE=Toolchain and generates build files. But with make I get the following linking errors:

<path to toolchain linker>/ld: warning: librt.so.1, needed by ../raspi_root/usr/lib/arm-linux-gnueabihf/libboost_thread.so, not found (try using -rpath or -rpath-link)
<path to toolchain linker>/ld: warning: libpthread.so.0, needed by ../raspi_root/usr/lib/arm-linux-gnueabihf/libboost_thread.so, not found (try using -rpath or -rpath-link)
<path to toolchain linker>/ld: ../raspi_root/usr/lib/arm-linux-gnueabihf/libboost_thread.so: undefined reference to `pthread_setspecific@GLIBC_2.4'
<path to toolchain linker>/ld: ../raspi_root/usr/lib/arm-linux-gnueabihf/libboost_thread.so: undefined reference to `pthread_condattr_setclock@GLIBC_2.4'
<path to toolchain linker>/ld: ../raspi_root/usr/lib/arm-linux-gnueabihf/libboost_thread.so: undefined reference to `pthread_key_create@GLIBC_2.4'
<path to toolchain linker>/ld: ../raspi_root/usr/lib/arm-linux-gnueabihf/libboost_thread.so: undefined reference to `pthread_join@GLIBC_2.4'
<path to toolchain linker>/ld: ../raspi_root/usr/lib/arm-linux-gnueabihf/libboost_thread.so: undefined reference to `pthread_detach@GLIBC_2.4'
<path to toolchain linker>/ld: ../raspi_root/usr/lib/arm-linux-gnueabihf/libboost_thread.so: undefined reference to `pthread_getspecific@GLIBC_2.4'
<path to toolchain linker>/ld: ../raspi_root/usr/lib/arm-linux-gnueabihf/libboost_thread.so: undefined reference to `pthread_create@GLIBC_2.4'
<path to toolchain linker>/ld: /home/felix/workspace/raspi_root/usr/lib/arm-linux-gnueabihf/libboost_chrono.so: undefined reference to `clock_gettime@GLIBC_2.4'

I figure the undefined references will be resolved once the libraries are found correctly? CMakeCache.txt says the found thread library is libpthread.a not libpthread.so . Also when running make VERBOSE=1 this is the linking command (with breaks for readability):

/home/felix/workspace/toolchain/bin/arm-linux-gnueabihf-g++    
    -rdynamic CMakeFiles/Test.dir/main.cpp.o  
    -o Test
    -Wl,-rpath,/home/felix/workspace/Test/../raspi_root/usr/lib/arm-linux-gnueabihf:/home/felix/workspace/raspi_root/usr/lib/arm-linux-gnueabihf
    ../raspi_root/usr/lib/arm-linux-gnueabihf/libboost_thread.so
    /home/felix/workspace/raspi_root/usr/lib/arm-linux-gnueabihf/libboost_chrono.so
    /home/felix/workspace/raspi_root/usr/lib/arm-linux-gnueabihf/libboost_system.so
    /home/felix/workspace/raspi_root/usr/lib/arm-linux-gnueabihf/libboost_date_time.so
    /home/felix/workspace/raspi_root/usr/lib/arm-linux-gnueabihf/libboost_atomic.so
    ../raspi_root/usr/lib/arm-linux-gnueabihf/libpthread.a
    ../raspi_root/usr/lib/arm-linux-gnueabihf/libbluetooth.so

Do I need to make CMake search for the shared libraries instead of the static ones? How would I do that? If not, what am I missing?

Thank you for your help.

Okay thanks to Tsyvarev for the quick hints to what I was missing. Turns out the answer is more obvious than I thought.

As Tsyvarev mentioned in the comments CMake does not search recursively through the specified root directory for libraries. Instead it searches the standard paths such as /usr/lib, /lib, etc. Further search paths can be added by appending them to CMAKE_LIBRARY_PATH as stated in the documentation . So to fix my errors I did the following: I appended my desired library paths to the search path.

...
set(SOURCES main.cpp)
set(CMAKE_LIBRARY_PATH 
    ${CMAKE_LIBRARY_PATH}
    "/lib/arm-linux-gnueabihf"
    "/usr/lib/arm-linux-gnueabihf"
)
add_executable(${project_target} ${SOURCES})
...

I then saw that I forgot to add any include directories:

target_include_directories(${project_target} PUBLIC
    ${SYSROOT}/usr/include/bluetooth
    ${SYSROOT}/usr/include/arm-linux-gnueabihf
)

And I was able to compile the main.cpp .

As for the EDIT : To properly link the boost thread library I also had to link its dependencies. So I put:

find_package(Boost COMPONENTS thread REQUIRED)

...

target_include_directories(${project_target} PUBLIC
    ${SYSROOT}/usr/include/bluetooth
    ${SYSROOT}/usr/include/arm-linux-gnueabihf
    ${Boost_INCLUDE_DIRS}
)

target_link_libraries(${project_target} PUBLIC
    rt
    Threads::Threads
    ${Boost_LIBRARIES}
    ${BLUETOOTH}
)

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