VSCode使用Makefile Tools插件开发C/C++程序

提起Makefile,可能有人会觉得它已经过时了,毕竟现在有比它更好的工具,比如CMake,XMake,Meson等等,但是在Linux下很多C/C++源码都是直接或者间接使用Makefile文件来编译项目的,可以说Makefile是基石。

另外,针对C++ 20的一些特性,像比较流行的CMake工具,目前支持还不完善,如果想要尝鲜C++ 20的一些新特性,比如Module,目前最好是使用Makefile比较方便。后面笔者会专门介绍C++ 20的Module使用。

微软官方出了一个VSCode的Makefile Tools插件,用于编译、调试、运行C/C++程序。

笔者以一个简单的示例来介绍它。

如果是Windows,则需要安装MSYS2 MinGW,然后在Windows下配置PATH环境变量,包含MinGW64的gcc以及MSYS2的make路径,比如:
G:\msys64\mingw64\binG:\msys64\usr\bin

在工作区编写一个C++文件, main.cc

#include <stdio.h>

int main(int argc, char*argv[]) {
	printf("测试\n");
	return 0;
}

再编写一个Makefile文件:

.PHONY: all clean

CC := gcc
CXX := g++
CFLAGS :=
CXXFLAGS := -gdwarf-4
Target := main

SRCS += $(wildcard *.cc)
OBJS := $(patsubst %.cc, %.o, ${SRCS})

all: $(Target)

$(Target): $(OBJS)
	$(CXX) $(CXXFLAGS) $(OBJS) -o $@

%.o: %.cc
	$(CXX) $(CXXFLAGS) -c $? -o $@

clean:
	rm -rf $(OBJS) $(Target)

在工作区编写的了Makefile后,VSCode输出窗口Makefile Tools会有一系列日志:

No current configuration is defined in the workspace state. Assuming 'Default'.
No target defined in the workspace state. Assuming 'Default'.
Dropping various extension output files at c:\Users\admin\AppData\Roaming\Code\User\workspaceStorage\9d677e46e192d47afa37529f2e89bbe9\ms-vscode.makefile-tools
Logging level: Normal
Configurations cached at c:\Users\admin\AppData\Roaming\Code\User\workspaceStorage\9d677e46e192d47afa37529f2e89bbe9\ms-vscode.makefile-tools\configurationCache.log
No path to the makefile is defined in the settings file.
No folder path to the makefile is defined in the settings file.
Always pre-configure: false
Always post-configure: false
Dry-run switches: '--always-make', '--keep-going', '--print-directory'
No current launch configuration is set in the workspace state.
Default launch configuration: MIMode = undefined,
                    miDebuggerPath = undefined,
                    stopAtEntry = undefined,
                    symbolSearchPath = undefined
Configure on open: true
Configure on edit: true
Configure after command: true
Only .PHONY targets: false
Save before build or configure: true
Build before launch: true
Clear output before build: true
Ignore directory commands: true
compile_commands.json path: null
Deduced command 'make.exe ' for configuration "Default"
The Makefile Tools extension process of configuring your project is about to run 'make --dry-run' in order to parse the output for useful information. This is needed to calculate accurate IntelliSense and targets information. Although in general 'make --dry-run' only lists (without executing) the operations 'make' would do in the current context, it is still possible some code to be executed, like $(shell) syntax in the makefile or recursive invocations of the $(MAKE) variable.
If you don't feel comfortable allowing this configure process and 'make --dry-run' to be invoked by the extension, you can chose a recent full, clean, verbose and up-to-date build log as an alternative, via the setting 'makefile.buildLog'. 

并且会弹出对话框:

在这里插入图片描述

点“确定”即可。

此时Makefile面板如下:
在这里插入图片描述

我们需要设置生成目标启动目标,在其后点笔那个的图标按钮,则会弹出所有目标,生成目标如下,选择all

在这里插入图片描述

启动目标如下,只有一个,选择它:
在这里插入图片描述
设置好后,如下图:
在这里插入图片描述
右上边有三个按钮,分别是编译调试运行
需要注意的是编译是使用的VSCode的shell类型任务执行的,如果如解决Windows下VSCode控制台乱码问题中所述,添加了VSCode的控制台启动参数,则可能会失败,需要去掉参数再执行,结果如下:

在这里插入图片描述
调试按钮,VSCode会使用cppdbg引擎启动gdb进行调试,UTF8中文可以正常显示。
在这里插入图片描述

<think>好的,用户想知道在Linux系统下使用VSCode结合CMake管理C/C++工程时,是否可以不用JSON配置文件。首先,我需要确认VSCode和CMake的基本工作流程。 CMake本身是通过CMakeLists.txt文件来管理项目的构建过程,生成对应的Makefile或其他构建系统的文件。而VSCode通常通过插件如CMake Tools来集成CMake,这时候插件可能会生成或依赖一些JSON配置文件,比如cmake-kits.json、settings.json等。不过,用户的问题是不使用JSON是否可以。 接下来,我需要考虑用户可能的情况。他们可能希望尽量减少配置文件的数量,或者对JSON配置不熟悉,想用纯CMake管理。这时候需要明确,虽然VSCode插件可能会自动生成一些JSON文件,但用户不一定需要手动编辑这些文件。核心的构建配置仍然由CMakeLists.txt处理。 然后,应该解释即使不手动配置JSON,VSCode的CMake Tools插件可能还是会自动生成必要的配置。用户如果不希望接触这些JSON文件,可以忽略它们,专注于CMakeLists.txt。但如果有特殊需求,比如指定编译器或构建类型,可能还是需要调整JSON设置,不过这不是强制性的。 另外,用户可能有更深层的需求,比如简化项目结构,或者避免维护多个配置文件。需要说明CMake本身足够强大,可以处理大多数构建需求,而VSCode的集成主要是为了便捷,不是必须依赖JSON的。 最后,需要给出步骤指导,如何在不手动配置JSON的情况下使用VSCode和CMake,以及可能需要的注意事项,比如插件自动生成的配置是否会影响项目,如何通过CMakeLists.txt覆盖这些设置等。确保用户了解核心在于CMake,而JSON只是辅助,可以按需选择是否使用。</think>在Linux系统下使用VSCode配合CMake管理C/C++工程时,完全可以不使用JSON配置文件。以下是具体说明: --- ### **一、基本原理** 1. **CMake核心地位** - CMake通过`CMakeLists.txt`文件实现工程管理 - 自动生成构建系统文件(如Makefile) - 不依赖任何JSON文件即可完成完整构建流程 2. **VSCode的辅助角色** - 通过CMake Tools插件提供可视化操作 - 自动生成的`.vscode/`目录下的JSON文件(如`settings.json`)仅用于IDE配置 - 这些JSON文件不是CMake工程管理的必要条件 --- ### **二、不依赖JSON的实践方案** #### **步骤1:基础工程结构** ```bash my_project/ ├── CMakeLists.txt ├── src/ │ └── main.cpp └── build/ # 构建目录(可自动创建) ``` #### **步骤2:CMake配置示例** ```cmake # CMakeLists.txt cmake_minimum_required(VERSION 3.10) project(MyProject) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) add_executable(${PROJECT_NAME} src/main.cpp ) ``` #### **步骤3:VSCode操作流程** 1. 安装必要插件: - CMake Tools(核心) - C/C++(IntelliSense支持) 2. 快捷键操作: - `Ctrl+Shift+P` → `CMake: Configure` 配置项目 - `Ctrl+Shift+P` → `CMake: Build` 执行编译 - `F5` 直接启动调试 --- ### **三、JSON文件的作用(可选项)** 虽然不强制使用,但了解其用途有助于高级配置: 1. `cmake-kits.json`:编译器套件配置 ```json { "name": "GCC 11", "compilers": { "C": "/usr/bin/gcc", "CXX": "/usr/bin/g++" } } ``` 2. `settings.json`:工作区设置 ```json { "cmake.buildDirectory": "${workspaceFolder}/build", "cmake.configureArgs": ["-DCMAKE_BUILD_TYPE=Debug"] } ``` --- ### **四、优势对比** | 方式 | 优点 | 缺点 | |--------------|-----------------------------|------------------------| | 纯CMake | 跨平台统一配置,无IDE依赖 | 需要记忆CMake命令 | | CMake+JSON | 可视化操作方便,可保存个性化设置 | 增加配置文件维护成本 | --- ### **五、典型场景建议** 1. **简单项目**:完全依赖`CMakeLists.txt`即可 2. **团队协作**:建议将`.vscode/`加入`.gitignore` 3. **特殊需求**:需要交叉编译或自定义工具链时,可选择性使用JSON配置 > 📌 关键结论:CMake本身不依赖JSON文件,VSCode的JSON配置仅用于增强开发体验而非必需。建议优先通过`CMakeLists.txt`实现工程管理,仅在需要IDE特殊配置时使用JSON。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值