ROS2-编译基础-02

1. 使用g++编译ROS2节点

动态链接库
gcc/g++ 链接库的编译与链接_surgewong的博客-CSDN博客_g++ 使用动态库

程序编译一般需要经预处理、编译、汇编和链接几个步骤

动态库 与 静态库
公共代码需要反复使用,把这些代码编译成为“库”文件。
链接步骤中,链接器将从库文件取得所需的代码,复制到生成的可执行文件中,这种库称为静态(链接)库,
其特点是可执行文件中包含了库代码的一份完整拷贝,缺点是被多次使用就会多份冗余拷贝。

程序在开始运行后调用库函数时才被载入,这种库独立于现有的程序,其本身不可执行,但包含着程序需要调用的一些函数,这种库称为动态(链接)库(Dynamic Link Library)。

在widows平台下,静态链接库是.lib文件,动态库文件是.dll文件。
在linux平台下,静态链接库是.a文件,动态链接库是.so文件。

平台 静态链接库文件扩展名 动态链接库文件扩展名
Windows .lib .dll
Linux .a .so

g++来编译first_node节点

g++ first_ros2_node.cpp

root@ubuntu-16:~/stephen# cat first_ros2_node.cpp
#include "rclcpp/rclcpp.hpp"

int main(int argc, char **argv)
{
    rclcpp::init(argc, argv);
    rclcpp::spin(std::make_shared<rclcpp::Node>("first_node"));
    return 0;
}

报错 找不到文件

first_ros2_node.cpp:3:10: fatal error: rclcpp/rclcpp.hpp: No such file or directory
    3 | #include "rclcpp/rclcpp.hpp"
      |          ^~~~~~~~~~~~~~~~~~~
compilation terminated.

g++ first_ros2_node.cpp -I /opt/ros/humble/include/rclcpp/

-I i的大写 include

-L l的大写 link

/usr/bin/ld: /tmp/ccoA8hho.o: in function `main':
first_ros2_node.cpp:(.text+0x37): undefined reference to `rcutils_get_default_allocator'

undefined reference to 伴随生涯的报错

终级命令:


root@ubuntu-16:~/stephen# cat comp.sh
g++ first_ros2_node.cpp \
-I /opt/ros/humble/include/rclcpp/ \
-I /opt/ros/humble/include/rcl/ \
-I /opt/ros/humble/include/rcutils/ \
-I /opt/ros/humble/include/rmw \
-I /opt/ros/humble/include/rcl_yaml_param_parser/ \
-I /opt/ros/humble/include/rosidl_runtime_c \
-I /opt/ros/humble/include/rosidl_typesupport_interface \
-I /opt/ros/humble/include/rcpputils \
-I /opt/ros/humble/include/builtin_interfaces \
-I /opt/ros/humble/include/rosidl_runtime_cpp \
-I /opt/ros/humble/include/tracetools \
-I /opt/ros/humble/include/rcl_interfaces \
-I /opt/ros/humble/include/libstatistics_collector \
-I /opt/ros/humble/include/statistics_msgs \
-L /opt/ros/humble/lib/ \
-lrclcpp -lrcutils -o first_node


-rw-r--r-- 1 root root   700 Jun  7 10:00 comp.sh
-rwxr-xr-x 1 root root 71168 Jun  7 10:00 first_node
-rw-r--r-- 1 root root   174 Jun  7 09:47 first_ros2_node.cpp
root@ubuntu-16:~/stephen# ./first_node
^C[INFO] [1749290509.685189085] [rclcpp]: signal_handler(signum=2)

root@ubuntu-16:~/stephen# ros2 node list
/first_node

2. make命令编译

sudo apt install make
touch Makefile
build:
	g++ first_ros2_node.cpp \
	-I /opt/ros/humble/include/rclcpp/ \
	-I /opt/ros/humble/include/rcl/ \
	-I /opt/ros/humble/include/rcutils/ \
	-I /opt/ros/humble/include/rmw \
	-I /opt/ros/humble/include/rcl_yaml_param_parser/ \
	-I /opt/ros/humble/include/rosidl_runtime_c \
	-I /opt/ros/humble/include/rosidl_typesupport_interface \
	-I /opt/ros/humble/include/rcpputils \
	-I /opt/ros/humble/include/builtin_interfaces \
	-I /opt/ros/humble/include/rosidl_runtime_cpp \
	-I /opt/ros/humble/include/tracetools \
	-I /opt/ros/humble/include/rcl_interfaces \
	-I /opt/ros/humble/include/libstatistics_collector \
	-I /opt/ros/humble/include/statistics_msgs \
	-L /opt/ros/humble/lib/ \
	-lrclcpp \
	-lrcutils \
	-o first_node
clean:
	rm first_node


make build	
make clean	
	
./first_node
ros2 node list	

4. 使用CMakeLists.txt编译

虽然通过make调用Makefile编译代码非常的方便,但是还是需要我们手写gcc指令来编译,那有没有什么办法可以自动生成Makefile呢?

sudo apt install cmake
touch CMakeLists.txt

cmake_minimum_required(VERSION 3.22)
project(first_node)

include_directories(/opt/ros/humble/include/rclcpp/)
include_directories(/opt/ros/humble/include/rcl/)
include_directories(/opt/ros/humble/include/rcutils/)
include_directories(/opt/ros/humble/include/rcl_yaml_param_parser/)
include_directories(/opt/ros/humble/include/rosidl_runtime_c/)
include_directories(/opt/ros/humble/include/rosidl_typesupport_interface/)
include_directories(/opt/ros/humble/include/rcpputils/)
include_directories(/opt/ros/humble/include/builtin_interfaces/)
include_directories(/opt/ros/humble/include/rmw/)
include_directories(/opt/ros/humble/include/rosidl_runtime_cpp/)
include_directories(/opt/ros/humble/include/tracetools/)
include_directories(/opt/ros/humble/include/rcl_interfaces/)
include_directories(/opt/ros/humble/include/libstatistics_collector/)
include_directories(/opt/ros/humble/include/statistics_msgs/)

link_directories(/opt/ros/humble/lib/)

# add_executable - 生成first_node可执行文件
add_executable(first_node first_ros2_node.cpp)

# target_link_libraries - 为first_node(目标) 添加需要动态链接库,相同于指定g++编译器-l参数
# 代替 -lrclcpp -lrcutils
target_link_libraries(first_node rclcpp rcutils)

mkdir build
cd build
cmake ..
make
build目录下发现first_node

终端日志:

root@ubuntu-16:~/stephen# touch CMakeLists.txt
root@ubuntu-16:~/stephen# vi CMakeLists.txt
root@ubuntu-16:~/stephen# mkdir build
cd build
root@ubuntu-16:~/stephen/build# cmake ..
-- The C compiler identification is GNU 11.4.0
-- The CXX compiler identification is GNU 11.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /root/stephen/build
root@ubuntu-16:~/stephen/build# ls- la
bash: ls-: command not found
root@ubuntu-16:~/stephen/build# ls -la
total 40
drwxr-xr-x 3 root root  4096 Jun  7 10:27 .
drwxr-xr-x 3 root root  4096 Jun  7 10:27 ..
-rw-r--r-- 1 root root 13716 Jun  7 10:27 CMakeCache.txt
drwxr-xr-x 5 root root  4096 Jun  7 10:27 CMakeFiles
-rw-r--r-- 1 root root  5339 Jun  7 10:27 Makefile
-rw-r--r-- 1 root root  1602 Jun  7 10:27 cmake_install.cmake
root@ubuntu-16:~/stephen/build# make
[ 50%] Building CXX object CMakeFiles/first_node.dir/first_ros2_node.cpp.o
[100%] Linking CXX executable first_node
[100%] Built target first_node
root@ubuntu-16:~/stephen/build# ls -la
total 112
drwxr-xr-x 3 root root  4096 Jun  7 10:27 .
drwxr-xr-x 3 root root  4096 Jun  7 10:27 ..
-rw-r--r-- 1 root root 13716 Jun  7 10:27 CMakeCache.txt
drwxr-xr-x 5 root root  4096 Jun  7 10:27 CMakeFiles
-rw-r--r-- 1 root root  5339 Jun  7 10:27 Makefile
-rw-r--r-- 1 root root  1602 Jun  7 10:27 cmake_install.cmake
-rwxr-xr-x 1 root root 71168 Jun  7 10:27 first_node
root@ubuntu-16:~/stephen/build# ./first_node

5. CMake 依赖查找流程

上面我们用g++、make、cmake三种方式来编译ros2的C++节点。用cmake虽然成功了,但是CMakeLists.txt的内容依然非常的臃肿,我们需要将其进一步的简化。

CmakLists.txt改成

cmake_minimum_required(VERSION 3.22)
project(first_node)

find_package(rclcpp REQUIRED)
add_executable(first_node first_ros2_node.cpp)
target_link_libraries(first_node rclcpp::rclcpp)

mkdir build
cd build
cmake ..
make

root@ubuntu-16:~/stephen/build# cmake ..
make
-- Found rclcpp: 16.0.12 (/opt/ros/humble/share/rclcpp/cmake)
-- Found Python3: /usr/bin/python3 (found version "3.10.12") found components: Interpreter
-- Found rosidl_generator_c: 3.1.6 (/opt/ros/humble/share/rosidl_generator_c/cmake)
-- Found rosidl_adapter: 3.1.6 (/opt/ros/humble/share/rosidl_adapter/cmake)
-- Found rosidl_generator_cpp: 3.1.6 (/opt/ros/humble/share/rosidl_generator_cpp/cmake)
-- Using all available rosidl_typesupport_c: rosidl_typesupport_fastrtps_c;rosidl_typesupport_introspection_c
-- Using all available rosidl_typesupport_cpp: rosidl_typesupport_fastrtps_cpp;rosidl_typesupport_introspection_cpp
-- Found rmw_implementation_cmake: 6.1.2 (/opt/ros/humble/share/rmw_implementation_cmake/cmake)
-- Found rmw_fastrtps_cpp: 6.2.7 (/opt/ros/humble/share/rmw_fastrtps_cpp/cmake)
-- Found OpenSSL: /usr/lib/x86_64-linux-gnu/libcrypto.so (found version "3.0.2")
-- Found FastRTPS: /opt/ros/humble/include
-- Using RMW implementation 'rmw_fastrtps_cpp' as default
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: /root/stephen/build
Consolidate compiler generated dependencies of target first_node
[ 50%] Building CXX object CMakeFiles/first_node.dir/first_ros2_node.cpp.o
[100%] Linking CXX executable first_node
[100%] Built target first_node
root@ubuntu-16:~/stephen/build# ls -la
total 148
drwxr-xr-x 6 root root  4096 Jun  7 10:33 .
drwxr-xr-x 3 root root  4096 Jun  7 10:33 ..
-rw-r--r-- 1 root root 28328 Jun  7 10:33 CMakeCache.txt
drwxr-xr-x 7 root root  4096 Jun  7 10:33 CMakeFiles
-rw-r--r-- 1 root root  2258 Jun  7 10:33 CTestConfiguration.ini
-rw-r--r-- 1 root root   104 Jun  7 10:33 CTestCustom.cmake
-rw-r--r-- 1 root root   245 Jun  7 10:33 CTestTestfile.cmake
-rw-r--r-- 1 root root  6612 Jun  7 10:33 Makefile
drwxr-xr-x 3 root root  4096 Jun  7 10:33 ament_cmake_core
drwxr-xr-x 2 root root  4096 Jun  7 10:33 ament_cmake_package_templates
drwxr-xr-x 2 root root  4096 Jun  7 10:33 ament_cmake_uninstall_target
-rw-r--r-- 1 root root  1602 Jun  7 10:27 cmake_install.cmake
-rwxr-xr-x 1 root root 71168 Jun  7 10:33 first_node
root@ubuntu-16:~/stephen/build# ./first_node
^C[INFO] [1749292501.273702659] [rclcpp]: signal_handler(signum=2)

posted @ 2025-06-07 18:14  jack-chen666  阅读(109)  评论(0)    收藏  举报