Man*_*nny 40 c compiler-errors include c-preprocessor
我有一个主目录,A有两个子目录B和C.
目录B包含头文件structures.c:
#ifndef __STRUCTURES_H
#define __STRUCTURES_H
typedef struct __stud_ent__
{
char name[20];
int roll_num;
}stud;
#endif
Run Code Online (Sandbox Code Playgroud)
目录C包含main.c代码:
#include<stdio.h>
#include<stdlib.h>
#include <structures.h>
int main()
{
stud *value;
value = malloc(sizeof(stud));
free (value);
printf("working \n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是我收到一个错误:
main.c:3:24: error: structures.h: No such file or directory
main.c: In function ‘main’:
main.c:6: error: ‘stud’ undeclared (first use in this function)
main.c:6: error: (Each undeclared identifier is reported only once
main.c:6: error: for each function it appears in.)
main.c:6: error: ‘value’ undeclared (first use in this function)
Run Code Online (Sandbox Code Playgroud)
将structures.h文件包含在内的正确方法是什么main.c?
Con*_*ius 40
当引用相对于您的c文件的头文件时,您应该使用#include "path/to/header.h"
该表单#include <someheader.h>仅用于内部标头或显式添加的目录(在带有-I选项的gcc中).
Jee*_*tel 16
写
#include "../b/structure.h"
Run Code Online (Sandbox Code Playgroud)
代替
#include <structures.h>
Run Code Online (Sandbox Code Playgroud)
然后进入c目录并编译你的main.c
gcc main.c
Run Code Online (Sandbox Code Playgroud)
如果您处理 Makefile 项目或只是从命令行运行代码,请使用
gcc -IC main.c
where-I选项将您的C目录添加到要搜索头文件的目录列表中,这样您就可以#include "structures.h"在项目的任何地方使用。