I have a main directory A
with two sub directories B
and C
.
我有一个主目录a和两个子目录B和C。
Directory B
contains a header file structures.c
:
目录B包含一个头文件结构。
#ifndef __STRUCTURES_H
#define __STRUCTURES_H
typedef struct __stud_ent__
{
char name[20];
int roll_num;
}stud;
#endif
Directory C
contains main.c
code:
目录C包含主要。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;
}
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
?
什么是正确的方式包括结构。h文件到c ?
4 个解决方案
#1
29
When referencing to header files relative to your c file you should use #include "path/to/header.h"
当引用与您的c文件相关的头文件时,应该使用#include“路径/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).
# include < someheader形式。h>只用于内部头文件或显式添加的目录(在gcc中使用-I选项)。
#2
11
write
写
#include "../b/structure.h"
in place of
在的地方
#include <structures.h>
then go in directory in c & compile your main.c with
然后进入c目录,编译你的主目录。c与
gcc main.c
#3
2
If you work on a Makefile project or simply run your code from command line, use
如果您在Makefile项目中工作,或者只是从命令行运行代码,请使用
gcc -IC main.c
gcc的" c
where -I
option adds your C
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.
where -I选项将C目录添加到要搜索头文件的目录列表中,因此您将能够使用#include“结构”。h”在您的项目。
#4
1
If you want to use the command line argument then you can give gcc -idirafter ../b/ main.c
如果您想使用命令行参数,那么您可以给gcc -idirafter…/ b / c
then you don't have to do any thing inside your program.
那么你就不需要在程序中做任何事情了。
#1
29
When referencing to header files relative to your c file you should use #include "path/to/header.h"
当引用与您的c文件相关的头文件时,应该使用#include“路径/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).
# include < someheader形式。h>只用于内部头文件或显式添加的目录(在gcc中使用-I选项)。
#2
11
write
写
#include "../b/structure.h"
in place of
在的地方
#include <structures.h>
then go in directory in c & compile your main.c with
然后进入c目录,编译你的主目录。c与
gcc main.c
#3
2
If you work on a Makefile project or simply run your code from command line, use
如果您在Makefile项目中工作,或者只是从命令行运行代码,请使用
gcc -IC main.c
gcc的" c
where -I
option adds your C
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.
where -I选项将C目录添加到要搜索头文件的目录列表中,因此您将能够使用#include“结构”。h”在您的项目。
#4
1
If you want to use the command line argument then you can give gcc -idirafter ../b/ main.c
如果您想使用命令行参数,那么您可以给gcc -idirafter…/ b / c
then you don't have to do any thing inside your program.
那么你就不需要在程序中做任何事情了。