opencv Cross Compiling with cmake - c++

I am trying to cross compile a c++ project that uses openCV and a camera API for Raspberry Pi but I have the following linker error after the compilation finishes successfully: make[2]: No rule to make target '/opt/vc/lib/libmmal_core.so', needed by 'agv_car'. Stop. although the .so file is in an directory that is included in the search directories(see cmake file below)
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
INCLUDE_DIRECTORIES($ENV{HOME}/rpi/rootfs/opt/vc/include)
INCLUDE_DIRECTORIES($ENV{HOME}/rpi/rootfs/opt/vc/include/interface/vcos/pthreads)
INCLUDE_DIRECTORIES($ENV{HOME}/rpi/rootfs/opt/vc/include/interface/vmcs_host/linux)
INCLUDE_DIRECTORIES(/home/mihai/rpi/rootfs/opt/vc/lib)
find_package(OpenCV REQUIRED)
find_package(raspicam REQUIRED)
file(GLOB SOURCE_FILES src/*.cpp)
add_executable(agv_car ${SOURCE_FILES})
target_link_libraries(agv_car ${OpenCv_LIBS} ${raspicam_CV_LIBS})
And here is the tool chain cmake file:
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_VERSION 1)
# Specify the cross compiler
SET(CMAKE_C_COMPILER $ENV{HOME}/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-gcc)
SET(CMAKE_CXX_COMPILER $ENV{HOME}/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-g++)
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} /home/mihai/rpi/rootfs/usr/local/lib/cmake)
# Where is the target environment
SET(CMAKE_FIND_ROOT_PATH $ENV{HOME}/rpi/rootfs)
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --sysroot=${CMAKE_FIND_ROOT_PATH}")
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} --sysroot=${CMAKE_FIND_ROOT_PATH}")
SET(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} --sysroot=${CMAKE_FIND_ROOT_PATH}")
# Search for programs only in the build host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# Search for libraries and headers only in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
Any suggestions would be helpful, I have spent some time trying to figure it but without any success. "rpi/rootfs" is the location where I have copied the root file directory of the Raspberry Pi from the SD card.
EDIT
after doing the changes Tsyvarev suggested I get the following errors: `
In file included from /home/mihai/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/include/c++/4.8.3/bits/locale_facets.h:39:0,
from /home/mihai/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/include/c++/4.8.3/bits/basic_ios.h:37,
from /home/mihai/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/include/c++/4.8.3/ios:44,
from /home/mihai/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/include/c++/4.8.3/istream:38,
from /home/mihai/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/include/c++/4.8.3/sstream:38,
from /home/mihai/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/include/c++/4.8.3/complex:45,
from /home/mihai/rpi/rootfs/usr/local/include/opencv2/core/cvstd.inl.hpp:48,
from /home/mihai/rpi/rootfs/usr/local/include/opencv2/core.hpp:3217,
from /home/mihai/rpi/rootfs/usr/local/include/opencv2/imgproc.hpp:46,
from /home/mihai/work/src/main.cpp:1:
/home/mihai/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/include/c++/4.8.3/cwctype:82:11: error: ‘::wctrans_t’ has not been declared
using ::wctrans_t;
^
/home/mihai/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/include/c++/4.8.3/cwctype:104:11: error: ‘::wctrans’ has not been declared
using ::wctrans;
^
In file included from /home/mihai/rpi/rootfs/usr/local/include/opencv2/imgproc/imgproc_c.h:46:0,
from /home/mihai/rpi/rootfs/usr/local/include/opencv2/imgproc.hpp:4698,
from /home/mihai/work/src/main.cpp:1:
/home/mihai/rpi/rootfs/usr/local/include/opencv2/imgproc/types_c.h: In constructor ‘CvMoments::CvMoments(const cv::Moments&)’:
/home/mihai/rpi/rootfs/usr/local/include/opencv2/imgproc/types_c.h:423:62: error: call of overloaded ‘sqrt(double&)’ is ambiguous
inv_sqrt_m00 = am00 > DBL_EPSILON ? 1./std::sqrt(am00) : 0;
^
/home/mihai/rpi/rootfs/usr/local/include/opencv2/imgproc/types_c.h:423:62: note: candidates are:
In file included from /home/mihai/rpi/rootfs/usr/local/include/opencv2/core/cvstd.hpp:65:0,
from /home/mihai/rpi/rootfs/usr/local/include/opencv2/core/base.hpp:58,
from /home/mihai/rpi/rootfs/usr/local/include/opencv2/core.hpp:54,
from /home/mihai/rpi/rootfs/usr/local/include/opencv2/imgproc.hpp:46,
from /home/mihai/work/src/main.cpp:1:
/home/mihai/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/include/c++/4.8.3/cmath:482:3: note: float std::sqrt(float)
sqrt(float __x)
^
/home/mihai/rpi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/arm-linux-gnueabihf/include/c++/4.8.3/cmath:486:3: note: long double std::sqrt(long double)
sqrt(long double __x)
^
CMakeFiles/agv_car.dir/build.make:54: recipe for target 'CMakeFiles/agv_car.dir/src/main.cpp.o' failed
make[2]: *** [CMakeFiles/agv_car.dir/src/main.cpp.o] Error 1
CMakeFiles/Makefile2:60: recipe for target 'CMakeFiles/agv_car.dir/all' failed
make[1]: *** [CMakeFiles/agv_car.dir/all] Error 2
Makefile:76: recipe for target 'all' failed
make: *** [all] Error 2
`

Related

Cross-compiling: How to set the path for libraries for linking?

I want to cross-compile c++ code for my raspberry-pi on my notebook (Ubuntu 18.04) and I spent hours and it still doesn't work.
I followed this little tutorial and it's working.
I want to link against the eclipse paho-mqtt-c++ lib, which I installed on my raspberry. I did rsync with my raspberry as in the tutorial, I now have a rootfs folder and there under rootfs/usr/local/include/ the header files for using the lib are stored and the lib itself is under rootfs/usr/local/lib.
My toolchain cmake file contains the following:
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_VERSION 1)
SET(CMAKE_C_COMPILER $ENV{HOME}/applications/raspberry-tools/gcc-linaro-6.5.0-2018.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc)
SET(CMAKE_CXX_COMPILER $ENV{HOME}/applications/raspberry-tools/gcc-linaro-6.5.0-2018.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-g++)
#SET(CMAKE_SYSROOT /home/manuelhoffmann/applications/raspberry-tools/rootfs)
SET(CMAKE_FIND_ROOT_PATH $ENV{HOME}/applications/raspberry-tools/rootfs)
#SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --sysroot=${CMAKE_FIND_ROOT_PATH}")
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} --sysroot=${CMAKE_FIND_ROOT_PATH}")
SET(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} --sysroot=${CMAKE_FIND_ROOT_PATH}")
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
#SET(CMAKE_LIBRARY_PATH /home/manuelhoffmann/applications/raspberry-tools/rootfs/usr/local/lib)
include_directories(${CMAKE_FIND_ROOT_PATH}/usr/local/include)
#INCLUDE_DIRECTORIES($ENV{HOME}/applications/raspberry-tools/rootfs/usr/local/include)
#link_directories(${CMAKE_FIND_ROOT_PATH}/usr/local/lib)
#find_library(${CMAKE_FIND_ROOT_PATH}/usr/local/lib)
add_definitions(-Wall -std=c11)
The include_directories was necessary so that it finds the headers and now it is compiling fine. But it still has a problem when linking, the output is:
raspberry-tools/gcc-linaro-6.5.0-2018.12-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-g++ -pthread CMakeFiles/kg_iot_gateway.dir/gateway.cpp.o CMakeFiles/kg_iot_gateway.dir/MQTTClient.cpp.o CMakeFiles/kg_iot_gateway.dir/socket.cpp.o CMakeFiles/kg_iot_gateway.dir/snmessage/MessageGWInfo.cpp.o CMakeFiles/kg_iot_gateway.dir/snmessage/MessagePublish.cpp.o CMakeFiles/kg_iot_gateway.dir/snmessage/MQTTSNFlags.cpp.o CMakeFiles/kg_iot_gateway.dir/snmessage/MQTTSNMessage.cpp.o CMakeFiles/kg_iot_gateway.dir/snmessage/MQTTSNMessageHeader.cpp.o CMakeFiles/kg_iot_gateway.dir/snmessage/MQTTSNMessageVariablePart.cpp.o CMakeFiles/kg_iot_gateway.dir/main.cpp.o -o kg_iot_gateway -lpaho-mqttpp3
/applications/raspberry-tools/gcc-linaro-6.5.0-2018.12-x86_64_arm-linux-gnueabihf/bin/../lib/gcc/arm-linux-gnueabihf/6.5.0/../../../../arm-linux-gnueabihf/bin/ld: cannot find -lpaho-mqttpp3
collect2: error: ld returned 1 exit status
CMakeFiles/kg_iot_gateway.dir/build.make:218: recipe for target 'kg_iot_gateway' failed
make[2]: *** [kg_iot_gateway] Error 1
make[2]: Leaving directory '/kg-iot-gateway/build'
CMakeFiles/Makefile2:75: recipe for target 'CMakeFiles/kg_iot_gateway.dir/all' failed
make[1]: *** [CMakeFiles/kg_iot_gateway.dir/all] Error 2
make[1]: Leaving directory '/kg-iot-gateway/build'
Makefile:129: recipe for target 'all' failed
make: *** [all] Error 2
All commented lines didn't work or bring the desired result.
So how can I tell cmake to look for libs also in rootfs/usr/local/lib? What am I doing wrong?
Thanks for helping!

Linking external libraries cmake arduino

I'm trying to link a downloaded library called ArduinoJson to my project with CMake, but it seems that whenever i include the file it is missing, it will just ask for the next file its missing. I will end up with a lot of include lines which i think isnt the right way. I cant seem to find the correct syntax to include a libary in CMake
CMakeLists.txt
cmake_minimum_required(VERSION 2.8.11)
SET(CMAKE_CXX_STANDARD 11)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)
SET(CMAKE_CXX_FLAGS "-DTESTING")
include_directories(
/usr/share/arduino/libraries/ArduinoJson/include
/usr/share/arduino/hardware/arduino/cores/arduino
)
set(files
DataHandler.h
DataHandler.cpp
JSON.cpp
JSON.h
HAL_mock.cpp
HAL_mock.h
HAL_Protocol.h
lib/ArduinoJson/ArduinoJson.h
)
# Locate GTest
find_package(GTest REQUIRED)
# Link runTests with what we want to test and the GTest library
add_executable(runTests DataHandlerTests.cpp JSONTests.cpp HALTests.cpp ${files})
target_link_libraries(runTests GTest::Main ArduinoJson)
The error I get
[ 14%] Building CXX object CMakeFiles/runTests.dir/JSONTests.cpp.o
In file included from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/Internals/../Internals/../String.hpp:14:0,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/Internals/../Internals/DynamicStringBuilder.hpp:11,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/Internals/../Internals/JsonPrintable.hpp:12,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/Internals/../JsonVariant.hpp:13,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/Internals/../JsonBuffer.hpp:14,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/Internals/BlockJsonBuffer.hpp:10,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson/DynamicJsonBuffer.hpp:10,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson.hpp:10,
from /usr/share/arduino/libraries/ArduinoJson/include/ArduinoJson.h:8,
from /usr/share/arduino/libraries/ArduinoJson/ArduinoJson.h:8,
from /home/zizyx/Desktop/git-sensorNetwerk/sensor-network/gtest/JSON.h:6,
from /home/zizyx/Desktop/git-sensorNetwerk/sensor-network/gtest/JSONTests.cpp:5:
/usr/share/arduino/hardware/arduino/cores/arduino/WString.h:29:26: fatal error: avr/pgmspace.h: No such file or directory
compilation terminated.
CMakeFiles/runTests.dir/build.make:86: recipe for target 'CMakeFiles/runTests.dir/JSONTests.cpp.o' failed
make[2]: *** [CMakeFiles/runTests.dir/JSONTests.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/runTests.dir/all' failed
make[1]: *** [CMakeFiles/runTests.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
I would like to add 2 maybe 3 lines which include all the libraries i need for my ArduinoJson library

cmake link xlib directories c++

I'm trying to compile a c++ program that uses xlib with cmake. However, I'm having a problem including and linking xlib libraries in cmake file.
This is the error that I'm getting.
main.cpp:378: undefined reference to `XClearWindow'
collect2: error: ld returned 1 exit status
CMakeFiles/project1.dir/build.make:94: recipe for target 'project1' failed
make[2]: *** [project1] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/project1.dir/all' failed
make[1]: *** [CMakeFiles/project1.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
And when I use just the command line to compile, it works just fine.
I use this command (g++ main.cpp -L/usr/X11R6/lib -lX11)
and this is my cmake file.
cmake_minimum_required(VERSION 3.6)
project(project1)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
link_directories(/usr/X11R6/lib)
include_directories(/usr/share/X11)
set(SOURCE_FILES main.cpp)
add_executable(project1 ${SOURCE_FILES})
In your case, you forgot to specify the libraries that cmake should use to link your application (target_link_libraries or link_libraries).
But, why not just let cmake find the required path, libraries and includes by itself? I suggest you to use find_package(X11). In your case, you can try:
cmake_minimum_required(VERSION 3.6)
project(project1)
set(CMAKE_CXX_STANDARD 11) # for c++11
find_package(X11 REQUIRED)
link_libraries(${X11_LIBRARIES})
include_directories(${X11_INCLUDE_DIR})
set(SOURCE_FILES main.cpp)
add_executable(project1 ${SOURCE_FILES})
I won't be able to explain why (if you have some idea, don't hesitate to comment my answer), but, in my case, I had to link X11 library using this command:
target_link_libraries( DisplayImage "-lX11" )
It works, at least, for the 3.5.1 version of cmake.

CMake and Window .Lib Files

I have been struggling to find an answer to this question across Stackoverflow, at least one that I understand. I recently bought CLion and want to port an older project from Visual Studio over to it. But I have no idea how to link or add .lib files to my project. I need to link Xinput and DSound.
In Visual Studio I just added the headers and then added these to the top of my file
#pragma comment(lib, "XInput.lib")
#pragma comment(lib, "Dsound.lib")
I just have absolutely no idea how to link to those libs using CMake as I am a complete beginner with it.
Any Help would be gladly appreciated
This is my current CMake file
cmake_minimum_required(VERSION 3.3)
project("Handmade_Hero")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
INCLUDE_DIRECTORIES("${CMAKE_CURRENT_BINARY_DIR}")
add_executable("Handmade_Hero" ${SOURCE_FILES})
UPDATE
cmake_minimum_required(VERSION 3.3)
project(Handmade_Hero)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
INCLUDE_DIRECTORIES("${CMAKE_CURRENT_BINARY_DIR}")
add_executable(Handmade_Hero ${SOURCE_FILES})
set( LIBS XInput DSound )
target_link_libraries(Handmade_Hero ${LIBS} )
My Compiler now throws the following errors:
PATH\ClionProjects\Handmade-Hero\main.cpp:3:20: fatal error: Xinput.h: No such file or directory
#include <Xinput.h>
^
compilation terminated.
mingw32-make.exe[3]: *** [CMakeFiles/Handmade_Hero.dir/main.cpp.obj] Error 1
CMakeFiles\Handmade_Hero.dir\build.make:62: recipe for target 'CMakeFiles/Handmade_Hero.dir/main.cpp.obj' failed
mingw32-make.exe[2]: *** [CMakeFiles/Handmade_Hero.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/Handmade_Hero.dir/rule] Error 2
mingw32-make.exe: *** [Handmade_Hero] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/Handmade_Hero.dir/all' failed
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/Handmade_Hero.dir/rule' failed
Makefile:117: recipe for target 'Handmade_Hero' failed
Simply add these after your CMakeLists.txt
SET( LIBS XInput DSound )
TARGET_LINK_LIBRARIES(projectname ${LIBS} )

cmake and external library

I try to use cmake with an external library but i can't. I use minGW and g++ compiler.
My project is organised as follow:
project/
CMakeLists.txt
src
main.cpp
lib
win
x86
SDL2-2.0.3 (contains SDL2.dll, SDL2.lib, SDL2main.lib and SDL2test.lib
include
SDL2 (contains all header)
bin
The cmake generator works `cmake -G"Eclipse CDT4 - Unix Makefiles"
My cmake file:
`
#
# File generated by CMakeBuilder
#
#
cmake_minimum_required(VERSION 2.6)
PROJECT ( project CXX )
################ INCLUDE Libs ####################
SET (CMAKE_INCLUDE_PATH "${PROJECT_SOURCE_DIR}/include/")
SET (CMAKE_LINKER_PATH "${PROJECT_SOURCE_DIR}/lib/win/x86/SDL2-2.0.3/")
message(STATUS "INCLUDE_PATH: ${CMAKE_INCLUDE_PATH}" )
message(STATUS "LINKER_PATH: ${CMAKE_LINKER_PATH}" )
INCLUDE_DIRECTORIES(${CMAKE_INCLUDE_PATH})
LINK_DIRECTORIES(${CMAKE_LINKER_PATH})
set (CMAKE_CXX_FLAGS "-lSDL2main -lSDL2 -lmingw32 ")
#Generate file source
file( GLOB_RECURSE source_files src/*)
ADD_EXECUTABLE(project ${source_files})
set(EXECUTABLE_OUTPUT_PATH bin/${CMAKE_BUILD_TYPE})
But when i launch make, i obtain this error:
`
CMakeFiles/project.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x0): multiple
definition of main
C:/Users/Gege/workspaces/workspaceCPP/project/lib/win/x86/SDL2-2.0.3/SDL2m
ain.lib(./Release/SDL_windows_main.obj):(.text[_main]+0x0): first defined here
Warning: corrupt .drectve at end of def file
C:/Users/Gege/workspaces/workspaceCPP/project/lib/win/x86/SDL2-2.0.3/SDL2m
ain.lib(./Release/SDL_windows_main.obj):(.text[_main]+0x12): undefined reference
to SDL_main'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:/Users/Gege/workspaces/workspaceCPP/project/lib/win/x86/SDL2- 2.0.3/SDL2main.lib(./
Release/SDL_windows_main.obj): bad reloc address 0x8 in section .text[_WinMain#
16]'
collect2.exe: error: ld returned 1 exit status
CMakeFiles/project.dir/build.make:87: recipe for target 'bin/project.exe'
failed
make[2]: *** [bin/project.exe] Error 1
CMakeFiles/Makefile2:60: recipe for target 'CMakeFiles/project.dir/all' faile
d
make[1]: *** [CMakeFiles/arduinoGeo.dir/all] Error 2
Makefile:75: recipe for target 'all' failed
make: *** [all] Error 2
`
Actually i don't want to put my lib in minGW folder to keep the build easy for the others.
Thanks for you help