我在C中将所有这些函数类似#defines放在哪里?

时间:2021-02-11 12:22:05

I'm working with an embedded system, and I'm ending up with a ton of HW-interfacing #define macros. I want to put all of these into a separate file (for OOP-ness), but I don't know the best way to #include that. Do I just put them all into a .c file, then include that? Seems silly to put these in a .h file.

我正在使用嵌入式系统,我最终得到了大量的HW接口#define宏。我想将所有这些放入一个单独的文件中(对于OOP-ness),但我不知道#include的最佳方法。我只是将它们全部放入.c文件中,然后包含它吗?把它们放在.h文件中似乎很傻。

4 个解决方案

#1


13  

I don't see anything wrong with the .h file.

我没有看到.h文件有什么问题。

#2


7  

These should go in the .h files. The other option is a .c file, and that would require using #include to include a .c file, which will very definitely confuse people -- as well as confusing your makefile, if it uses the standard assumption that every .c file will correspond directly to a compiled .o file.

这些应该放在.h文件中。另一个选项是.c文件,这需要使用#include来包含一个.c文件,如果它使用每个.c文件将使用的标准假设,这将非常混淆人们 - 以及混淆你的makefile直接对应于已编译的.o文件。

The normal pattern is that .h files are for things that are included in other places (and, particularly, in multiple other places), and that .c files are for things that are compiled once into object files.

正常模式是.h文件用于包含在其他地方(特别是在其他多个地方)的东西,而.c文件用于一次编译到目标文件中的东西。

Thus, the following things normally go into .h files:

因此,通常会将以下内容放入.h文件中:

  • Function prototypes
  • Constant declarations
  • Global variable extern declarations
  • 全局变量extern声明

  • Inline function definitions
  • 内联函数定义

  • Type definitions
  • and macro definitions, such as what you're asking about.
  • 和宏定义,例如你要问的内容。

Conversely, the following things normally go into .c files:

相反,以下内容通常会进入.c文件:

  • Global variable definitions
  • 全局变量定义

  • Function definitions that will be compiled into object code and linked
  • 将编译为目标代码并链接的函数定义

The case of "function definitions only go into .c files" is simply the degenerate case when you don't have any inline functions.

“功能定义仅进入.c文件”的情况只是当您没有任何内联函数时的简并情况。

In C++, where lots of functions are defined in templated form and thus the definitions need to be included whenever they're used, those definitions very often go in the .h (or .hpp, or whatever) file. So this sort of thing definitely has precedent.

在C ++中,许多函数以模板形式定义,因此无论何时使用它们都需要包含定义,这些定义通常都在.h(或.hpp或其他)文件中。所以这种事情肯定有先例。

#3


2  

Put them where you need them.

把它们放在你需要的地方。

If you need it only for one file then put it at the top of that file.

如果只需要一个文件,则将其放在该文件的顶部。

If you need it for multiple files then put it in a header file.

如果您需要多个文件,请将其放在头文件中。

#4


2  

I'm not necessarily recommending this but have seen it in quite a few embedded projects over the last 10+ years: include inline functions as .inl.

我不一定推荐这个,但在过去的10多年中已经在很多嵌入式项目中看到了它:将内联函数包含为.inl。

*s breaks down the responsibilities nicely. You might consider separating inline and macro definitions from ordinary function prototypes and such:

布鲁克斯很好地分解了责任。您可以考虑将内联和宏定义与普通函数原型分开,例如:

#include "prototypes.h"
#include "macros.inl"

int foo(void);
int bar(char);

Your end goal is consistency: any layout decisions should assist those who succeed you.

您的最终目标是一致性:任何布局决策都应该帮助那些接替您的人。

#1


13  

I don't see anything wrong with the .h file.

我没有看到.h文件有什么问题。

#2


7  

These should go in the .h files. The other option is a .c file, and that would require using #include to include a .c file, which will very definitely confuse people -- as well as confusing your makefile, if it uses the standard assumption that every .c file will correspond directly to a compiled .o file.

这些应该放在.h文件中。另一个选项是.c文件,这需要使用#include来包含一个.c文件,如果它使用每个.c文件将使用的标准假设,这将非常混淆人们 - 以及混淆你的makefile直接对应于已编译的.o文件。

The normal pattern is that .h files are for things that are included in other places (and, particularly, in multiple other places), and that .c files are for things that are compiled once into object files.

正常模式是.h文件用于包含在其他地方(特别是在其他多个地方)的东西,而.c文件用于一次编译到目标文件中的东西。

Thus, the following things normally go into .h files:

因此,通常会将以下内容放入.h文件中:

  • Function prototypes
  • Constant declarations
  • Global variable extern declarations
  • 全局变量extern声明

  • Inline function definitions
  • 内联函数定义

  • Type definitions
  • and macro definitions, such as what you're asking about.
  • 和宏定义,例如你要问的内容。

Conversely, the following things normally go into .c files:

相反,以下内容通常会进入.c文件:

  • Global variable definitions
  • 全局变量定义

  • Function definitions that will be compiled into object code and linked
  • 将编译为目标代码并链接的函数定义

The case of "function definitions only go into .c files" is simply the degenerate case when you don't have any inline functions.

“功能定义仅进入.c文件”的情况只是当您没有任何内联函数时的简并情况。

In C++, where lots of functions are defined in templated form and thus the definitions need to be included whenever they're used, those definitions very often go in the .h (or .hpp, or whatever) file. So this sort of thing definitely has precedent.

在C ++中,许多函数以模板形式定义,因此无论何时使用它们都需要包含定义,这些定义通常都在.h(或.hpp或其他)文件中。所以这种事情肯定有先例。

#3


2  

Put them where you need them.

把它们放在你需要的地方。

If you need it only for one file then put it at the top of that file.

如果只需要一个文件,则将其放在该文件的顶部。

If you need it for multiple files then put it in a header file.

如果您需要多个文件,请将其放在头文件中。

#4


2  

I'm not necessarily recommending this but have seen it in quite a few embedded projects over the last 10+ years: include inline functions as .inl.

我不一定推荐这个,但在过去的10多年中已经在很多嵌入式项目中看到了它:将内联函数包含为.inl。

*s breaks down the responsibilities nicely. You might consider separating inline and macro definitions from ordinary function prototypes and such:

布鲁克斯很好地分解了责任。您可以考虑将内联和宏定义与普通函数原型分开,例如:

#include "prototypes.h"
#include "macros.inl"

int foo(void);
int bar(char);

Your end goal is consistency: any layout decisions should assist those who succeed you.

您的最终目标是一致性:任何布局决策都应该帮助那些接替您的人。