# CMake Functions work with 'catkin_make' but fail with 'catkin build

I have two catkin packages. One package will be called 'function_maker' and the other is 'function_user'. Here are my two CMakeLists.txt:

function_maker CMakeLists.txt

cmake_minimum_required(VERSION 2.8.3)
project(function_maker)

find_package(catkin REQUIRED)

catkin_package()

function(test_func arg)
message("Function Called")
endfunction()


function_user CMakeLists.txt

cmake_minimum_required(VERSION 2.8.3)
project(function_user)

find_package(catkin REQUIRED
COMPONENTS
function_maker
)

catkin_package()

test_func()


When I use 'catkin_make' to build my workspace, everything works as expected and during the build process I see a message that says "Function Called".

However, when I use 'catkin build' to build my workspace, I get the following error message:

Unknown CMake command "test_func".


Why is there a difference between 'catkin_make' and 'catkin build'? How can I make this work with both build tools?

edit retag close merge delete

find_package(catkin REQUIRED
COMPONENETS
function_maker


is this a copy-pasta? COMPONENETS is not a recognised argument to find_package(..).

( 2019-04-10 14:05:22 -0500 )edit

( 2019-04-10 14:07:10 -0500 )edit

Sort by » oldest newest most voted

The most likely reason is that catkin_make coalesces all projects (ie: ROS packages) into a single build (equivalent to calling add_subdirectory(..)) whereas catkin_tools (which supplies the catkin command) builds all packages in isolation.

(Edit: including @Dirk Thomas' comment) Refer to Migrating from catkin_make in the catkin_tools documentation for more information on the differences between catkin_make and catkin_tools.

When I use 'catkin_make' to build my workspace, everything works as expected and during the build process I see a message that says "Function Called".

However, when I use 'catkin build' to build my workspace, I get the following error message:

So with catkin_make, test_func() is defined (and declared) in the same build context, while with catkin_tools, test_func() only exists within the context in which function_maker is processed.

This leads to test_func() being an unknown symbol in the latter case, hence the error message.

How can I make this work with both build tools?

One approach could be to use the CFG_EXTRAS argument of the catkin_package(..) macro (documentation).

This would allow you to export the function in such a way that its definition (and declaration) are imported into the build context of function_user, provided function_user properly find_package(..)s function_maker.

more

I will be testing this out today and will do so. Thank you for your help

( 2019-04-11 09:23:54 -0500 )edit

@gvdhoorn I thought I had this working, but went to catkin build today and I am running into an issue. The CFG_EXTRAS cmake file I created seems to not have access to some CMAKE variables. Specifically, the .cmake.in file thinks that ${function_maker_SOURCE_DIR} is an empty string. The reason I need this, is because "test_func" really uses some python scripts in${function_maker_SOURCE_DIR}/src to generate headers from an XML file. Any idea how I can solve this?

( 2019-04-20 16:43:35 -0500 )edit

@gvdhoorn I ended up printing all defined cmake variables and found that instead of SOURCE_DIR, the extra cmake files defined the path as SOURCE_PREFIX. Was this documented? I am very unfamiliar with cmake, so maybe I should have known this.

( 2019-04-20 17:01:18 -0500 )edit