CMAKE cross-compilation libraries not found

I am having strange problems with my cmake cross compiler projects.

My own libraries were found, but not (system) libraries from my toolchain.

I used to use KDevelop on a debian squeeze machine. now on my new debian wheezy machine the setup failed. It does not find system libraries such as m or pthread .

It worked perfectly on my old machine, but I don’t remember that I did anything special to make this work.

Here is one of my CMakeLists.txt files

 cmake_minimum_required(VERSION 2.8) SET(CMAKE_SYSTEM_NAME Linux) SET(CMAKE_SYSTEM_VERSION 2.6.36.4) SET(CMAKE_C_COMPILER arm-angstrom-linux-gnueabi-gcc) SET(CMAKE_CXX_COMPILER arm-angstrom-linux-gnueabi-g++) include_directories(../include ../../../sample/include) project(testmain) add_executable(testmain some_c-source-file.c) set(CMAKE_LIBRARY_PATH ../lib/arm-26/lib ../../../sample/lib/arm-26/lib) find_library(LIBS_TEST NAMES akku) find_library(LIBS_M NAMES m) find_library(LIBS_PTHREAD NAMES pthread ) target_link_libraries(akkumain ${LIBS_TEST} ${LIBS_M} ${LIBS_PTHREAD}) set(CMAKE_C_FLAGS "-Wall -Werror") set(CMAKE_C_FLAGS_DEBUG "-g3 -O2 -rdynamic") set(CMAKE_C_FLAGS_RELEASE "-g0 -O0") set(CMAKE_CXX_FLAGS "-Wall -Werror") set(CMAKE_CXX_FLAGS_DEBUG "-g3 -O2 -rdynamic") set(CMAKE_CXX_FLAGS_RELEASE "-g0 -O0") 

This message is displayed when I try to compile with KDevelop: (to repeat it myself: it worked on my old machine)

 /home/user/testmain/build> /usr/bin/cmake -DCMAKE_BUILD_TYPE=Debug /home/user/testmain/ -- The C compiler identification is GNU 4.3.3 -- The CXX compiler identification is GNU 4.3.3 -- Check for working C compiler: /usr/local/angstrom/arm/bin/arm-angstrom-linux-gnueabi-gcc -- Check for working C compiler: /usr/local/angstrom/arm/bin/arm-angstrom-linux-gnueabi-gcc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working CXX compiler: /usr/local/angstrom/arm/bin/arm-angstrom-linux-gnueabi-g++ -- Check for working CXX compiler: /usr/local/angstrom/arm/bin/arm-angstrom-linux-gnueabi-g++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done CMake Error: The following variables are used in this project, but they are set to NOTFOUND. Please set them or make sure they are set and tested correctly in the CMake files: LIBS_M linked by target "akkumain" in directory /home/user/testmain LIBS_PTHREAD linked by target "akkumain" in directory /home/user/testmain 

So found LIBS_TEST. But not libm or libpthread . I tried this with different projects: all my libraries were found, but not one of the "system" libraries.

I already tried different things, for example

 set(CMAKE_FIND_LIBRARY_PREFIXES lib ) set(CMAKE_FIND_LIBRARY_SUFFIXES .a ) 

and something else that I don’t remember.

The only thing WORKS when I specify the directory manually:

 find_library(ASTLIBS_M NAMES m PATHS /usr/local/angstrom/arm/arm-angstrom-linux-gnueabi/usr/lib) 

After pointing this to my CMakeLists.txt a library is found, and I can compile my project without any errors.

BUT: This is not what I want, because I have many projects and many libraries, and I do not want to edit all my CMakeLists.txt ... CMakeLists.txt

Does anyone know what made my old machine find system libs without specifying anything special inside my IDE / CMake files?

Edit: I just noticed for one of my executables that at the Linker stage it throws some errors that some characters from glibc cannot find - it seems that something is wrong with my debian wheezy system. - I hope I can understand this ...

Edit:

Maybe I should give a brief summary: My code compiles well, but all the libraries from my tool chain are not found, but if I add the path to the libraries of my tool chain manually, it compiles, but does not work at the linker stage.

+4
source share
2 answers

There are certain default paths where CMake find_library . If your system libraries on your old machine are located in one such place, they will be found without any additional work that needs to be done.

However, since your new path to these libraries looks like "/ usr / local / angstrom / arm / arm-angstrom-linux-gnueabi / usr / lib", you need to tell CMake about this.

One of the ways you showed (adding the path explicitly). But in this case, the path is probably specific to this machine only - therefore, it would be better for you to set this path only when CMake is called on this machine. You can add it to CMAKE_PREFIX_PATH , for example:

 cmake . -DCMAKE_PREFIX_PATH=/usr/local/angstrom/arm/arm-angstrom-linux-gnueabi/usr 

(Note: the path in this case gets the "lib" added when find_library called).

Or, if you want to affect only the find_library search find_library , and not all find_xxx modules, set CMAKE_LIBRARY_PATH

 cmake . -DCMAKE_LIBRARY_PATH=/usr/local/angstrom/arm/arm-angstrom-linux-gnueabi/usr/lib 
+2
source

Have you ever tried to use the toolchain file ? I also cross compile ARM and AVR LOT and it works fine without any problems (I also use KDevelop and works great with CMake). The main thing is to specify the path to your root file system toolchain using the variable CMAKE_FIND_ROOT_PATH . Try putting all of this in a file, which I usually name after the architecture that I cross-compile (in this case, I called it arm-unknown-linux-gnueabi.cmake ):

 # the name of the target operating system SET(CMAKE_SYSTEM_NAME Linux) # which C and C++ compiler to use SET(CMAKE_C_COMPILER arm-unknown-linux-gnueabi-gcc) SET(CMAKE_CXX_COMPILER arm-unknown-linux-gnueabi-g++) # here is the target environment located SET(CMAKE_FIND_ROOT_PATH /home/claudio/TS-7400/rootfs) # adjust the default behaviour of the FIND_XXX() commands: # search headers and libraries in the target environment, search # programs in the host environment set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH) set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH) 

Notice the CMAKE_FIND_ROOT_PATH_MODE_xxx variables, where CMake will look for binary files, libraries, and header files. I usually install PROGRAM on NEVER , so it never uses binaries from your cross-architecture root file system, since they will not work on your main machine anyway. For libraries and header files, BOTH means that it will search first for your specified ROOT_PATH, and then if it does not find something, it will go through the host system.

Thus, whenever you want to cross-compile a project, you need to create an assembly directory (so that it does not mix your sources with files created during assembly) and then run cmake from there, specifying the toolchain file that you want to use (I I assume that your CMakeLists.txt along with your toolchain file in the same directory as your sources, is project_sources_dir in my example):

 cd project_sources_dir mkdir build cd build cmake -DCMAKE_TOOLCHAIN_FILE=../arm-unknown-linux-gnueabi.cmake .. 

The whole point of using the toolchain file is that if you want to compile the same project for your host computer, you do not need to change one line in CMakeLists.txt . Just run cmake without specifying the toolchain file:

 cd project_sources_dir mkdir build cd build cmake .. 

and your project is ready to compile native for your main machine. If this is not enough, you can find more information on CMake Cross Compiling

+12
source

Source: https://habr.com/ru/post/1492063/


All Articles