文件结构:
project_root
|-- inc
| |-- header.h
|-- src
| |-- helpers.c
| |-- main.c
header.h
#ifndef HEADER_H
# define HEADER_H
void func(void);
#endif
helpers.c
void func()
{
/* do something */
}
main.c
#include "header.h"
int main(void)
{
func();
return (0);
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/inc",
],
"defines": [],
"macFrameworkPath": [
"/System/Library/Frameworks",
"/Library/Frameworks"
],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
tasks.json
"tasks": [
{
"type": "shell",
"label": "gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"-g",
"-Wall",
"-Werror",
"-Wextra",
"-o0"
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
],
"options": {
"cwd": "${workspaceFolder}"
},
"group": {
"kind": "build",
"isDefault": true
},
}
],
"version": "2.0.0"
}
当我在VSCode中构建程序时,出现以下错误。project_root/src/main.c:xx:xx: fatal error: 'header.h' file not found
如何避免此错误?(如何让VSCode的构建功能知道我的标头在哪里?)
我在c_cpp_properties.json
中配置了我的包含路径,因此我没有在main.c
中包含我的标头的花样。
我不想在#include "../inc/header.h"
中写main.c
,所以这对我来说不是解决方案。
让我们说这是我的项目。文件结构:project_root |-inc | |-header.h |-src | |-helpers.c | |-main.c header.h #ifndef HEADER_H#定义HEADER_H void func(void); #...
tasks.json
标志在args
属性下的-I
中指定包含路径。