Add ros cmake to an already existing cmake - c++

I have a really big code with this cmake that works:
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(MYPROJECT)
set (CMAKE_CXX_STANDARD 11)
find_package(PCL 1.7 REQUIRED)
if(DEFINED PCL_LIBRARIES)
list(REMOVE_ITEM PCL_LIBRARIES "vtkproj4")
endif()
FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED)
include_directories(${PCL_INCLUDE_DIRS})
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
add_executable (main src/main.cpp)
target_link_libraries (main ${PCL_LIBRARIES})
Now, I want to publish the results of this code to a ros topic. So I added this code to a ros workspace, and need to add ros stuff into the cmake. I changed the project name to the pkg name, and added to the cmake the find_package, include_directories and catkin_package, ending with this CMake:
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
project(plc)
set (CMAKE_CXX_STANDARD 11)
find_package(PCL 1.7 REQUIRED)
if(DEFINED PCL_LIBRARIES)
list(REMOVE_ITEM PCL_LIBRARIES "vtkproj4")
endif()
find_package(catkin REQUIRED COMPONENTS
pcl_conversions
pcl_ros
roscpp
)
FIND_PACKAGE(Boost COMPONENTS program_options REQUIRED)
include_directories(${catkin_INCLUDE_DIRS})
include_directories(${PCL_INCLUDE_DIRS})
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})
catkin_package(CATKIN_DEPENDS roscpp pcl_ros pcl_conversions)
add_executable (main src/main.cpp)
target_link_libraries (main ${PCL_LIBRARIES})
also added this to the package.xml:
<build_depend>pcl_conversions</build_depend>
<build_depend>pcl_ros</build_depend>
<build_depend>roscpp</build_depend>
<build_export_depend>pcl_conversions</build_export_depend>
<build_export_depend>pcl_ros</build_export_depend>
<build_export_depend>roscpp</build_export_depend>
<exec_depend>pcl_conversions</exec_depend>
<exec_depend>pcl_ros</exec_depend>
<exec_depend>roscpp</exec_depend>
But I keep getting this errors, that according to google means that I made the CMake wrong.
usr/bin/ld: main.cpp:(.text+0x6bdc): undefined reference to `ros::Rate::Rate(double)'
/usr/bin/ld: main.cpp:(.text+0x6beb): undefined reference to `ros::NodeHandle::ok() const'
/usr/bin/ld: main.cpp:(.text+0x6c07): undefined reference to `ros::Time::now()'
/usr/bin/ld: main.cpp:(.text+0x6c3e): undefined reference to `ros::spinOnce()'
/usr/bin/ld: main.cpp:(.text+0x6c4d): undefined reference to `ros::Rate::sleep()'
/usr/bin/ld: main.cpp:(.text+0x6c5e): undefined reference to `ros::Publisher::~Publisher()'
/usr/bin/ld: main.cpp:(.text+0x6c6d): undefined reference to `ros::NodeHandle::~NodeHandle()'
/usr/bin/ld: main.cpp:(.text+0x6cfc): undefined reference to `ros::Publisher::~Publisher()'
/usr/bin/ld: main.cpp:(.text+0x6d0b): undefined reference to `ros::NodeHandle::~NodeHandle()'
Any idea how can I fix this? I'm clueless.
PS: I have another workspace with python ros that publishes/subscribes without problems, and this code was working perfectly before I added the ros part in cpp.

Make sure you also link agains the catkin specified libraries (${catkin_LIBRARIES}), because there the ROS libs are listed in:
target_link_libraries (main
${PCL_LIBRARIES})
${catkin_LIBRARIES}
)

Related

undefined reference to boost::system::generic_category() cmake

I have an undefined reference while trying to compile my Qt5 project.
I am usually pretty confident with using CMake but this time I have a strange error that I can't figure out:
undefined reference to boost::system::generic_category()
/usr/local/include/boost/system/error_code.hpp:223: undefined reference to 'boost::system::generic_category()'
My configurations are:
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Qt5.15
cmake version 3.16.3
After typing whereis boost the outcome wasboost: /usr/include/boost and after applying the great power of dpkg -s libboost-dev | grep 'Version' :) the version is Version: 1.71.0.0ubuntu2
I don't understand what is happening, below an example of how my CMakeLists.txt is structured:
cmake_minimum_required (VERSION 3.1)
project(projectA)
set (OpenCV_DIR /home/to/opencv/build)
find_package( OpenCV REQUIRED )
find_package( Boost COMPONENTS system thread filesystem REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
find_package(Qt5Widgets)
find_package(Qt5PrintSupport)
#make them into headers
qt5_wrap_ui (UI_HDRS ${UI})
add_executable(projectA main/main.cpp ui/qdarkstyle/style.qrc ${SRCS} ${UI_HDRS} ${UI_SRCS})
target_link_libraries (projectA Qt5::Widgets ${Boost_LIBRARIES} ${OpenCV_LIBS} Qt5::PrintSupport)
add_library(projectA_lib SHARED ${SRCS} ${UI_HDRS})
target_include_directories (projectA_lib PUBLIC "src/" "ui/")
link_directories(${Boost_LIBRARY_DIRS})
target_link_libraries (projectA_lib Qt5::Widgets ${Boost_LIBRARIES} ${OpenCV_LIBS})
I have searched and applied solutions I saw on all possible sources I was able to find such as:
This source but that didn't work.
Also from here it seems that this solution shall be applied:
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost REQUIRED COMPONENTS system)
# the call to include_directories is now useless:
# the Boost::system imported target used below
# embeds the include directories
project(APP C CXX)
add_executable(APP src.cpp)
target_link_libraries(APP Boost::system)
However that also didn't do any specific benefits to finding the solution.
Other posts I consulted were this, this one but no answer was provided.
This post was useful but that also didn't provide any advice that didn't already know.
Thanks for pointing to the right direction and trying to find a solution.

How to add OpenXLSX to my project in c++?

I have a project and I want to use OpenXLSX as excel library. However, I can't add this to my project.
I move all OpenXLSX files into my project folder, it didn't work. Also moved them to my Desktop, it didn't work either.
Here is my CMakeList.txt file:
#cmake_minimum_required(VERSION 3.5)
#project(PCL_Visualizer LANGUAGES CXX)
#set(CMAKE_CXX_STANDARD 11)
#set(CMAKE_CXX_STANDARD_REQUIRED ON)
#add_executable(PCL_Visualizer main.cpp)
cmake_minimum_required(VERSION 3.5)
project(mainwindow)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# init_qt: Let's do the CMake job for us
set(CMAKE_AUTOMOC ON) # For meta object compiler
set(CMAKE_AUTORCC ON) # Resource files
set(CMAKE_AUTOUIC ON) # UI files
# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "/home/fatih/Desktop")
# Find the QtWidgets library
find_package(Qt5 REQUIRED Widgets)
find_package(VTK REQUIRED)
find_package(PCL 1.7.1 REQUIRED)
find_package(OpenXLSX REQUIRED)
# Fix a compilation bug under ubuntu 16.04 (Xenial)
#list(REMOVE_ITEM PCL_LIBRARIES "vtkproj4")
include_directories(${PCL_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/OpenXLSX-master)
#include_directories(OpenXLSX-master/library/headers)
#include_directories(OpenXLSX-master/library/external/pugixml)
#include_directories(OpenXLSX-master/library/external/nowide/nowide)
#include_directories(OpenXLSX-master/library/external/zippy)
add_definitions(${PCL_DEFINITIONS})
set(project_SOURCES main.cpp mainwindow.cpp)
add_executable(${PROJECT_NAME} ${project_SOURCES} icons.qrc)
target_link_libraries(${PROJECT_NAME} ${PCL_LIBRARIES} Qt5::Widgets)
Everytime I try to cmake .. and make, i get this error.
Could not find a package configuration file provided by "OpenXLSX" with any
of the following names:
OpenXLSXConfig.cmake
openxlsx-config.cmake
Add the installation prefix of "OpenXLSX" to CMAKE_PREFIX_PATH or set
"OpenXLSX_DIR" to a directory containing one of the above files. If
"OpenXLSX" provides a separate development package or SDK, be sure it has
been installed.
I just add these commands to my CMakeList.txt and problem solved partially.
include_directories(/home/fatih/Desktop/OpenXLSX-master/library)
include_directories(/home/fatih/Desktop/OpenXLSX-master/build/library)
include_directories(/home/fatih/Desktop/OpenXLSX-master/library/headers)
link_directories(/home/fatih/Desktop/OpenXLSX-master/build/library)
link_directories(/home/fatih/Desktop/OpenXLSX-master/build/library/headers)
link_directories(/home/fatih/Desktop/OpenXLSX-master/library/headers)
target_link_libraries(${PROJECT_NAME} OpenXLSX VERSION 0.2.0 LANGUAGES CXX)
However, now I get different type of error.
/usr/bin/ld: cannot find -lOpenXLSX
/usr/bin/ld: cannot find -lVERSION
/usr/bin/ld: cannot find -l0.2.0
/usr/bin/ld: cannot find -lLANGUAGES
/usr/bin/ld: cannot find -lCXX
/usr/bin/ld: cannot find -lOpenXLSX
/usr/bin/ld: cannot find -lVERSION
/usr/bin/ld: cannot find -l0.2.0
/usr/bin/ld: cannot find -lLANGUAGES
/usr/bin/ld: cannot find -lCXX
Edit: I solved that problem too. If someone gets an error like this, I changed the target_link_library comment to this:
target_link_libraries(${PROJECT_NAME} /home/fatih/Desktop/OpenXLSX-master/build/output/libOpenXLSX-shared.so)

undefined reference to `boost::system::generic_category()

I'm developing a cpp project in Clion. For the build I use cmake. Last weekend I upgraded from ubuntu 18.04 to 20.04, from then I cannot build my project and I get the error:
/usr/local/include/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()'. It seems that something in the linking isn't right. I reinstalled all packages, tried different versions but nothing. My CMakeLists.txt is:
cmake_minimum_required(VERSION 3.7)
project(pki_cpp)
set(CMAKE_CXX_STANDARD 11)
set(THREADS_PREFER_PTHREAD_FLAG ON)
#find_package(libmongocxx REQUIRED)
find_package(mongocxx REQUIRED)
find_package(bsoncxx REQUIRED)
include_directories(${LIBMONGOCXX_INCLUDE_DIR})
include_directories(${LIBBSONCXX_INCLUDE_DIR})
include_directories(SYSTEM ./lib)
set(BOOST_ROOT /usr/lib/)
find_package(Boost 1.71.0 REQUIRED COMPONENTS system chrono thread filesystem)
include_directories(${Boost_INCLUDE_DIR})
if(NOT Boost_FOUND)
message(FATAL_ERROR "Could not find boost!")
endif()
find_package(Threads REQUIRED)
find_package(OpenSSL REQUIRED)
# A custom library
add_subdirectory(asnDatatypes)
include_directories({CMAKE_CURRENT_SOURCE_DIR} asnDatatypes)
set_property(TARGET asnLibrary PROPERTY POSITION_INDEPENDENT_CODE ON)
# end custom library
#cryptopp library
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/contrib/cmake")
find_package(CryptoPP REQUIRED)
include_directories(${CRYPTOPP_INCLUDE_DIR})
link_directories(${CryptoPP_INCLUDE_DIR})
# end cryptopp
add_executable(pki_cpp main.cpp rootCA.cpp rootCA.h)
target_include_directories(pki_cpp PRIVATE ${Boost_INCLUDE_DIRS} ${LIBMONGOCXX_INCLUDE_DIRS} )
link_directories(pki_cpp ${Boost_LIBRARY_DIRS})
target_link_libraries(pki_cpp ${Boost_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY} ${Boost_LIBRARIES} Boost::system Threads::Threads boost_system asnLibrary OpenSSL::SSL ${CRYPTOPP_LIBRARIES} ${LIBMONGOCXX_LIBRARIES} mongo::bsoncxx_shared mongo::mongocxx_shared ${CMAKE_THREAD_LIBS_INIT} ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY} Boost::system)
Any help is appreciated!
That's a lot of stuff, all randomly ordered and commented out. The obvious error is a failure to link Boost System
I'd go with
FIND_PACKAGE(Boost 1.65.0
COMPONENTS system program_options REQUIRED)
LINK_DIRECTORIES(${Boost_LIBRARY_DIRS})
INCLUDE_DIRECTORIES(${Boost_INLUDE_DIRS})
LINK_LIBRARIES(${Boost_LIBRARIES})
Alternatively you can be specific and link specific libs to specific targets
TARGET_LINK_LIBRARIES(mylib ${Boost_SYSTEM_LIBRARY})
TARGET_LINK_LIBRARIES(myexecutable ${Boost_THREAD_LIBRARY})

OpenCV and Gtest conflict in CMake

I am trying to do some Google Tests on my application and I met some conflicts between OpenCV and GTest:
/usr/lib/gcc/i686-linux-gnu/4.6/../../../../lib/libgtest.a(gtest-all.cc.o): In function `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)':
gtest-all.cc:(.text+0xdd84): multiple definition of `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)'
/usr/local/lib/libopencv_ts.a(ts_gtest.cpp.o):ts_gtest.cpp:(.text._ZN7testing8internal8GTestLogC2ENS0_16GTestLogSeverityEPKci+0x0): first defined here
...
The GTest is used in opencv_ts library. Does anyone kows how to resolve these multiple deinitions?
I think that if I add just the libraries that I use from OpenCV it will be resolved, but I do not know how to do this. I have tried:
target_link_libraries(${Exec8name}_test ${OpenCV}/opencv_core.so* ... )
target_link_libraries(${Exec8name}_test ${OpenCV_LIBS}/opencv_core.so* ... )
target_link_libraries(${Exec8name}_test ${OpenCV_LIBS_DIR}/opencv_core.so* ... )
etc, but I get just errors of not found or No rule to make tarket
I have tried to remove one of the two, but I get errors of
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
...
This is my CMakeLists.txt
cmake_minimum_required(VERSION 2.8)
option(test "Build all tests." OFF)
set(EXECUTABLE_NAME MyProj)
project(${EXECUTABLE_NAME})
set(CMAKE_CXX_FLAGS "-g -Wall")
include_directories( src/main/cpp
${Boost_INCLUDE_DIRS}
)
find_package(OpenCV REQUIRED)
find_package(Boost REQUIRED COMPONENTS filesystem
system
regex
program_options)
add_executable(${EXECUTABLE_NAME}_Sol2
src/main/cpp/main.cpp
src/main/cpp/solution2/MySol2.hpp
src/main/cpp/solution2/MySol2.cpp
)
target_link_libraries(${EXECUTABLE_NAME}_Sol2 ${OpenCV_LIBS}
${Boost_LIBRARIES}
)
if (test)
find_package(GTest REQUIRED)
enable_testing()
include_directories( ${GTEST_INCLUDE_DIRS} )
add_executable(${EXECUTABLE_NAME}_Sol2_test
src/test/cpp/test_Sol2.cpp
src/main/cpp/solution2/MySol2.hpp
src/main/cpp/solution2/MySol2.cpp
)
target_link_libraries(${EXECUTABLE_NAME}_Sol2_test ${OpenCV_LIBRARIES}
${Boost_LIBRARIES}
)
target_link_libraries(${EXECUTABLE_NAME}_Sol2_test ${GTEST_LIBRARIES}
pthread
)
add_test(${EXECUTABLE_NAME}_Sol2_test
${CMAKE_CURRENT_BINARY_DIR}/${EXECUTABLE_NAME}_Sol2_test
)
endif()
Can anyone tell me some ways to fix it?
The "opencv_ts" module contains gtest, so you could just include the required OpenCV modules excluded "ts" module insted of all. For example:
find_package(OpenCV REQUIRED core imgproc highgui)
Add just the libs that I need like
target_link_libraries(${EXECUTABLE_NAME}_Sol2 opencv_core
opencv_highgui
opencv_imgproc
...
)

cmake link with libboost_python-py32.so instead of libboost_python.so

I'm trying to build python bindings for a library that i wrote, and i'm having some trouble getting cmake to understand that it should use the boost-python library for python 3.
Here is my cmake file:
cmake_minimum_required(VERSION 2.8)
FIND_PACKAGE(Boost COMPONENTS
system
thread
python REQUIRED)
find_package(PythonLibs REQUIRED)
INCLUDE_DIRECTORIES(${PYTHON_LIBRARIES})
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_DIRS})
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIRS})
ADD_LIBRARY(
pschulze SHARED
src/candidate_relation.cpp
src/schulze.cpp
src/calculate.cpp
src/candidate.cpp
src/ranking.cpp
src/userinput.cpp
python.cpp)
TARGET_LINK_LIBRARIES(pschulze ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
ADD_EXECUTABLE(
schulze
src/candidate_relation.cpp
src/schulze.cpp
src/calculate.cpp
src/candidate.cpp
src/ranking.cpp
src/userinput.cpp
src/json-spirit/json_spirit_reader.cpp
src/json-spirit/json_spirit_value.cpp
main.cpp)
TARGET_LINK_LIBRARIES(schulze ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})
ADD_DEFINITIONS(-std=gnu++0x -Os)
add_subdirectory (tests)
set(CMAKE_BUILD_TYPE Debug)
And this is the linker error that I get:
Linking CXX executable schulze
CMakeFiles/schulze.dir/src/schulze.cpp.o: In function `arg_to_python':
/usr/include/boost/python/converter/builtin_converters.hpp:122: undefined reference to `PyInt_FromLong'
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../lib/libboost_python.so: undefined reference to `PyString_Size'
This might do the trick :
set(Python_ADDITIONAL_VERSIONS 3.2)
find_package(Boost COMPONENTS system thread python-py32 REQUIRED)