4.1 C/C++开发环境:VSCode+CMake+VS2019

使用VSCode中的CMake插件,配置VS编译器并生成构建系统。

一、软件安装

1.1 软件下载

VS和VSCode下载Visual Studio 2022 + Visual Studio Code

CMake下载cmake-4.0.2-windows-x86_64.msi

1.2 安装

(1)cmake安装

默认是勾选将cmake路径添加到环境变量中的,如果没有则要手动添加到PATH环境变量下。

(2)VS安装

注意:选好要下载的部分:

  • 使用C++的桌面开发
  • Visual Studio扩展开发

(3)vscode和cmake插件安装

二、配置使用

2.1 准备文件

创建如下的目录和文件:

# tree TestDemon/
TestDemon/
├── CMakeLists.txt    # cmake配置文件
└── hello.c           # 源码

CMakeLists.txt文件内容如下:

cmake_minimum_required(VERSION 3.10)
project(hello VERSION 1.0)

set(CXX_STANDARD 11)
set(CXX_STANDARD_REQUIRED True)

add_executable(hello hello.c)

hello.c文件内容如下:

#include <stdio.h>
int main()
{
    printf("hello ffmpeg \r\n");
    return 0;
}

2.2 配置VS编译器 并生成构建系统

选择VS编译器:
在这里插入图片描述
在这里插入图片描述
这个操作背后 是执行的cmake命令生成构建系统:

cmake -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE --no-warn-unused-cli -S./ -B./build -G "Visual Studio 16 2019" -T host=x64 -A x64
  • -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE 启用生成compile_commands.json文件,这个文件被许多代码分析工具(如clangd)使用:BOOL=TRUE显式指定这是一个布尔值并设为TRUE(注意:VS+CMake是不会生成compile_commands.json文件,即使设置了DCMAKE_EXPORT_COMPILE_COMMANDS也不会生成)
  • --no-warn-unused-cli 禁止显示未使用的命令行参数的警告,当你在命令行传递一些CMakeLists.txt中没有使用变量时不会警告
  • -S./ 指定源目录(项目根目录,即包含CMakeLists.txt的地方)./ 表示当前目录
  • -B./build 指定构建目录(生成构建系统文件将放在这里)
  • -G "Visual Studio 15 2017" 指定生成器(Generator)
    • Visual Studio 15 2017 是vs2017的CMake标识符
    • Visual Studio 16 2019
    • Visual Studio 17 2022
  • -T host=x64 指定工具集(Toolset)表示使用64位版本构建工具
  • -A x64 指定平台(Architecture) x64 表示生成64位目标平台

2.3 编译

# 使用原来生成的配置
cd build
cmake --build . --config Debug
cmake --build . --config Release

# 重新生成配置(要先删除build目录)
mkdir build && cd build
cmake ..  # 生成配置
cmake --build . --config Debug
cmake --build . --config Release
VSCode中设置CMake的编译器可以通过以下步骤完成: 1. **安装必要的扩展**: - 打开VSCode,点击左侧的扩展图标(或按 `Ctrl+Shift+X`)。 - 搜索并安装“C/C++CMake Tools”扩展。 2. **配置CMake**: - 打开你的CMake项目文件夹。 - 按 `Ctrl+Shift+P` 打开命令面板,输入并选择 `CMake: Configure`。 - 选择你要使用的编译器(如GCC、Clang等)。 3. **修改 `settings.json`**: - 打开设置文件:点击左下角的齿轮图标,选择“设置”,然后点击右上角的“打开设置(JSON)”图标。 - 添加以下配置,根据你的需求修改编译器路径CMake路径: ```json { "cmake.cmakePath": "路径到你的CMake可执行文件", "cmake.configureSettings": { "CMAKE_C_COMPILER": "路径到你的C编译器", "CMAKE_CXX_COMPILER": "路径到你的C++编译器" }, "C_Cpp.default.compilerPath": "路径到你的C/C++编译器" } ``` 4. **配置任务**: - 按 `Ctrl+Shift+P` 打开命令面板,输入并选择 `Tasks: Configure Task`。 - 选择“Create tasks.json file from template”,然后选择“Others”。 - 在 `tasks.json` 中添加以下配置: ```json { "version": "2.0.0", "tasks": [ { "label": "CMake Configure", "type": "shell", "command": "cmake", "args": [ "-B", "build", "-G", "Unix Makefiles" ], "group": { "kind": "build", "isDefault": true }, "presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "shared" }, "problemMatcher": [] }, { "label": "CMake Build", "type": "shell", "command": "cmake", "args": [ "--build", "build" ], "group": { "kind": "build", "isDefault": true }, "presentation": { "echo": true, "reveal": "always", "focus": false, "panel": "shared" }, "problemMatcher": [] } ] } ``` 5. **编译项目**: - 按 `Ctrl+Shift+B` 打开构建任务,选择“CMake Build”进行编译。 通过以上步骤,你就可以在VSCode中设置并使用CMake的编译器了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值