编译项目时出现下一个错误。
./PC.h:15:12: error: unknown type name 'InstructionMemory'
PC *new_pc(InstructionMemory *memoria);
^
./PC.h:17:1: error: unknown type name 'InstructionMemory'
InstructionMemory *get_memory(PC *programCounter);
^
2 errors generated.
In file included from main.c:5:
./InstructionRegister.h:7:36: error: unknown type name 'BankRegister'
int opera(InstructionRegister *IR, BankRegister *bank);
但这对我来说没有意义,我查看了这些文件,它们是头文件,所以我知道你不能在头文件中使用#include。所以我不知道我做错了什么。
这是我的PC.h文件的内容:
typedef struct PC PC;
PC *new_pc(InstructionMemory *memoria);
int getpc(PC *programCounter);
InstructionMemory *get_memory(PC *programCounter);
char *fecth(PC *programCounter);
char *linea_actual(PC *programCounter);
我使用下一个makefile来编译:
CC = gcc
CFLAGS=-I
DEPS = ALU.h InstructionRegister.h Rebasing.h BankRegister.h MemoryInstruction.h ControlUnit.h PC.h
run: exect
./exect $(EX)
%.o: %.c $(DEPS)
$(CC) -c $< $(CFLAGS)
exect: ALU.c InstructionRegister.c Rebasing.c BankRegister.c MemoryInstruction.c main.c ControlUnit.c PC.c
gcc -o exect InstructionRegister.c Rebasing.c BankRegister.c MemoryInstruction.c main.c ControlUnit.c PC.c -I.
clean:
rm -f *.o
最佳答案
so I know that you cannot use
#include
into headers file.
你错了。您可以而且经常应该并且愿意使用多个 include directives在你的头文件中。
一个典型的小型 C 项目(例如总共不到十万行 C 代码)通常会有一个单个通用头文件,例如myheader.h
,通常以 include guard 开头和几个系统包括,所以像
#ifndef MYHEADER_INCLUDED
#define MYHEADER_INCLUDED
// some system includes
#include <stdio.h>
#include <stdlib.h>
/// your type declarations
enum foo_en { /* blablabla */ };
struct bar_st { /* blablabla */ };
typedef struct bar_st Bar;
/* etc... */
/// your global functions
extern int myglobfun(int, int);
/* etc...*/
/// your global variables (have only a few of them)
extern Bar*my_bar;
/* etc... */
#endif /*MYHEADER_INCLUDED*/
这只是组织项目的一种可能性。有些人喜欢在每个翻译单元(例如 C 源文件)中的一些自己的头文件之前拥有许多头文件和显式 #include
系统头文件。
拥有单个通用 header 的优点是 Makefile
易于编码,您甚至可以precompile你的公共(public)标题。缺点是该 header 中的任何更改(例如在公共(public) struct
中添加字段)都会强制 make
重新编译所有内容(对于小项目来说这没什么大不了的)。
或者,您可以有许多头文件,然后一定要使用 include guards (在单个公共(public) header 的情况下,它实际上是无用的,但不会造成伤害),并定义有关多重包含的规则。通常,头文件本身会包含其他所需的头文件,或者检查它们是否已包含,例如与
// file "myheaderone.h"
#ifndef MYHEADERONE_INCLUDED
// check that "myotherheader.h" has been included
#ifndef MYOTHERHEADER_INCLUDED
#error myotherheader.h should have been #include-d
#endif
//// etc
或者,您可以在 myfirstheader.h
开头附近编写 #include "myotherheader.h"
代码
如果您有多个 header ,则需要一个复杂的 Makefile
并且您宁愿生成依赖项,请参阅 this 。它使用一些 preprocessor options像 -M
和 friend 一样到 gcc
。
顺便说一句,你的Makefile
是错误的。不要在其中硬编码 cc
(但使用 $(CC)
)。是sure询问GCC所有警告和调试信息(例如 CFLAGS= -Wall -g
)。了解 GNU make catalog of rules通过运行 make -p
。而您的 CFLAGS= -I
确实错误,您可能需要 CFLAGS= -I。 -Wall -g
因为 -I
后面应该始终跟一个目录。
gcc
,请养成始终向其传递 -Wall
的习惯。很多时候(当然在你的情况下)你还需要 -Wextra
(以获得更多警告;记住编译器警告是你的 friend ,你应该改进你的代码直到你没有它们)并且可能-g
(能够使用-g
调试器)。为了进行基准测试,要求编译器 optimize (例如使用 -O1
或 -O2 -mcpu=native
)。
请注意-H
preprocessor option :它要求 gcc 显示每个包含的文件。有时(例如,为了调试一些讨厌的宏)您想查看翻译单元的预处理形式,请使用 gcc -C -E
来获取它。
关于c - 当我用 C 编译时,头文件中出现导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38167406/