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

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

问题描述

我有一个主目录 A ,有两个子目录 B C

I have a main directory A with two sub directories B and 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 ?

推荐答案

引用头文件 relative 时你的c文件你应该使用 #includepath / to / header.h

When referencing to header files relative to your c file you should use #include "path/to/header.h"

表单 #include< someheader.h> 仅用于内部标头或显式添加的目录(在gcc中使用 -I 选项)。

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天全站免登陆