【ROS 2】Ubuntu 22.04.5 LTS 系统 Visual Studio Code 开发工具配置 ② ( 编写运行 C++ 程序 | make、cmake 工具编译代码 )
文章目录
在上一篇 博客 【ROS 2】Ubuntu 22.04.5 LTS 系统 Visual Studio Code 开发工具配置 ① ( 开发工具配置 | 编写运行 Python 程序 | python3 命令运行 ) 中 , 编写 Python 代码 , 但是执行时 直接在 命令行 中执行该 Python 脚本 ;
Visual Studio Code 开发工具 在其中只扮演 编辑器 作用 , 编译运行操作 , 并不在 VSCode 中进行 ;
本篇博客中 , 编写 C++ 程序 , 然后再 命令行中 使用 g++ 编译器进行编译 , 在命令行中运行编译后的 a.out 可执行程序 ;
一、Visual Studio Code 开发工具编写运行 C++ 程序
1、编写 C++ 代码
编写如下 C++ 代码 :
#include"iostream"intmain(){ std::cout <<"Hello World !"<< std::endl;return0;}2、打开 VSCode 集成终端
右键点击 " 资源管理器 " 空白处 , 在弹出的 菜单栏 中 , 选择 " 在集成终端中打开 " 选项 ,

3、编译 C++ 代码
执行
g++./hello_world.cpp 命令 , 编译上述 C++ 代码文件 , 使用 g++ 编译器 将 C++ 代码编译为 二进制可执行文件 ;
4、执行编译结果
编译完成后 , 默认会生成一个 a.out 二进制可执行文件 , 然后执行
./a.out 命令 , 执行上述可执行文件 ;
完整执行结果如下所示 :
hsl@hsl-VirtualBox:~/Project$ g++ ./hello_world.cpp hsl@hsl-VirtualBox:~/Project$ ./a.out Hello World ! hsl@hsl-VirtualBox:~/Project$ 
二、使用 make 工具编译 C++ 代码
1、编写 Makefile 编译脚本
使用 make 工具 编译 目录下的 hello_world.cpp 源码文件 , 需要编写如下 Makefile 编译脚本 :
# 定义编译器:指定g++作为C++编译器 CXX= g++ # 定义编译选项:启用C++11标准,消除冗余警告 CXXFLAGS=-std=c++11-Wall # 定义目标可执行文件名称 TARGET= hello_world # 默认目标(执行make时优先执行) all:$(TARGET) # 生成可执行文件的规则:依赖hello_world.cpp,生成$(TARGET)$(TARGET): hello_world.cpp $(CXX)$(CXXFLAGS)-o $(TARGET) hello_world.cpp # 清理编译产物的规则 clean: rm -f $(TARGET)2、编译并执行
编写完 Makefile 构建脚本后 , 在 终端 命令行中 , 执行
make 命令 , 即可编译生成 可执行二进制文件 hello_world ;
执行
./hello_world 命令 , 执行 生成的 可执行二进制文件 ;
完整执行过程 :
hsl@hsl-VirtualBox:~/Project$ make g++-std=c++11-Wall -o hello_world hello_world.cpp hsl@hsl-VirtualBox:~/Project$ ./hello_world Hello World ! hsl@hsl-VirtualBox:~/Project$ 
三、使用 cmake 工具编译 C++ 代码
1、编写 CMakeLists.txt 构建脚本
# 1. 指定CMake的最低版本要求(兼容大多数系统,可根据实际环境调整) cmake_minimum_required(VERSION3.10) # 2. 定义项目名称(自定义,仅用于标识项目,不影响编译结果) project(HelloWorldProject) # 3. 设置C++标准(匹配代码使用的语法,此处为C++11,可根据需求改为14/17等) set(CMAKE_CXX_STANDARD11)set(CMAKE_CXX_STANDARD_REQUIREDON) # 强制要求指定的C++标准,否则编译失败 # 4. 添加可执行文件 # 格式:add_executable(可执行文件名称 源码文件路径) # 此处可执行文件名为hello_world,源码文件为hello_world.cpp(若在子目录需写相对路径,如src/hello_world.cpp) add_executable(hello_world hello_world.cpp)2、编译并执行
在 终端 窗口 中 , 执行
cmake .命令 , 生成 Makefile 文件 , 然后 执行
make 命令 , 生成 最终的 可执行二进制文件 hello_world , 最后执行
./hello_world 命令 , 执行编译结果 ;
完整执行流程 :
hsl@hsl-VirtualBox:~/Project$ cmake .-- The C compiler identification is GNU11.4.0-- The CXX compiler identification is GNU11.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:/home/hsl/Project hsl@hsl-VirtualBox:~/Project$ make [50%] Building CXX object CMakeFiles/hello_world.dir/hello_world.cpp.o [100%] Linking CXX executable hello_world [100%] Built target hello_world hsl@hsl-VirtualBox:~/Project$ ./hello_world Hello World ! hsl@hsl-VirtualBox:~/Project$ 