CMake: Iterate over multiple source files for multiple executables

asked 2018-03-08 21:27:31 -0600

piraka9011 gravatar image

updated 2018-03-09 20:16:30 -0600

Hi,

I am trying to minimize the size of my CMakeLists.txt file by iterating over my source files and using a foreach() to add my executables (each of my source files is a seperate executable). I did something similar to this stackoverflow question and other variants but I just cant get my packages to compile. I have tried using set() and file(RELATIVE ...) with and without wildcards (*.cpp) but it still doesn't work. When using the method similar to the stack overflow question, I get the following compilation error:

make[2]: *** No rule to make target 'frasier/frasier_navigation/frasier_behavior/CMakeFiles//home/piraka9011/catkin_ws/src/frasier/frasier_navigation/frasier_behavior/src/conditions/face_rec_condition.dir/depend'.  Stop.
CMakeFiles/Makefile2:5266: recipe for target 'frasier/frasier_navigation/frasier_behavior/CMakeFiles/home/piraka9011/catkin_ws/src/frasier/frasier_navigation/frasier_behavior/src/conditions/face_rec_condition.dir/all' failed
make[1]: *** [frasier/frasier_navigation/frasier_behavior/CMakeFiles/home/piraka9011/catkin_ws/src/frasier/frasier_navigation/frasier_behavior/src/conditions/face_rec_condition.dir/all] Error 2

My structure is like this:

catkin_ws/
    src/
        frasier_behavior/
        CMakeLists.txt
        src/
            foo.cpp    
            conditions/
                condition_a.cpp
                condition_b.cpp

Are there any examples of being able to do so or has anyone come across this issue before? Is this even feasible with the ROS package structure?

EDIT: Per gvdhoorn's request, here's what I tried doing (COND_SRCS are my source files for 'conditions' in a behavior tree)

file( GLOB COND_SRCS src/conditions/*.cpp)
foreach( file ${COND_SRCS})
    string( REPLACE ".cpp" "" name ${file})
    add_executable( ${name} ${file})
    target_link_libraries( ${name}  ${catkin_LIBRARIES})
endforeach( file ${COND_SRCS} )

and have tried replace file(GLOB...) with something like

set(COND_SRCS
  src/conditions/face_rec_condition.cpp
  src/conditions/grasp_condition.cpp
  src/conditions/navigation_condition.cpp
)
edit retag flag offensive close merge delete

Comments

Please add the relevant parts of your CMakeLists.txt that you are having issues with. The link to the SO question is appreciated, but in order to help you, we need to know what you've been doing exactly.

gvdhoorn gravatar imagegvdhoorn ( 2018-03-09 06:06:02 -0600 )edit

COND_SRCS are my source files for 'conditions' in a behavior tree

So each condition is a separate executable?

gvdhoorn gravatar imagegvdhoorn ( 2018-03-11 04:05:49 -0600 )edit

@gvdhoorn exactly, each condition has to be a separate executable.

piraka9011 gravatar imagepiraka9011 ( 2018-03-11 16:57:23 -0600 )edit