环境依赖 |:—–:|:—-:| |操作系统|Windows| |编译器|MingGW工具集| |编译工具(Build System)|Cmake| |语言|c/c++| |文本编辑器|VsCode|
如何在VsCode里配置好Cmake实现一键编译链接多文件工程
配置过程学习自:手把手教会VSCode的C++环境搭建,多文件编译,Cmake,json调试配置( Windows篇) - bilibili
当你不懂一个工具的使用方法,而只能在搜索引擎找到零碎且不靠谱的教程,阅读它的官方操作手册和说明文档会很有用!
这个文档手把手教你为VS配置GCC来完成c/c++文件的编译,同时了解tasks.json的一些属性的介绍Using GCC with MinGW - Visual Studio Code Doc
在阅读前推荐为你的vscode安装下载以下插件
为CMakeLists.txt文件的攥写提供代码补全
微软提供的功能扩展,一键生成模板等
在这个简单的多文件编译链接的例子里,看看如何为VsCode的运行按钮继承基于cmake的多文件编译和链接功能
该工程共包含三个文件 main.c fun.c fun.h,放置在同一路径下
//main.c
#include <stdio.h>
#include "fun.h"
int main(int argc,char *argv[])
{
int a = 20;
int b = 30;
swap(&a,&b);
printf("a=%d\n",a);
printf("b=%d\n",b);
return 0;
}
//fun.c
#include "fun.h"
void swap(int* a,int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
//fun.h
void swap(int* a,int* b);
先配置一个最基本最简单的CMakeLists.txt文件
在VsCode里新建CMakeLists.txt,和源文件放在同一路径下,并编辑以下内容
project(myPro)
add_executable(myPro main.c fun.c)
project 为该工程指定一个名字
add_executable 的第一个参数将作为最后生成的可执行文件的文件名,后面列出所有依赖c源文件
配置好后在你的工程目录下打开你的命令终端
你可以直接在VsCode的当前窗口上方菜单栏选择”终端(Terminal)(T)”-“新终端从当前工程文件夹路径打开一个终端
输入指令:
cmake .
第一个参数”.”代表当前路径,这个指令将会读取当前路径的CMakeLists.txt并据此生成makefile
输入下一条指令:
mingw32-make
//或者
cmake --build .
这个指令调用的可执行文件是你的mingw64/bin/mingw32-make.exe
可以依据生成的makefile对工程下的源文件编译、链接成最后的可执行文件myPro.exe
最后在终端执行可执行文件
./MyPro.exe
以上步骤通过命令行调用cmake为一个简单工程找出文件间的依赖关系并生成出可执行文件
下面我们希望通过绑定VsCode的调试功能,F5一键完成上面的全部步骤
我们需要配置launch.json和tasks.json
以下的launch.json和tasks.json可以由VsCode的C/C++插件自动生成模板
–在VsCode窗口键入ctrl+shift+P打开菜单栏搜索C/C++查找
也可以自行在工程根目录创建.vscode文件夹,并在里面自行编辑launch.json和tasks.json
//launch.json
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "gcc.exe - build active file",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}\\MyPro.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:\\Coding\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "Build"
}
]
}
几个宏的含义:
${workspaceFolder} 你的Vscode打开的文件夹的完整路径
${fileDirname} 你当前VsCode打开的文件所在文件夹的完整路径
更多宏可以参考: VsCode Docs
几个重要属性(property)值得关注:
“program”:执行任务的程序的完整路径,在这里是c源文件编译链接后生成的可执行文件,文件名应该和你的CMakeLists.txt中命名的可执行文件名一致
“args”:传递给可执行文件的命令行参数,如果你的主函数main(int argc,char argv[])需要提供参数在中括号内提供即可
“cwd”:调试器的工作目录,定义了你的C程序里的’当前路径’(./)
“miDebuggerPath”: Debug断点调试器,你需要把这里的路径指向你本地中的mingw64/bin/gdb.exe
“stopAtEntry”: 如果设置为true,断点调试程序将会在程序进入main()函数的时候停止并进入debug模式
“preLaunchTask”:launch前预先要执行的任务,这里的内容与tasks.json中”labels”属性的字符串保持一致,tasks.json文件的属性配置会在下文说明
其他的参数我暂时不太清楚,在这个工程里保持默认可以正常运作
启动按钮会从tasks.json读取来确认如何构建和运行你的程序 (the play button will read from tasks.json to figure out how to build and run your program.)
//tasks.json { "version": "2.0.0", "options": { "cwd": "${workspaceFolder}" }, "tasks": [ { "type": "shell", "label": "build makefile", "command": "cmake", "args": [ "." ] }, { "label": "build executable file", "command":"mingw32-make", "args": [], }, { "label": "Build", "dependsOrder": "sequence", "dependsOn":[ "build makefile", "build executable file" ] } ] }
单个任务将command和args传递给shell解释器(cmd/powershell) 前两个任务实质就是编写了VsCode可识别的自动化脚本完成了在命令行终端输入
cmake .
mingw32-make
在指定路径生成了makefile和链接好的可执行文件
一些值得注意的属性
“label”: 标记唯一任务的任务名,随意取,但要保持和后面配置的一致
“command”:指定要执行的命令(要运行的Shell程序,在各个工具的bin文件夹里的可执行文件)
“args”: 指定传递给命令的命令行参数
(The args array specifies the command-line arguments that will be passed to command)
“cwd”: 生成可执行文件的路径,可以自行修改
第三个任务负责完成前两个任务的复合
“dependOn”列表中写入前两个任务的label,要求该tasks的执行依赖于前两个任务的执行
“dependsOrder”:”sequence”指定依赖的执行顺序按照列表列出的顺序
现在注意先前编辑的launch.json
“preLaunchTask”属性,表示执行程序前要求预先完成的任务
确保该属性的值为第三个任务的label,则VsCode会在执行可执行程序前检查tasks.json并在命令行调取cmake完成源文件的重新编译链接
完成所有配置后,尝试切回主函数所在的c文件,单击F5或菜单栏运行(Run)(R)-启动调试
到此,你通过对VsCode的个性化配置得到了一个轻量级的c/c++ IDE,为自己鼓掌
附录:
I
VS Code supports variable substitution inside strings in launch.json and has the following predefined variables:
${userHome} - the path of the user’s home folder
${workspaceFolder} - the path of the folder opened in VS Code
${workspaceFolderBasename} - the name of the folder opened in VS Code without any slashes (/)
${file} - the current opened file
${fileWorkspaceFolder} - the current opened file’s workspace folder
${relativeFile} - the current opened file relative to workspaceFolder
${relativeFileDirname} - the current opened file’s dirname relative to workspaceFolder
${fileBasename} - the current opened file’s basename
${fileBasenameNoExtension} - the current opened file’s basename with no file extension
${fileDirname} - the current opened file’s dirname
${fileExtname} - the current opened file’s extension
${cwd} - the task runner’s current working directory upon the startup of VS Code
${lineNumber} - the current selected line number in the active file
${selectedText} - the current selected text in the active file
${execPath} - the path to the running VS Code executable
${defaultBuildTask} - the name of the default build task
${pathSeparator} - the character used by the operating system to separate components in file paths
–Update 2022.8.6
链接:VsCode Docs
II
The task’s properties have the following semantic:
label: The task’s label used in the user interface.
type: The task’s type. For a custom task, this can either be or . If is specified, the command is interpreted as a shell command (for example: bash, cmd, or PowerShell). If is specified, the command is interpreted as a process to execute.shellprocessshellprocess
command: The actual command to execute.
windows: Any Windows specific properties. Will be used instead of the default properties when the command is executed on the Windows operating system.
group: Defines to which group the task belongs. In the example, it belongs to the group. Tasks that belong to the test group can be executed by running Run Test Task from the Command Palette.test
presentation: Defines how the task output is handled in the user interface. In this example, the Integrated Terminal showing the output is revealed and a terminal is created on every task run.alwaysnew
options: Override the defaults for (current working directory), (environment variables), or (default shell). Options can be set per task but also globally or per platform. Environment variables configured here can only be referenced from within your task script or process and will not be resolved if they are part of your args, command, or other task attributes.cwdenvshell
runOptions: Defines when and how a task is run.
链接:VsCode Docs
specifie 指定 active file 活动文件 argument 参数 propety 属性