c - 包含来自另一个目录的头文件

标签 c compiler-errors include c-preprocessor

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

目录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/

相关文章:

c - OS X 上的 vwprintf/vswprintf 问题

c - 使用 fgets() 读取文件最后一行时出现段错误

c++ - 在 CodeBlocks 中的 Windows 8 上的 cygwin64 下使用 strptime

eclipse - "Exception in thread "main "java.util.NoSuchElementException: head of empty list "error and more in Scala

gcc - 为什么要在项目中使用#include_next?

c++ - 在 C++ 中包含非标准 C 头文件

c - 有什么方法可以在 C 中执行类似 std::bind 的操作吗?

c++ - C++编译错误已自行修复(试图找出原因)

android - 空载Map Android-Google Maps V2

Node.js 使用外部 JS 文件?