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 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}
)
Related
On Ubuntu, I have two directories: build and src. In src, my CMakeLists.txt file has the lines:
add_executable(Test main.cpp)
target_link_libraries(Test libCamera.so)
After running cmake in the build directory (cmake ../src), I then copy my library file libCamera.so into the build directory. After running make, the main.cpp.o file compiles successfully, but I receive the following error during linking:
/usr/bin/ld: cannot find -lCamera
Why is this? The shared library is in the same directory that I am building in... and the same thing happens if I copy the library to /usr/bin...
You should not put prefix lib and suffix .so of the library, so just use:
target_link_libraries(Test Camera)
if your library not found you may need to add directory, where library is located:
link_directories( /home/user/blah ) # for specific path
link_directories( ${CMAKE_CURRENT_BINARY_DIR} ) # if you put library where binary is generated
Note: you copied lib to /usr/bin but unlike Windows where dll files stored with executables, in Linux that is not the case, so it would be /usr/lib, not /usr/bin. Also you may change LD_LIBRARY_PATH variable to make your program to find a library in a custom location.
I'm trying to use glbinding in my own project. I'm using cmake to build everything. The problem is linker cannot find this library. Probably I don't build library thus it cannot be linked, but I don't know how to achive that.
I've written linking code according to https://github.com/hpicgs/glbinding#linking-binaries.
Cmake:
set(SOURCE_FILES main.cpp)
add_executable(AKOpenGLEngine ${SOURCE_FILES})
set(CMAKE_PREFIX_PATH ${CMAKE_MODULE_PATH} glbinding )
find_package(glbinding REQUIRED)
include_directories(${GLBINDING_INCLUDES})
target_link_libraries(AKOpenGLEngine glbinding ${GLBINDING_LIBRARIES})
Error:
Linking CXX executable AKOpenGLEngine
ld: library not found for -lglbinding
main.cpp:
#include <glbinding/gl/gl.h>
int main(void) {
glbinding::Binding::initialize();
exit(EXIT_SUCCESS);
}
My current project structure:
Have you tried to remove the glbinding from target_link_libraries? ${GLBINDING_LIBRARIES} should be sufficient; it passes <your_specific_file_path_to_glbinding_library> to the linker. With -lglbinding the linker searches for a library within some default directories, your glbinding or build directory not included, thus throwing a library not found. To verify the content of ${GLBINDING_LIBRARIES} you can print it to cmake output, e.g., via message(STATUS ${GLBINDING_LIBRARIES}). However, i also suggest to integrate glbinding as external project as suggested by #janisz.
EDIT: sorry, didn't see the valid, but collapsed answer of #jet47
I want to compile an example code from this site (at the bottom). I downloaded GLFW 3.0.4 source code and I built it in a custom location. I used a default settings and GLFW was built as a static library. My CMakeLists.txt looks like this:
cmake_minimum_required(VERSION 2.8)
project(glfw_test_project)
SET(CMAKE_CXX_FLAGS "-Wall -Werror -g -std=c++11")
find_package(OpenGL REQUIRED)
include_directories(/home/user/MyLibs/OpenGL/glfw-3.0.4/include)
link_directories(/home/user/MyLibs/OpenGL/glfw-3.0.4/build/src)
add_executable(glfw_test main.cpp)
target_link_libraries(glfw_test ${OPENGL_gl_LIBRARY})
target_link_libraries(glfw_test glfw3)
When I run make I get following info:
Linking CXX executable glfw_test
/usr/bin/ld: /home/user/MyLibs/OpenGL/glfw-3.0.4/build/src/libglfw3.a(x11_clipboard.c.o): undefined reference to symbol 'XConvertSelection'
//usr/lib/x86_64-linux-gnu/libX11.so.6: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [glfw_test] Error 1
make[1]: *** [CMakeFiles/glfw_test.dir/all] Error 2
make: *** [all] Error 2
What is wrong ?
Edit1
When I build GLFW as a dynamic library everything works fine. Then the last line in my CMakeLists.txt have to be replaced with:
target_link_libraries(glfw_test glfw)
What is wrong when using a static library ?
I experienced the same problems on Ubuntu 14.04 LTS.
First: error adding symbols: DSO missing from command line shows a wrong order of linker command line parameters. This post made me realise that. You need to call $ make VERBOSE=1 to see the linker call and check it with your own eyes. Remember: TARGET_LINK_LIBRARIES() should be the last command in your CMakeLists.txt And have a look at this order explanation.
Second: if you build against static GLFW3 libraries all the X11 libraries are not automatically added to your linker call, as atsui already showed above. In the GLFW-Documentation you can find a cmake variable which should fit your needs: ${GLFW_STATIC_LIBRARIES}. To make use of it and enable a more automatic build process, let pkg-config find all dependencies for X11 libs:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(myProject)
FIND_PACKAGE( PkgConfig REQUIRED )
pkg_search_module( GLFW3 REQUIRED glfw3 ) # sets GLFW3 as prefix for glfw vars
# now ${GLFW3_INCLUDE_DIR}, ${GLFW3_LIBRARIES} and ${GLFW3_STATIC_LIBRARIES}
# are set
INCLUDE_DIRECTORIES( ${GLFW3_INCLUDE_DIR} )
ADD_EXECUTABLE( myProject mySource.cpp )
TARGET_LINK_LIBRARIES( myProject ${GLFW_STATIC_LIBRARIES} )
# they include X11 libs
Now check the linker lib parameters again with $ make VERBOSE=1 and you should find many more X11 libs like:
-lXrandr -lXi -lXrender -ldrm -lXdamage -lXxf86vm -lXext -lX11 and -lpthread.
Third: when linking against the shared library, the dependencies to X11 etc. are resolved dynamically. That is why you don't need to explicitly add X11 lib flags to the linker. In that case you only need:
TARGET_LINK_LIBRARIES( myProject ${GLFW_LIBRARIES} )
Fourth: it happened to me, that some glfwCommands had undefined reference to - linker errors. I found out, that I installed GLFW3 in /usr/local/lib but the linker was given only the LD_LIBRARY_PATH environment variable which was only set to /usr/lib32. Adding the parameter -L/usr/local/lib solved the reference problem. To avoid it in future I've set LD_LIBRARY_PATH=/usr/lib32;/usr/local/lib;.
Hint: to check what values are in your GLFW cmake variables, print them during build:
FOREACH(item ${GLFW3_STATIC_LIBRARIES})
MESSAGE(STATUS " using lib: " ${item})
ENDFOREACH()
It seems that you resolved your own issue by rebuilding GLFW as a shared library and linking the example code with that. Instead, I'll try to answer your followup question about why it doesn't work when you try to link the static library.
Basically, if you want to link against the static library libglfw3.a, you need to link all of the dependent X11 libraries. You get an error linking the static library because you didn't specify any additional libraries to link, and CMake doesn't know what these dependencies are. You don't get an error when you link the shared library libglfw.so because the X11 libraries are linked to the shared library, and CMake knows to pull those in for you automatically.
If you want to use the static library, you can determine the necessary libraries to link as follows. According to the .pc file in GLFW, you can type this in the command-line to find out what these are:
pkg-config --static --libs x11 xrandr xi xxf86vm gl
If you translate this into a CMake command, it would look like this:
target_link_libraries( glfw_test glfw3 Xrandr Xrender Xi GL m dl drm Xdamage X11-xcb xcb-glx xcb-dri2 xcb-dri3 xcb-present xcb-sync xshmfence Xxf86vm Xfixes Xext X11 pthread xcb Xau Xdmcp)
So if you add this to the CMakeLists.txt, the example code should build without an issue.
By the way, you can automatically find the X11 libraries using CMake in the following way:
find_package(X11 REQUIRED)
target_link_libraries( my_program ${X11_LIBRARIES} )
However, the set of libraries stored in the X11_LIBRARIES variable will only contain a subset of the libraries needed for statically linking GLFW.
I'm not really sure how to properly handle static/dynamic linking in CMake, but I hope this helps.
I downloaded libboost1.50-all in Raspberry Pi and has successfully compiled and execute a program using threads. Libraries were also found in CMake. I then copied the libraries of the boost and its include from /usr/lib and /usr/include/boost respectively to C:\Boost such that the hierarchy becomes:
C:
-> Boost
-> lib
... files
-> include
-> boost
... files
I then used the same CMakeLists.txt and the source code but the library was not found.
NOTE: The cross compiler that I used is fully working and I was able to produce an executable with CMake in Cygwin using the std library. I even specified the location of the library and the user and the root.
Is there anything that I missed out?
cmake_minimum_required(VERSION 2.8)
set(BOOST_ROOT C:/Boost/)
set(BOOST_INCLUDEDIR C:/Boost/include/)
set(BOOST_LIBRARYDIR C:/Boost/lib/)
SET(Boost_DEBUG ON)
find_package(Boost 1.50.0 COMPONENTS thread system)
if (Boost_FOUND)
include_directories (${Boost_INCLUDE_DIRS})
add_executable (thread thread.cpp)
target_link_libraries(thread ${Boost_LIBRARIES})
endif()
Use CMAKE -GUI and then check whether your boost libraries are detected . If not then manually set in the CMAKE-GUI and configure again.
TEMPORARY SOLUTION that I was able to come up to!
I placed the boost lib and include to where the cross compiler is installed since the cross compile was able to link the source code to libstdc++.
The library is placed here:
C:\cygwin\opt\cross\x-tools\arm-unknown-linux-gnueabi\arm-unknown-linux-gnueabi\sysroot\lib
The include files are placed here:
C:\cygwin\opt\cross\x-tools\arm-unknown-linux-gnueabi\arm-unknown-linux-gnueabi\include\c++\4.6.3
The CMakeLists content is as follows now:
cmake_minimum_required(VERSION 2.8)
add_executable (thread main.cpp)
target_link_libraries(thread boost_thread boost_system)
Open Cygwin terminal and invoke cmake there, then make. Viola! It now compiles successfully! :>
The magic lies in here:
target_link_libraries(thread boost_thread boost_system)
I found this in one of the questions here in Stackoverflow where someone said to manually link the libraries..
Even though that it worked, why is it that CMake cannot detect the boost library both in Windows (Cygwin terminal) and Linux (VMware) -- These I tried -- but the library was found in Raspberry Pi (Raspbian) using the same CMakeLists.txt and main.cpp. Isn't it the purpose of CMake is to find the libraries itself? If I were just to link them manually, better do it like this then:
arm-unknown-linux-gnueabi-g++.exe -lboost_thread -lboost_system
I'm new to this forum, but I've seen it a few times while trying to search for a solution to this problem. I'm trying to generate KML files to use in Google Earth using a C++ API library I found for Ubuntu (libkml-dev_1.2.0-1ubuntu6_amd64). I installed the package using the command sudo apt-get install libkml-dev, came back successful. Afterwards, I used the command line terminal to navigate to the examples folder to try and execute the program 'helloworld.cc' with the command g++ helloworld.cc -o helloworld, but then got a slew of errors (mainly claiming that kmldom is an undefined reference). Sorry, I wanted to attach the text file, but don't know how so I included a sample of the error below. I've searched high and low all week, made sure the header files were indeed included in the download, and even contacted the Google Earth developers about the problem (and they responded that they have nothing to do with this and to redirect all questions regarding this issue to StackOverflow).
Does anyone know what is causing this problem(s) and what I can do to resolve this so I can move on please?
/tmp/cc5u2JyV.o: In function HelloKml(bool)': helloworld.cc:(.text+0x17): undefined reference to kmldom::KmlFactory::GetFactory()'
helloworld.cc:(.text+0x27): undefined reference to kmldom::KmlFactory::CreateCoordinates() const' helloworld.cc:(.text+0x328): undefined reference to kmldom::AsPoint(boost::intrusive_ptrkmldom::Element)'
/tmp/cc5u2JyV.o: In function boost::intrusive_ptr<kmldom::Coordinates>::intrusive_ptr(kmldom::Coordinates*, bool)': helloworld.cc:(.text._ZN5boost13intrusive_ptrIN6kmldom11CoordinatesEEC2EPS2_b[_ZN5boost13intrusive_ptrIN6kmldom11CoordinatesEEC5EPS2_b]+0x3d): undefined reference to kmlbase::intrusive_ptr_add_ref(kmlbase::Referent*)'
/tmp/cc5u2JyV.o: In function boost::intrusive_ptr<kmldom::Coordinates>::~intrusive_ptr()': helloworld.cc:(.text._ZN5boost13intrusive_ptrIN6kmldom11CoordinatesEED2Ev[_ZN5boost13intrusive_ptrIN6kmldom11CoordinatesEED5Ev]+0x23): undefined reference to kmlbase::intrusive_ptr_release(kmlbase::Referent*)'
/tmp/cc5u2JyV.o: In function boost::intrusive_ptr<kmldom::Geometry>::intrusive_ptr(boost::intrusive_ptr<kmldom::Geometry> const&)': helloworld.cc:(.text._ZN5boost13intrusive_ptrIN6kmldom8GeometryEEC2ERKS3_[_ZN5boost13intrusive_ptrIN6kmldom8GeometryEEC5ERKS3_]+0x35): undefined reference to kmlbase::intrusive_ptr_add_ref(kmlbase::Referent*)'
collect2: ld returned 1 exit status
When you compile application that uses library, you need to link it when your application compiles. So, try using this parameters:
To specify a directory to search for your libs, use -L:
-L/data[...]/lib
To specify the actual library name, use -l:
-labc (links abc.a or abc.so)
To specify a directory to search for include files, use -I:
-I/data[...]/lib
I've found the solution. The problem is that you are linking libkml incrorrectly. You should find the paths to the header files and to the library files (*.so). I found them here:
/usr/include/kml/ (include dit with headers),
/usr/lib/x86_64-linux-gnu/ (library dir).
I use CMake and CMakeLists.txt for a project. Using libkml may look like this:
`cmake_minimum_required (VERSION 3.2)
# Set language standard
set(CMAKE_CXX_STANDARD "11")
project (test_proj)
add_definitions(-std=c++11)
# Set default build type to RelWithDebInfo if not specified
if (NOT CMAKE_BUILD_TYPE)
message (STATUS "Default CMAKE_BUILD_TYPE not set using Release with Debug Info")
set (CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE
STRING "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel"
FORCE)
endif()
# linking boost library
find_package(Boost COMPONENTS system filesystem thread REQUIRED)
if(NOT Boost_FOUND)
message(SEND_ERROR "Failed to find boost.")
return()
else()
include_directories(${Boost_INCLUDE_DIRS})
endif()
add_executable(test_proj main.cpp)
set (LibKML_INCLUDE_DIRS /usr/include/kml/)
set (LibKML_LIBRARIES /usr/lib/x86_64-linux-gnu/libkmlbase.so /usr/lib/x86_64-linux-gnu/libkmlconvenience.so /usr/lib/x86_64-linux-gnu/libkmldom.so /usr/lib/x86_64-linux-gnu/libkmlengine.so /usr/lib/x86_64-linux-gnu/libkmlregionator.so /usr/lib/x86_64-linux-gnu/libkmlxsd.so)
message("LibKML is at: ${LibKML_INCLUDE_DIRS} and ${LibKML_LIBRARIES}")
message("Boost is at: ${Boost_INCLUDE_DIRS} and ${Boost_LIBRARIES}")
target_include_directories(test_proj
PUBLIC
${LibKML_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS} )
target_link_libraries(test_proj
PUBLIC
${LibKML_LIBRARIES} ${Boost_LIBRARIES})
install(TARGETS test_proj EXPORT test_proj_export)
export(EXPORT test_proj_export FILE cmake/test_proj-targets.cmake)`