新建3个源码文件,分别是bill.c、bill.h、test.c。
这里我们把3个文件的内容分别贴出来。
bill.c
#include <stdio.h>
void bill(char *arg)
{
printf("bill: you passed %s\n", arg);
}
bill.h
#ifndef BILL_H
#define BILL_H
void bill(char *);
#endif
test.c
#include <stdio.h>
#include "bill.h"
int main()
{
bill("Hello World");
return 0;
}
打开目录的.vscode文件夹下的tasks.json文件,找到红框里面的内容

方法1:
修改红方框中的{file}为{fileDirname}\\*.c这样就可以编译当前文件夹下的所有c文件,实现多文件编译了。说明:如果是c++代码,修改为{fileDirname}\\*.cpp
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe 生成活动文件",
"command": "D:\\mingw64\\bin\\gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${fileDirname}\\*.c",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
方法2:
打开tasks.json,找到"args"属性,在其中"-g"后,"-o"前加入"${fileDirname}\YourcFileName.c",每组字符串以逗号分隔

运行结果:

虽然很多人在初学编程的时候使用的编译器是VS,但也相信有不少小伙伴在代码学习的过程中使用的编译环境是VScode。多文件编译可以实现三子棋,扫雷等小游戏的运行,希望可以帮助到你。
博客介绍了在VSCode中实现C/C++多文件编译的方法。新建bill.c、bill.h、test.c三个源码文件,可通过两种方式实现多文件编译。方法一是修改tasks.json文件中的{file};方法二是在tasks.json的“args”属性中添加特定内容。多文件编译可运行三子棋、扫雷等小游戏。
4294

被折叠的 条评论
为什么被折叠?



