Currently in the process of porting from ROS1 to ROS2. Having problems getting the makefile to link the exe and libs together without multiple definition errors. I've paired it right back to this simple makefile. Simply building a library file then an exe with has the lib header file as part of its own header. Directory structure looks like this:
cmake_minimum_required(VERSION 3.8)
project(simple_navigation)
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_action REQUIRED)
find_package(nav2_msgs REQUIRED)
find_package(behaviortree_cpp_v3 REQUIRED)
#find_package(yaml-cpp REQUIRED)
find_package(tf2 REQUIRED)
find_package(tf2_geometry_msgs REQUIRED)
find_package(std_msgs REQUIRED)
find_package(std_srvs REQUIRED)
# Install directories
install(DIRECTORY
DESTINATION share/${PROJECT_NAME}
)
# Install C++ behaviors
set(BEHAVIOR_SOURCES
src/simple_navigation.cpp
)
set(TARGET_DEPENDS
rclcpp
behaviortree_cpp_v3
std_srvs
rclcpp_action
nav2_msgs
tf2
tf2_geometry_msgs
)
add_library(ConfigLoaderLib src/ConfigLoader.cpp)
ament_target_dependencies(ConfigLoaderLib ${TARGET_DEPENDS})
target_include_directories(ConfigLoaderLib PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
include_directories(include)
add_executable(simple_navigation src/simple_navigation.cpp )
ament_target_dependencies(simple_navigation ${TARGET_DEPENDS})
target_link_libraries(simple_navigation ConfigLoaderLib )
install(TARGETS
simple_navigation
DESTINATION lib/${PROJECT_NAME})
And errors like:
/usr/bin/ld: libConfigLoaderLib.a(ConfigLoader.cpp.o): in function `Loader::~Loader()':
ConfigLoader.cpp:(.text+0x1e4): multiple definition of `Loader::~Loader()'; CMakeFiles/simple_navigation.dir/src/simple_navigation.cpp.o:simple_navigation.cpp:(.text+0x1ec): first defined here
I'm sure this is something daft - I've been trawling the site etc for answers and having found one that solves it.
Many Thanks
Mark