【发布时间】:2021-07-27 14:43:26
【问题描述】:
我是 VS Code 和 C 的血腥初学者(实际上我一无所知)。但我有一个目标。我希望 - 有一天 - 能够从 IAU SOFA 编译 C 文件。但在我必须学习如何编译之前。为此,我将 Mac clang 编译器安装到 VS Code 中(我现在也可以将其用于 Python)。我创建了四个简单的文件:hellocopy.c、hello.c、myheader.h 和 tasks.json。我告诉 VS Code 用 shift 命令 B 编译。
我得到了错误:
开始构建... /usr/bin/clang -g /Users/widmerhans/projects/Eigene_C_Programme/*.c /Users/widmerhans/projects/Eigene_C_Programme/Myheader/*.h -o /Users/widmerhans/projects/Eigene_C_Programme/hellocopy clang:错误: 生成多个输出文件时不能指定-o 构建完成 有错误。终端进程启动失败(退出代码:-1)。
hellocopy.c:
#include <stdio.h>
#include "myheader.h"
int main(){
int age;
printf("Enter age\n");
scanf("%d", &age);
printf("age is %d", age);
hello();
return 0;
}
hello.c:
#include <stdio.h>
#include "myheader.h"
void hallo() {
printf("Hallo World\n");
}
myheader.h:
#ifndef MYHEADER_H
#define MYHEADER_H
#define PI (3.1416)
// extern int meineVariable;
// extern int meineFunktion1(int);
// extern int meineFunktion2(char);
extern void hello();
#endif /* MYHEADER_H */
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang build C files",
"command": "/usr/bin/clang",
"args": [
"-g",
"${fileDirname}/*.c",
"${fileDirname}/Myheader/*.h",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
感谢您的帮助。 最好的祝福 汉斯
【问题讨论】:
-
编辑帖子时,您可以使用
{}图标将某些部分格式化为代码示例。 -
关于 VS Code 中的多文件编译,我的建议是毕业到构建系统。 Makefile 支持现在是原生的,还有 CMake 或 meson 或其他。然后,您可以依靠构建系统的扩展来完成繁重的工作。
标签: c visual-studio-code