Ask Your Question
0

Separate compilation C++ ROS

asked 2016-05-30 06:10:43 -0500

lfr gravatar image

updated 2016-06-06 03:41:00 -0500

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.

edit retag flag offensive close merge delete

Comments

1

Everything looks good to me. Can you please post your complete CMakeLists.txt?

BennyRe gravatar image BennyRe  ( 2016-05-30 06:58:49 -0500 )edit

I updated the question (with the new CMakeLists.txt)

lfr gravatar image lfr  ( 2016-05-31 07:37:37 -0500 )edit

In your question you write that you do add_executable(main_test src/main_test.cpp src/functions_test.cpp) which is correct. In your CMakeLists.txt you don't do this. You write that you updated your file, was it this what you updated?

BennyRe gravatar image BennyRe  ( 2016-06-06 07:43:16 -0500 )edit

Yes, it was what I updated because it didn't works and I wanted to try what spmaniato advises in his answer.

lfr gravatar image lfr  ( 2016-06-06 07:58:07 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2016-06-14 09:49:37 -0500

lfr gravatar image

updated 2016-06-14 09:50:06 -0500

Hello !

I found my mistake. I forgot that templates functions must be defined inside the header file. It works properly now.
Thank you to all of you for helping me.

lfr.

edit flag offensive delete link more
0

answered 2016-05-30 08:36:06 -0500

spmaniato gravatar image

Not the most elegant solution for your use case, but I think this will work:

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)
edit flag offensive delete link more

Comments

I did as you said but I still have the same error.
If I have just basics functions inside functions_test, it works correctly but if I have functions which have pointer to another function in parameter, I have the undefined reference error and I don't know why.

lfr gravatar image lfr  ( 2016-05-31 07:19:54 -0500 )edit

Currently, I work with a #include "functions_test.cpp" at the beginning of the main_test.cpp file. It works but I know it is not a good solution.

lfr gravatar image lfr  ( 2016-05-31 07:21:17 -0500 )edit

That doesn't sound like a CMake issue then. There's some other problem. Could you update the original question with the exact compilation error message please? And maybe the problematic function declaration too, if it's not already in the error message. Thanks

spmaniato gravatar image spmaniato  ( 2016-05-31 14:18:14 -0500 )edit
1

I updated the question with the information you requested.

lfr gravatar image lfr  ( 2016-06-02 02:47:59 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-05-30 06:10:43 -0500

Seen: 247 times

Last updated: Jun 14 '16