包含另一个目录中的头文件
我有一个主目录 A
和两个子目录 B
和 C
。
目录 B
包含头文件 structurals.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)
将structurals.h
文件包含到main.c
中的正确方法是什么?
I have a main directory A
with two sub directories B
and C
.
Directory B
contains a header file structures.c
:
#ifndef __STRUCTURES_H
#define __STRUCTURES_H
typedef struct __stud_ent__
{
char name[20];
int roll_num;
}stud;
#endif
Directory C
contains main.c
code:
#include<stdio.h>
#include<stdlib.h>
#include <structures.h>
int main()
{
stud *value;
value = malloc(sizeof(stud));
free (value);
printf("working \n");
return 0;
}
But I get an error:
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)
What is the correct way to include the structures.h
file into main.c
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当引用相对于 c 文件的头文件时,您应该使用
#include "path/to/header.h"
形式
#include;
仅用于内部标头或显式添加的目录(在 gcc 中使用-I
选项)。When referencing to header files relative to your c file you should use
#include "path/to/header.h"
The form
#include <someheader.h>
is only used for internal headers or for explicitly added directories (in gcc with the-I
option).写入
然后
进入 c & 目录中编译你的 main.c
write
in place of
then go in directory in c & compile your main.c with
如果您处理 Makefile 项目或只是从命令行运行代码,请使用
gcc -IC main.c
,其中
-I
选项添加您的C
目录添加到要搜索头文件的目录列表中,这样您就可以在项目中的任何位置使用#include "structurals.h"
。If you work on a Makefile project or simply run your code from command line, use
gcc -IC main.c
where
-I
option adds yourC
directory to the list of directories to be searched for header files, so you'll be able to use#include "structures.h"
anywhere in your project.如果您想使用命令行参数,那么您可以给出 gcc -idirafter ../b/ main.c ,
然后您无需在程序中执行任何操作。
If you want to use the command line argument then you can give
gcc -idirafter ../b/ main.c
then you don't have to do any thing inside your program.
用此替换您的代码,并且不要忘记将
parent_directory
替换为您的文件所在文件夹的路径...我不知道您的文件
structurals.h
是否具有.h
扩展名或.c
,所以请注意...Replace your code with this and don't forget to replace
parent_directory
with the path of the folder in which your file is...I don't know whether your file
structures.h
has.h
extension or.c
so please see to it...