Ubuntu 系统下使用 VSCode 编写运行 C++ 程序及 Make CMake 编译配置
在 Ubuntu 22.04.5 LTS 系统下,利用 Visual Studio Code 编写 C++ 程序的方法。内容涵盖三种编译方式:直接使用 g++ 编译器编译运行、使用 Make 工具配合 Makefile 脚本编译、以及使用 CMake 工具配合 CMakeLists.txt 构建项目。通过具体示例代码和终端命令演示了从编写源码到生成可执行文件的全过程。

在 Ubuntu 22.04.5 LTS 系统下,利用 Visual Studio Code 编写 C++ 程序的方法。内容涵盖三种编译方式:直接使用 g++ 编译器编译运行、使用 Make 工具配合 Makefile 脚本编译、以及使用 CMake 工具配合 CMakeLists.txt 构建项目。通过具体示例代码和终端命令演示了从编写源码到生成可执行文件的全过程。

编写如下 C++ 代码:
#include <iostream>
int main() {
std::cout << "Hello World !" << std::endl;
return 0;
}
右键点击'资源管理器'空白处,在弹出的菜单栏中,选择'在集成终端中打开'选项。
执行以下命令,使用 g++ 编译器将 C++ 代码文件编译为二进制可执行文件:
g++ ./hello_world.cpp
编译完成后,默认会生成一个 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 工具编译目录下的 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)
编写完 Makefile 构建脚本后,在终端命令行中,执行以下命令,即可编译生成可执行二进制文件 hello_world:
make
执行以下命令,执行生成的可执行二进制文件:
./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$
# 1. 指定 CMake 的最低版本要求(兼容大多数系统,可根据实际环境调整)
cmake_minimum_required(VERSION 3.10)
# 2. 定义项目名称(自定义,仅用于标识项目,不影响编译结果)
project(HelloWorldProject)
# 3. 设置 C++ 标准(匹配代码使用的语法,此处为 C++11,可根据需求改为 14/17 等)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# 强制要求指定的 C++ 标准,否则编译失败
# 4. 添加可执行文件
# 格式:add_executable(可执行文件名称 源码文件路径)
# 此处可执行文件名为 hello_world,源码文件为 hello_world.cpp(若在子目录需写相对路径,如 src/hello_world.cpp)
add_executable(hello_world hello_world.cpp)
在终端窗口中,执行以下命令生成 Makefile 文件,然后执行 make 命令生成最终的执行二进制文件 hello_world,最后执行命令执行编译结果:
cmake .
make
./hello_world
完整执行流程:
hsl@hsl-VirtualBox:~/Project$ 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:/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$

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online
将 Markdown(GFM)转为 HTML 片段,浏览器内 marked 解析;与 HTML转Markdown 互为补充。 在线工具,Markdown转HTML在线工具,online
将 HTML 片段转为 GitHub Flavored Markdown,支持标题、列表、链接、代码块与表格等;浏览器内处理,可链接预填。 在线工具,HTML转Markdown在线工具,online
通过删除不必要的空白来缩小和压缩JSON。 在线工具,JSON 压缩在线工具,online
将JSON字符串修饰为友好的可读格式。 在线工具,JSON美化和格式化在线工具,online