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