当我尝试运行具有多个模块的 C 项目时,它默认只编译选定的模块。我尝试编辑tasks.json来编译文件夹中的每个C和头文件,但它只是将星号作为星号传递到我的命令行。
我编辑了tasks.json并尝试使用以下输出构建项目:
Starting build...
/usr/bin/gcc -f diagnostics-color=always -g /home/user/VSCodeProjects/multimodule/main.c /home/user/VSCodeProjects/multimodule/*.c /home/user/VSCodeProjects/multimodule/*.h -o /home/user/VSCodeProjects/multimodule/main
/usr/bin/ld: /tmp/ccgdRy3t.o: in function `main':
/home/user/VSCodeProjects/multimodule/main.c:4: multiple definition of `main'; /tmp/ccW8LBPU.o:/home/user/VSCodeProjects/multimodule/main.c:4: first defined here
collect2: Fehler: ld gab 1 als Ende-Status zurück
Build finished with error(s).
正如此处所示,它在运行 gcc 命令时仅使用星号,而不是寻址文件夹中的所有 .c 和 .h 文件。
我已经搜索过这个问题并发现了一些问题,例如这issue https://github.com/Microsoft/vscode/issues/16865在 VSCode GitHub 页面上,但似乎没有一个解决方案有效。我发现的大多数答案并不适用于此,因为我已经在使用 2.0.0 版本。
main.c的内容:
#include "test.h"
int main(void)
{
printTest();
return 0;
}
test.c的内容:
#include "test.h"
#include<stdio.h>
void printTest(){
printf("Test");
}
test.h的内容:
#if !defined(TEST_H)
#define TEST_H
void printTest();
#endif // TEST_H
tasks.json 的内容:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"${fileDirname}/*.c",
"${fileDirname}/*.h",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
]
}