具有多个文件(头文件)的C项目 [英] C project with multiple files (header files)

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

问题描述

我对C语言还很陌生,但是已经积累了一些经验.现在,我正在使用(或多或少)复杂的数据结构(例如Map(我将使用Maps作为示例))来创建较大的项目.因为我希望我的数据结构代码可用于将来的项目,所以我希望它们相当通用并且位于单独的文件中.
由于C不使用/没有泛型(如Java)oder模板(如C ++)或任何类似的概念,所以我考虑使用全局定义的数据类型,如

I'm fairly new to the C language, but have acquired some experience. Now I'm on my way creating larger projects with (more or less) complex data structures (e.g. Map (I will use Maps as an example thoughout)). Since I want my data structure code to be reusable for future projects, I like them being rather generic and in separate files.
Since C doesn't use/have Generics (like Java) oder Templates (like C++) or any similar concept I thought about using a globally defined data type like

typedef union {
    int integer;
    char * str;
    // etc.
} data_t;

并将其放入 main.h 中,该文件将包含在所有其他(头)文件中(可能使用防护措施).这对我来说效果很好,但是...

and put that in a main.h which will be included into all other (header) files (possible using guards). This works fairly well for me, but …

是否可以将数据结构集成到我的 data_t 中(包括 main.h 以使用 data_t )?/strong>
简单但显然不起作用(由于循环包含)的解决方案是将 main.h 中的 #include"map.h" 包含在 main.h 中,同时还包含> map.h 中的> main.h ;如前所述,由于明显的原因,这是行不通的.
基本上,我想要一个可以容纳其他地图的地图,同时只使用一个 data_t 和一个地图实现.跟踪哪个层"被保留.我将在周围的程序中完成操作(或者我可以在 data_t 中添加有关其类型的信息,这不是这里的重点).我知道仅使用 void * 就有可能实现;但我不想不必要地引用诸如 int 之类的原始数据类型.

is there a way to integrate data structures into my data_t (which include main.h to use data_t) ?
The simple-but-obviously-not-working (due to circular includes) solution is to #include "map.h" in main.h while also including main.h in map.h; as mentioned, this doesn't work for obvious reasons.
Basically I want a Map that can hold other Maps, all while using only one data_t and one Map-implementation. Keeping track of which "layer" I am on will be done in the surrounding program (or maybe I can add some info in the data_t about its type, this is not the focus here). I know that this will be possible when just using a void *; but I don't want unnecessary references for primitive datatypes like int if I don't have to.

有什么干净的方法可以做这种行为吗?

Is there any clean way to do such behavior ?

谢谢!
(如果需要任何实际代码,请告诉我)

Thank you !
(if any actual code is needed, tell me)

main.h 这样的通用声明,例如我的 data_t :

main.h which I want to contain general declarations like my data_t:

#ifndef _MAIN_H_
#define _MAIN_H_

#include "map.h"

typedef union {
    int integer;
    char * str;
    map_t map;
} data_t;

#endif

map.h :

#ifndef _MAP_H_
#define _MAP_H_

#include "main.h"

typedef char * key_t;

typedef struct {
    int (*hash_f)(key_t);
    int size;
    data_t * data;
} map_t;

int map_init(map_t * map, int (*hash_f)(key_t key));
int map_put(map_t map, key_t key, data_t data);
int map_get(map_t map, key_t key);

#endif

使用 make 进行编译:

% make
gcc -Wall -Wextra -Wpedantic -g -c main.c -o build/main.o
In file included from main.c:1:
./main.h:8:5: error: unknown type name 'map_t'
    map_t map;
    ^
1 error generated.
make: *** [build/main.o] Error 1

推荐答案

在一个源代码文件中使用声明和类型定义.
编译后,请考虑将其放在标题中.这样,您可以确保一开始就看到编译器看到的包含头文件的一个(每个)代码文件的内容.

Get your declarations and typedefinitions working within one source code file.
When that compiles, think what to put in a header. That way you make sure that at first you see what the compiler sees for one (each) code file your header gets included into.

如果无法使它在一个不包含代码的文件中工作,那么您将遇到无法使用标头和防护解决的问题.
但是,如果可以这样做,则只需查看单个文件,然后考虑现在我还想在下一个代码文件中使用什么?".并将其放在标题中(当然,请确保只将声明,typedef,宏-def移到标头中.代码,变量定义不会移到标头中.)

If you cannot get it to work within one non-including code file, then you have a problem that cannot be solved with headers and guards.
If however you can do it, then just look at the single file and think "Now what do I want to also use in the next code file?" and put that into a header. (Make of course sure to only move declarations, typedefs, macro-defs into a header. Code, variable definitions do not go into headers.)

循环依赖不能由守卫解决,只能由于(间接)多重包含而重新定义.

Circular dependencies cannot be solved by guards, only redefinitions due to (indirect) multiple inclusion.

这篇关于具有多个文件(头文件)的C项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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