我有一个主目录 A
和两个子目录 B
和 C
。
目录B
包含头文件structures.c
:
#ifndef __STRUCTURES_H
#define __STRUCTURES_H
typedef struct __stud_ent__
{
char name[20];
int roll_num;
}stud;
#endif
目录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;
}
但是我得到一个错误:
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)
将structures.h
文件包含到main.c
中的正确方法是什么?
最佳答案
当引用头文件时相对于你的c文件你应该使用#include "path/to/header.h"
表格#include <someheader.h>
仅用于内部 header 或明确添加的目录(在带有 -I
选项的 gcc 中)。
关于c - 包含来自另一个目录的头文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7581408/