Unable to include a header-only library in ROS - cmake

I have the following project structure:
utils
include
utils
image_utils.h
CMakelists.txt
package.xml
graph
src
graph.cpp
CMakelists.txt
package.xml
I am trying to use image_utils.h in graph.cpp.
The CMakelists inside utils looks like this:
cmake_minimum_required(VERSION 3.0.2)
project(utils)
find_package(catkin REQUIRED COMPONENTS roscpp)
catkin_package(
INCLUDE_DIRS include
LIBRARIES ${PROJECT_NAME}
CATKIN_DEPENDS roscpp
)
include_directories(
include
${catkin_INCLUDE_DIRS}
)
add_library(${PROJECT_NAME} INTERFACE)
target_link_libraries(${PROJECT_NAME} INTERFACE ${catkin_LIBRARIES} )
install(
DIRECTORY include/
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
FILES_MATCHING PATTERN "*.h"
)
And in the CMakelists inside graph i call find_package(catkin REQUIRED COMPONENTS roscpp) as seen below:
cmake_minimum_required(VERSION 3.0.2)
project(pose_graph)
set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_CXX_FLAGS "-std=c++11")
find_package(catkin REQUIRED COMPONENTS roscpp std_msgs cv_bridge utils)
find_package(OpenCV 3.4 REQUIRED)
include_directories(${catkin_INCLUDE_DIRS} )
catkin_package(CATKIN_DEPENDS utils)
add_executable(graph src/graph.cpp)
target_link_libraries(graph ${catkin_LIBRARIES} ${OpenCV_LIBS} utils)
The package.xml of graph has the following relevant tag:
<depend>utils</depend>
My error message is:
graph.cpp:12:10: fatal error: utils/image_utils.h: No such file or directory
#include "utils/image_utils.h"
^~~~~~~~~~~~~~~~~~~~~
Any idea what I am doing wrong here?

You need to rearrange your folder structure to utils/include/utils/image_utils.h and then also add an add_library(...) call in your CMakeLists.txt file.

Finally figured it out. Here are my CMakelists files:
utils:
cmake_minimum_required(VERSION 3.0.2)
project(utils)
find_package(catkin REQUIRED COMPONENTS roscpp)
catkin_package(
INCLUDE_DIRS include
CATKIN_DEPENDS roscpp
)
include_directories(
include
${catkin_INCLUDE_DIRS}
)
install(
DIRECTORY include/${PROJECT_NAME}/
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
FILES_MATCHING PATTERN "*.h"
)
graph:
cmake_minimum_required(VERSION 3.0.2)
project(pose_graph)
set(CMAKE_BUILD_TYPE "Release")
set(CMAKE_CXX_FLAGS "-std=c++11")
find_package(catkin REQUIRED COMPONENTS roscpp std_msgs cv_bridge utils)
find_package(OpenCV 3.4 REQUIRED)
catkin_package(CATKIN_DEPENDS utils)
include_directories(${catkin_INCLUDE_DIRS} )
add_executable(graph src/graph.cpp)
target_link_libraries(graph ${catkin_LIBRARIES} ${OpenCV_LIBS})

Related

How to include glew library in cmake file

How to include library in cmake.
The path for the glew header files is"
C:\VizPlugins_Library\Library\glew\glew-2.1.0\include\GL"
The path for library is
C:\VizPlugins_Library\Library\glew\glew-2.1.0\lib\Release\x64
with the lib file having name
"glew32.lib"
The path for dll file
C:\VizPlugins_Library\Library\glew\glew-2.1.0\bin\Release\x64
The name of the file is
glew32.dll
How to include the glew package path in the cmake file.
This is the cmake file.
make_minimum_required(VERSION 3.0)
project(ocean)
include_directories(include)
link_directories(${CMAKE_SOURCE_DIR}/lib)
include_directories(${CMAKE_BINARY_DIR}/include)
set(ENGINE_NAME engine)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_FLAGS "-O4")
set(BUILD_SHARED_LIBS True)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/build)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/build)
set(CMAKE_BUILD_TYPE Release)
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
find_package(GLM REQUIRED)
find_package(GLFW3 REQUIRED)
set(LIBS glfw3 gdi32 opengl32 glew32)
include_directories(
"${CMAKE_SOURCE_DIR}/src"
"${CMAKE_SOURCE_DIR}/include"
)
file(GLOB_RECURSE SOURCES ${CMAKE_SOURCE_DIR}/src/*.cpp)
add_executable(${PROJECT_NAME} ${SOURCES})
target_link_libraries(${PROJECT_NAME} ${LIBS})

Combining two CMakeLists.txt file (ROS and Libtorch)

I am trying to combine two CMakeLists.txt files to compile a C++ program that has both ROS and Libtorch dependencies. The individual files are provided below:
Libtorch CMakeLists.txt file:
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(example-app)
find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
add_executable(example-app example-app.cpp)
target_link_libraries(example-app "${TORCH_LIBRARIES}")
set_property(TARGET example-app PROPERTY CXX_STANDARD 14)
# The following code block is suggested to be used on Windows.
# According to https://github.com/pytorch/pytorch/issues/25457,
# the DLLs need to be copied to avoid memory errors.
if (MSVC)
file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll")
add_custom_command(TARGET example-app
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
${TORCH_DLLS}
$<TARGET_FILE_DIR:example-app>)
endif (MSVC)
I found this here: https://pytorch.org/cppdocs/installing.html
ROS CMakeListis.txt file:
cmake_minimum_required(VERSION 2.8.3)
project(fly_bot_cpp)
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
geometry_msgs
tf
gazebo_msgs
)
include_directories(
include
${catkin_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
)
add_executable(example src/example.cpp)
target_link_libraries(example ${catkin_LIBRARIES})
The program example-app.cpp has both libraries of ROS and LibTorch.
So here is what i tried to do:
cmake_minimum_required(VERSION 2.8.3)
project(fly_bot_cpp)
set(CMAKE_PREFIX_PATH="home/jarvis/libtorch;/opt/ros/melodic")
find_package(catkin REQUIRED COMPONENTS
roscpp
std_msgs
geometry_msgs
tf
rospy
message_generation
)
find_package(Torch REQUIRED)
include_directories(
include
${catkin_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
)
add_executable(test_quad src/test_quad.cpp)
target_link_libraries(test_quad ${catkin_LIBRARIES} "${TORCH_LIBRARIES}")
set_property(TARGET test_quad PROPERTY CXX_STANDARD 14)
The code test_quad.cpp (previously referred to as example-app.cpp) contains ros header files and torch header files:
#include "ros/ros.h"
#include <torch/torch.h>
.
.
.
However, I get the following error.
fatal error: torch/torch.h: No such file or directory
#include <torch/torch.h>
^~~~~~~~~~~~~~~
compilation terminated.
Can someone please help me out??
Thank you so much.
Remove the quotes around ${TORCH_LIBRARIES} in your target_link_libraries.
The line should look like this
target_link_libraries(test_quad ${catkin_LIBRARIES} ${TORCH_LIBRARIES})
This should resolve the torch header.
Please also take a look at this post to find some more answers to where you might get stuck in the future-https://answers.ros.org/question/347885/combining-cmakeliststxt-of-libtorch-and-cmakeliststxt-of-ros-package/#
I hope this answer is helpful to someone new coming onboard to ros and libtorch :)

Unable to compile C++ program with tf2_ros header inclusion

I have installed ros2 dashing diademata. I wrote a ROS2 node using C++14. I would like to read static_tf by creating a buffer and TransformListener
I am getting the following error when I include, #include "tf2_ros/buffer.h" If I do not include, I do not get any error.
The error is as follows
In file included from /opt/ros/dashing/include/tf2/LinearMath/Vector3.h:21:0,
from /opt/ros/dashing/include/tf2/LinearMath/Matrix3x3.h:19,
from /opt/ros/dashing/include/tf2/LinearMath/Transform.h:21,
from /opt/ros/dashing/include/tf2/buffer_core.h:35,
from /opt/ros/dashing/include/tf2_ros/buffer_interface.h:37,
from /opt/ros/dashing/include/tf2_ros/buffer.h:35,
from /module/src/example/include/example/example.h:17,
from /module/src/example/src/example.cpp:7:
/opt/ros/dashing/include/tf2/LinearMath/Scalar.h: In function ‘tf2Scalar tf2Log(tf2Scalar)’:
/opt/ros/dashing/include/tf2/LinearMath/Scalar.h:187:66: error: expression cannot be used as a function
TF2SIMD_FORCE_INLINE tf2Scalar tf2Log(tf2Scalar x) { return log(x); }
CMakeLists.txt is as follows
cmake_minimum_required(VERSION 3.5)
project(curb_odometry)
# Default to C99
if(NOT CMAKE_C_STANDARD)
set(CMAKE_C_STANDARD 99)
endif()
# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic -Wdeprecated-declarations -Wunused-parameter)
endif()
# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(nav_msgs REQUIRED)
find_package(std_msgs REQUIRED)
find_package(tf2_ros REQUIRED)
find_package(tf2 REQUIRED)
find_package(message_filters REQUIRED)
find_package(Boost REQUIRED COMPONENTS system)
find_package (Eigen3 3.3 REQUIRED NO_MODULE)
include_directories(include
${Boost_INCLUDE_DIRS}
${tf2_ros_INCLUDE_DIRS}
${rclcpp_INCLUDE_DIRS}
${sensor_msgs_INCLUDE_DIRS}
${geometry_msgs_INCLUDE_DIRS}
${nav_msgs_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
${EIGEN3_INCLUDE_DIR}
${message_filters_INCLUDE_DIR}
)
link_directories(${Boost_LIBRARY_DIRS})
add_executable(example src/example.cpp src/curb_detection.cpp src/curb_odometry.cpp)
target_link_libraries(example ${Boost_LIBRARIES} )
target_include_directories(example PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
ament_target_dependencies(
example
"rclcpp"
"sensor_msgs"
"geometry_msgs"
"nav_msgs"
"std_msgs"
"boost"
"message_filters"
"geometry2"
"tf2_ros"
"tf2"
)
install(TARGETS example
EXPORT export_${PROJECT_NAME}
DESTINATION lib/${PROJECT_NAME})
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# uncomment the line when a copyright and license is not present in all source files
#set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# uncomment the line when this package is not in a git repo
#set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
ament_package()
Looks like the error is coming from tf library. Any help in this regard will be appreciable.

cannot find -lfcl using fcl library

I want to run a ROS node using fcl library. I just wrote a small code and I edited the CMakeLists.txt to make sure that I can run the node but I got the following error:
/usr/bin/ld: cannot find -lfcl
collect2: error: ld returned 1 exit status
What should I do?
My CMakeLists.txt is the following:
cmake_minimum_required(VERSION 2.8.3)
project(package_name)
find_path(FCL_INCLUDE_DIRS
NAMES fcl/collision.h
HINTS ${PC_FCL_INCLUDEDIR}
PATHS "${CMAKE_INSTALL_PREFIX}/include")
find_package(catkin REQUIRED COMPONENTS
nav_msgs
roscpp
sensor_msgs
visualization_msgs
tf
dynamic_reconfigure
message_generation
laser_geometry
geometry_msgs
cmake_modules
)
find_package(Boost REQUIRED)
find_package(Eigen REQUIRED)
find_package(PCL REQUIRED)
find_package(OpenCV REQUIRED)
find_package(fcl REQUIRED)
generate_dynamic_reconfigure_options(
)
generate_messages(
DEPENDENCIES
geometry_msgs sensor_msgs nav_msgs visualization_msgs
)
catkin_package(
INCLUDE_DIRS include
LIBRARIES ${haptic_teleoperation}
CATKIN_DEPENDS message_runtime nav_msgs roscpp sensor_msgs
DEPENDS eigen
)
###########
## Build ##
###########
## Specify additional locations of header files
## Your package locations should be listed before other locations
# include_directories(include)
include_directories(include
${catkin_INCLUDE_DIRS}
${Eigen_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
${PCL_INCLUDE_DIRS}
${OpenCV_INCLUDE_DIRS}
${FCL_INCLUDE_DIRS}
)
## Declare a cpp library
add_library()
add_executable(fcl_test src/fcl_test.cpp)
add_dependencies(fcl_test
${PROJECT_NAME}_gencfg
${PROJECT_NAME}_generate_messages_cpp
)
target_link_libraries(fcl_test
fcl
${catkin_LIBRARIES}
${Boost_LIBRARIES}
${OpenCV_LIBRARIES}
)
Replace fcl by ${fcl_LIBRARIES} in the link-command:
target_link_libraries(fcl_test
${fcl_LIBRARIES}
${catkin_LIBRARIES}
${Boost_LIBRARIES}
${OpenCV_LIBRARIES}
)
If find_package(fcl REQUIRED) works correctly, this variable should be automatically set.

Assimp with cmake

I want to include assimp to my project using CMake. I have Ubuntu 14.04 LTE and QTCreator.
Project contain of main.cpp and linked libraries stored in libs directory.
Main CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
project(test)
find_package(OpenGL REQUIRED)
# libs contain external libaries
add_subdirectory (libs)
include_directories(
libs/glfw-3.0.4/include
libs/assimp-3.1.1/include/
)
set(allLibs
${GLFW_LIBRARIES}
${OPENGL_LIBRARY}
GLEW_LIB
assimp
)
add_executable(test
main/main.cpp
)
target_link_libraries(Manipulator glfw assimp ${allLibs} )
CMakeList.txt inside libs directory
### GLFW ###
add_subdirectory (glfw-3.0.4)
include_directories(
glfw-3.0.4/include/GLFW/
glew-1.11.0/include/
assimp-3.1.1/include/
)
set(OPENGL_LIBRARY
-lGL -lGLU -lXrandr -lXext -lX11 -lrt
${CMAKE_DL_LIBS}
${GLFW_LIBRARIES}
)
### GLEW ###
set(GLEW_SOURCE
glew-1.11.0/src/glew.c
)
add_library( GLEW_LIB STATIC
${GLEW_SOURCE}
)
target_link_libraries(GLEW_LIB
${OPENGL_LIBRARY}
)
### ASSIMP ###
# Zlib
add_subdirectory( assimp-3.1.1/contrib/zlib )
# Boost workaround
include_directories( assimp-3.1.1/code/BoostWorkaround )
add_definitions( -DASSIMP_BUILD_BOOST_WORKAROUND )
# Compile AssImp
add_subdirectory( assimp-3.1.1/code )
And I receive following error.
CMake Error at libs/assimp-3.1.1/code/CMakeLists.txt:725 (INSTALL):
install TARGETS given no ARCHIVE DESTINATION for static library target
"assimp".
This point's me to this
INSTALL( TARGETS assimp # 725 line
LIBRARY DESTINATION ${ASSIMP_LIB_INSTALL_DIR}
ARCHIVE DESTINATION ${ASSIMP_LIB_INSTALL_DIR}
RUNTIME DESTINATION ${ASSIMP_BIN_INSTALL_DIR}
COMPONENT ${LIBASSIMP_COMPONENT})
How to correctly link this library?
This is a CMakeList.txt that I use to build my project.
All external libraries were stored in libs directory. Main file was stored in main directory.
cmake_minimum_required (VERSION 2.6)
project (Project name)
find_package(OpenGL REQUIRED)
#Adding external CMakeList.txt files
add_subdirectory(libs/glfw-3.1)
#add GLEW as STATIC library
add_library(GLEW STATIC libs/glew-1.11.0/src/glew.c libs/glew-1.11.0/include/GL/glew.h)
#add include directories
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
common/
libs/glfw-3.1/include/
libs/glew-1.11.0/include/
libs/glm/
libs/assimp-3.1.1/include/
libs/CImg_162/CImg.h
)
# set variables that are needed
set(ZLIB_LIBRARIES zlibstatic)
set(ENABLE_BOOST_WORKAROUND ON)
set(BUILD_STATIC_LIB ON)
set(BUILD_ASSIMP_TOOLS ON)
set(ASSIMP_BUILD_STATIC_LIB ON)
add_subdirectory(libs/assimp-3.1.1)
#Add my own liblary
add_subdirectory(common)
set(extLibs
${extLibs}
${GLFW_LIBRARIES}
)
add_executable (openGL main/main.cpp
Source/vertexShader.vs
Source/fragmentShader.fs
)
target_link_libraries(openGL common assimp GLEW glfw ${GLFW_LIBRARIES})
CMakelist.txt from Common looks like this:
add_library(common model.cpp)
This should work on Ubuntu 14.04. If some libraries are missing try to follow steps from this tutorial tutorial