包含来自另一个目录的头文件 [英] Including a header file from another directory

查看:28
本文介绍了包含来自另一个目录的头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主目录 A 和两个子目录 BC.

目录B包含一个头文件structures.c:

#ifndef __STRUCTURES_H#define __STRUCTURES_Htypedef 结构 __stud_ent__{字符名称[20];int roll_num;}螺柱;#万一

目录C包含main.c代码:

#include#include#include int main(){螺柱*值;值 = malloc(sizeof(stud));免费(价值);printf("工作
");返回0;}

但我收到一个错误:

main.c:3:24:错误:structures.h:没有那个文件或目录main.c:在函数‘main’中:main.c:6: 错误:'stud' 未声明(第一次在这个函数中使用)main.c:6: error: (每个未声明的标识符只报告一次main.c:6: 错误:对于它出现的每个函数.)main.c:6: error: ‘value’ undeclared (第一次在这个函数中使用)

structures.h 文件包含到 main.c 中的正确方法是什么?

解决方案

当引用头文件相对到你的 c 文件时,你应该使用 #include "path/to/header.h"

表单 #include 仅用于内部标题或显式添加的目录(在带有 -I 选项的 gcc 中).>

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 
");
    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?

解决方案

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).

这篇关于包含来自另一个目录的头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆