0
$\begingroup$

Rosanswers logo

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.


Originally posted by user334478 on ROS Answers with karma: 3 on 2023-02-22

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

You need to install the required targets. For more about this see this link: https://docs.ros.org/en/foxy/Tutorials/Beginner-Client-Libraries/Writing-A-Simple-Cpp-Publisher-And-Subscriber.html#cmakelists-txt

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


Originally posted by GeorgNo with karma: 183 on 2023-03-23

This answer was ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.