Separate compilation C++ ROS
Hello !
I have a main_test.cpp file which include the functions_test.h file whose the definitions are contained in the functions_test.cpp file.
Here the procedure that I followed:
- I placed functions_test.h to this location: include/my_package_name/
- I placed functions_test.cpp and main_test.cpp to this location: src/
I added in the CMakeLists.txt the following lines:
include_directories(include ${catkin_INCLUDE_DIRS})
add_executable(main_test src/main_test.cpp src/functions_test.cpp)
target_link_libraries(main_test ${catkin_LIBRARIES})
My problem is when I run catkin_make, the prototypes of the functions in the functions_test.h file are successfully seen but not the definitions of the functions inside the functions_test.cpp file and therefore I get an undefined reference error.
I don't know what I missed. If someone can help me, I will be grateful,
lfr
I did some changes, but I still have the same error.
Here my new CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.3)
project(my_package_name)
find_package(catkin REQUIRED COMPONENTS
actionlib
move_base_msgs
roscpp
std_msgs
tf
)
catkin_package(
INCLUDE_DIRS include
LIBRARIES functions_test
)
include_directories(include ${catkin_INCLUDE_DIRS})
add_library(functions_test src/functions_test.cpp)
target_link_libraries(functions_test ${catkin_LIBRARIES})
add_executable(main_test src/main_test.cpp)
target_link_libraries(main_test functions_test)
Here the complete error message:
CMakeFiles/main_test.dir/src/main_test.cpp.o: In function `main':
main_test.cpp:(.text+0x22d): undefined reference to `void nav_api::execute_checkpoints<double, bool>(std::vector<move_base_msgs::MoveBaseGoal_<std::allocator<void> >, std::allocator<move_base_msgs::MoveBaseGoal_<std::allocator<void> > > > (*)(double, bool), double, bool)'
main_test.cpp:(.text+0x2da): undefined reference to `void nav_api::execute_checkpoints<double, double, bool>(std::vector<move_base_msgs::MoveBaseGoal_<std::allocator<void> >, std::allocator<move_base_msgs::MoveBaseGoal_<std::allocator<void> > > > (*)(double, double, bool), double, double, bool)'
main_test.cpp:(.text+0x383): undefined reference to `void nav_api::execute_checkpoints<double, bool>(std::vector<move_base_msgs::MoveBaseGoal_<std::allocator<void> >, std::allocator<move_base_msgs::MoveBaseGoal_<std::allocator<void> > > > (*)(double, bool), double, bool)'
collect2: error: ld returned 1 exit status
make[2]: *** [/home/********/catkin_ws/devel/lib/my_package_name/main_test] Error 1
make[1]: *** [my_package_name/CMakeFiles/main_test.dir/all] Error 2
make: *** [all] Error 2
Invoking "make -j2 -l2" failed
The prototypes of the concerned functions is below:
template <typename P0>
void execute_checkpoints(std::vector <move_base_msgs::MoveBaseGoal>(*order)(P0), P0 p0);
template <typename P1, typename P2>
void execute_checkpoints(std::vector <move_base_msgs::MoveBaseGoal>(*order)(P1, P2), P1 p1, P2 p2);
template <typename P1, typename P2, typename P3>
void execute_checkpoints(std::vector <move_base_msgs::MoveBaseGoal>(*order)(P1, P2, P3), P1 p1, P2 p2, P3 p3);
I want to notify that the code works correctly when I include directly the functions_test.cpp at the beginning of main_test.cpp. But I know that it is a bad solution.
Everything looks good to me. Can you please post your complete CMakeLists.txt?
I updated the question (with the new CMakeLists.txt)
In your question you write that you do
add_executable(main_test src/main_test.cpp src/functions_test.cpp)
which is correct. In yourCMakeLists.txt
you don't do this. You write that you updated your file, was it this what you updated?Yes, it was what I updated because it didn't works and I wanted to try what spmaniato advises in his answer.