ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A answers.ros.org
Ask Your Question
0

colcon build doesn't generate executables

asked 2023-02-22 09:40:05 -0500

user334478 gravatar image

I am trying to get started with ROS2 Humble and when running colcon build no executable files are created.

My project structure looks like this:

examples
├── CMakeLists.txt
├── package.xml
└── src
    ├── CMakeLists.txt
    └── helloWorld.cpp

File contents:

examples/CMakeLists.txt:

cmake_minimum_required(VERSION 3.25)
project(examples LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

find_package(ament_cmake REQUIRED)

add_subdirectory(src)

ament_package()

examples/src/CMakeLists.txt:

add_executable(helloWorld helloWorld.cpp)

examples/helloWorld.cpp

#include <iostream>

using std::cout;
using std::endl;

int main()
{
    cout << "Hello World" << endl;
}

Running colcon build in my workspace root succeeds but no executable helloWorld is generated. However, when I edit examples/CMakeLists.txt like this, it DOES work:

cmake_minimum_required(VERSION 3.25)
project(examples LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

find_package(ament_cmake REQUIRED)

#add_subdirectory(src)
add_executable(helloWorld "src/helloWorld.cpp") #add executable directly instead

ament_package()

This successfully generates the executable and works with ros2 run.

It looks like add_subdirectory doesn't work correctly... probably has to do something with ament... does anyone know how to do this correctly? I usually strcuture my CMake projects with add_subdirectory and want this to work with ROS as well.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2023-03-23 11:33:32 -0500

GeorgNo gravatar image

You need to install the required targets. For more about this see this link: https://docs.ros.org/en/foxy/Tutorial...

add_executable(helloWorld src/helloWorld.cpp)
ament_target_dependencies(helloWorld)

install(TARGETS
  helloWorld
  DESTINATION lib/${PROJECT_NAME})

should do the trick for your simple example

edit flag offensive delete link more

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: 2023-02-22 09:38:29 -0500

Seen: 395 times

Last updated: Mar 23