简体   繁体   English

在多个头文件中使用相同的函数名称是否是一种好习惯?

[英]Is it good practice to have the same function names in multiple header files?

I'm writing a userland in C (be it minimal at the moment) and I'm making it like BusyBox where you can run a command by invoking a symlink with its name. 我正在用C语言编写一个用户域(目前为止很少),就像BusyBox一样,您可以在其中通过调用带有其名称的符号链接来运行命令。 I've got a "main" C file which includes all the other files inside it (the other files are header files) and then runs their code if the symlink name matches. 我有一个“主” C文件,其中包含其中的所有其他文件(其他文件是头文件),然后在符号链接名称匹配时运行其代码。 I was wondering whether it is deemed OK to have functions with the same name across different header files? 我想知道在不同的头文件中具有相同名称的函数是否正常?

For example, if I had thing1.h : 例如,如果我有thing1.h

void help(void)
{
    // Print help text for thing1
}

int thing1(int argc, char *argv[])
{
    if (something)
        help();
}

And thing2.h : 还有thing2.h

void help(void)
{
    // Print help text for thing2
}

int thing2(int argc, char *argv[])
{
    if (something)
        help();
}

And everything.c : everything.c

#include "thing1.h"
#include "thing2.h"

int main(int argc, char *argv[])
{
    if (something)
        thing1(argc, argv);
    else
        thing2(argc, argv);
}

Would it be better to rename those help functions to thing1_help and thing2_help respectively, or is it OK to leave them as they are? 将这些帮助函数分别重命名为thing1_helpthing2_help还是thing2_help ,还是可以将它们保留为原样?

First, please understand more how the C preprocessor works and read also the documentation of cpp . 首先,请进一步了解C预处理程序的工作原理,并阅读cpp文档

Notice that preprocessing is a purely textual operation. 请注意,预处理是纯文本操作。 So you could avoid having any header files and #include directives, eg by copy-and-pasting things. 因此,您可以避免使用任何头文件和#include指令,例如通过复制粘贴内容。 That would be a bad idea. 那将是一个坏主意。

So header files are mostly a conventional thing (however, the C99 standard does mandate some standard headers, like <stdlib.h> or <stdio.h> ; and POSIX specifications also mandates several headers). 因此,头文件通常是常规的东西(但是, C99标准确实要求一些标准头,例如<stdlib.h><stdio.h>POSIX规范也要求一些头)。 Common practice includes: 常见的做法包括:

  • wrapping the header file contents with an include guard (to disable multiple inclusion) 用头文件保护头包装头文件内容(以禁用多个头文件)
  • putting only declaration , not definition of (often "global") functions, types, variables in header files. 在头文件中仅放置声明 ,而不放置(通常是“全局”)函数,类型,变量的定义
  • putting the definition of static inline functions in header files. static inline函数的定义放在头文件中。

Actually, standard header inclusion (eg #include <stdlib.h> ) could even in principle be implemented without any stdlib.h textual file : the compiler implementation could for instance query a database to process #include <stdlib.h> directive, but I know no compiler doing that. 实际上,原则上甚至可以在没有任何stdlib.h文本文件的情况下实现标准头包含(例如#include <stdlib.h> ):编译器实现例如可以查询数据库以处理#include <stdlib.h>指令,但是我知道没有编译器这样做。

Some people (me included) are putting #include directives (notably for standard C99 or Posix headers) inside their header files. 有些人(包括我在内)正在将#include指令(特别是对于标准C99或Posix头文件)放入其头文件中。 Others are documenting the list of headers to be included before their own header files. 其他人则在自己的头文件之前记录要包含的头列表。

Look at the preprocessed form of your code (which is what most of the compiler care about, since the preprocessor is the first phase of the compiler). 查看代码的预处理形式(这是大多数编译器关心的,因为预处理器是编译器的第一阶段)。 You could obtain the preprocessed form of everything.c using GCC with 您可以使用GCC使用以下方法获取everything.c的预处理形式:

 gcc -C -E everything.c > everything.i

Then look inside everything.i with an editor or a pager. 然后用编辑器或寻呼机查看everything.i

In practice, I would suggest to make -if it is really short- your thing2 a static inline function, then to have a single (not several) header: 在实践中,我会建议做-如果它真的是你的短thing2一个static inline函数,然后有一个单一 (没有几个)标题:

// file thing.h
#ifndef THING_INCLUDED_
// declarations
void help(void);
int thing1(int argc, char *argv[]);
// definition:
static inline void thing2(int argc, char**argv) {
   if (something_with(argc, argv)) 
      help();
} 
#endif /*THING_INCLUDED_*/

then put the definitions of help and of thing1 in eg your everything.c and add there an #include "thing.h" after the others #include directives. 然后将helpthing1定义放在例如您的everything.c并在其他#include指令之后添加一个#include "thing.h"

Both help functions should be the respective c files then it wouldn't matter that there's a name clash as they are private to the scope of that compilation unit. 这两个帮助函数都应该是各自的c文件,因此名称冲突并不重要,因为它们是该编译单元范围的私有文件。

In general keep code in c files and only declarations in h files. 通常,将代码保存在c文件中,仅将声明保存在h文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM