定义用于多个文件的枚举数据类型

时间:2022-06-25 19:18:47

I have several files like main.c, main.h, some_funcs.c and some_funcs.h.

我有几个文件,如main.c,main.h,some_funcs.c和some_funcs.h。

main.h is included in main.c
main.h include some_funcs.h
some_funcs.c include some_funcs.h

When I now define a new data type like:

我现在定义一个新的数据类型,如:

//Datatypes
enum _bool {
   false = 0,
   true = 1
};

typedef enum _bool Bool;

If i define it in e.g. main.h and want to use it in some_func.c it does not work. Is there a ways to define it somewhere without always include the header where it is defined?

如果我在例如main.h并希望在some_func.c中使用它,它不起作用。有没有办法在某处定义它而不总是包含定义它的标题?

3 个解决方案

#1


3  

Put the definition in some_funcs.h. This will make it visible in main.h, main.c, some_funcs.h and some_funcs.c.

将定义放在some_funcs.h中。这将使它在main.h,main.c,some_funcs.h和some_funcs.c中可见。

A more general solution is to put common data types into a file named common.h for instance.

更通用的解决方案是将通用数据类型放入名为common.h的文件中。

You then include this file in all header files.

然后,将此文件包含在所有头文件中。

This would be the content of the common.h file. The ifdef is to ignore the content of the file if it was already included.

这将是common.h文件的内容。如果文件的内容已经包含,则ifdef将忽略该文件的内容。

#ifndef COMMON_H
#define COMMON_H

//Datatypes
enum _bool {
   false = 0,
   true = 1
};

typedef enum _bool Bool;

#endif

#2


1  

Is there a ways to define it somewhere without always include the header where it is defined?

有没有办法在某处定义它而不总是包含定义它的标题?

NO.

You have to include header in each source file.

您必须在每个源文件中包含标头。

A normal practice is to have your own typdef.h. Define your own data types in it and include it in your source files of interest.

通常的做法是拥有自己的typdef.h。在其中定义自己的数据类型并将其包含在您感兴趣的源文件中。

#3


1  

Is there a ways to define it somewhere without always include the header where it is defined?

有没有办法在某处定义它而不总是包含定义它的标题?

It sounds like can I use a definition of a type without defining it.:)

听起来我可以在不定义类型的情况下使用类型的定义。:)

You should include the header file where the enumeration and typedef are defined in each module that uses it.

您应该包含头文件,其中枚举和typedef在使用它的每个模块中定义。

Take into account that there is 1) type _Bool in C and 2) standard header <stdbool.h> where macros bool, false, and true are already defined.

考虑到在C中有1)类型_Bool和2)标准头 ,其中已经定义了宏bool,false和true。

#1


3  

Put the definition in some_funcs.h. This will make it visible in main.h, main.c, some_funcs.h and some_funcs.c.

将定义放在some_funcs.h中。这将使它在main.h,main.c,some_funcs.h和some_funcs.c中可见。

A more general solution is to put common data types into a file named common.h for instance.

更通用的解决方案是将通用数据类型放入名为common.h的文件中。

You then include this file in all header files.

然后,将此文件包含在所有头文件中。

This would be the content of the common.h file. The ifdef is to ignore the content of the file if it was already included.

这将是common.h文件的内容。如果文件的内容已经包含,则ifdef将忽略该文件的内容。

#ifndef COMMON_H
#define COMMON_H

//Datatypes
enum _bool {
   false = 0,
   true = 1
};

typedef enum _bool Bool;

#endif

#2


1  

Is there a ways to define it somewhere without always include the header where it is defined?

有没有办法在某处定义它而不总是包含定义它的标题?

NO.

You have to include header in each source file.

您必须在每个源文件中包含标头。

A normal practice is to have your own typdef.h. Define your own data types in it and include it in your source files of interest.

通常的做法是拥有自己的typdef.h。在其中定义自己的数据类型并将其包含在您感兴趣的源文件中。

#3


1  

Is there a ways to define it somewhere without always include the header where it is defined?

有没有办法在某处定义它而不总是包含定义它的标题?

It sounds like can I use a definition of a type without defining it.:)

听起来我可以在不定义类型的情况下使用类型的定义。:)

You should include the header file where the enumeration and typedef are defined in each module that uses it.

您应该包含头文件,其中枚举和typedef在使用它的每个模块中定义。

Take into account that there is 1) type _Bool in C and 2) standard header <stdbool.h> where macros bool, false, and true are already defined.

考虑到在C中有1)类型_Bool和2)标准头 ,其中已经定义了宏bool,false和true。