如何在C中处理重复的函数名?

时间:2021-10-19 16:00:14

I have a little project in which I named two same name function in two different source file, but while I building the project, the compiler failed with 'func_name already defined in filename.obj'.

我有一个小项目,我在两个不同的源文件中命名了两个相同的名称函数,但是在构建项目时,编译器失败并且'filename已经在filename.obj中定义了'func_name'。

Why could not I have two functions with the same name in two different source files? I thought the function should be local to the source file only if when we declared it in the header file will it become global.

为什么我不能在两个不同的源文件中有两个具有相同名称的函数?我认为该函数应该是源文件的本地函数,只有当我们在头文件中声明它将变为全局时。

And except for changing the filename, are there any other elegant solution to duplicated function name in the C programming language?

除了更改文件名外,C编程语言中是否还有其他优雅的复制函数名解决方案?

5 个解决方案

#1


29  

In C, a function has global scope by default. To restrict its scope, use the static keyword to make it private to a the module.

在C中,函数默认具有全局范围。要限制其范围,请使用static关键字将其设置为模块的私有。

The role of the header file is just to publicize the function along with its signature to other modules.

头文件的作用只是将函数及其签名公布给其他模块。

All global names must (with some caveats) be unique. This makes sense because that name is what is used by the linker to connect a function call to implementation of the function itself.

所有全球名称必须(有一些警告)是唯一的。这是有道理的,因为该名称是链接器用于将函数调用连接到函数本身的实现的名称。

Names with static and local scope need only be unique within their scope.

具有静态和本地范围的名称只需在其范围内是唯一的。

#2


6  

Whether some thing is declared in header file or in source file makes absolutely no difference for the compiler. In fact, the compiler proper knows absolutely nothing about any "header files", since header files are embedded into source files by so called preprocessor, which does its work before the compiler proper. By the time the source files (with embedded header files) get to the actual compiler, there's no way to tell what was there originally and what was inserted from header files. The source file with all the header files embedded into it is called translation unit. I.e. the compiler proper works with translation units, not with some "source" or "header" files.

是否在头文件或源文件中声明某些东西对编译器没有任何区别。实际上,编译器本身对任何“头文件”一无所知,因为头文件被所谓的预处理器嵌入到源文件中,它在编译器正常之前完成它的工作。当源文件(带有嵌入的头文件)到达实际的编译器时,无法分辨最初的内容以及从头文件中插入的内容。嵌入了所有头文件的源文件称为翻译单元。即编译器适用于翻译单元,而不是某些“源”或“标题”文件。

In C language all objects and functions declared at file scope have external linkage by default, which means that they are global, unique for the entire program. So, you thought incorrectly. Functions are not local to one source file only.

在C语言中,在文件范围内声明的所有对象和函数默认都具有外部链接,这意味着它们是全局的,对于整个程序是唯一的。所以,你认为不正确。函数不仅仅是一个源文件的本地。

If you want to make a function (or an object) local to a single translation unit, you have to take some explicit steps. You have to declare it as static. Declaring it as static will give it internal linkage, which essentially means that it becomes internal to its translation unit.

如果要在单个翻译单元中创建本地函数(或对象),则必须采取一些明确的步骤。你必须将它声明为静态。将其声明为静态将使其内部链接,这实际上意味着它变为其翻译单元的内部。

Declaring your functions static will only work if both of them really have to be local to their own translation units. If this is not the case, i.e. if at least one of the functions should be a globally accessible (linkable) function, then you have no other choice but to rename one of functions.

将您的函数声明为静态只有在它们都必须是自己的翻译单元本地时才有效。如果不是这种情况,即如果至少有一个函数应该是全局可访问(可链接)函数,那么除了重命名其中一个函数之外别无选择。

#3


6  

Why could not I have two function with the same name in two differenct source file?

为什么我不能在两个不同的源文件中使用两个具有相同名称的函数?

Because the linker needs to know which is meant when you reference it.

因为链接器需要知道引用它时的含义。

Imagine that a.h and b.h both declare my_function(). The compiler generates code for both. Now, imagine that c.c calls my_function() - how does the linker know which of the two versions of the function should be called?

想象一下,a.h和b.h都声明了my_function()。编译器为两者生成代码。现在,假设c.c调用my_function() - 链接器如何知道应该调用函数的两个版本中的哪一个?

#4


4  

Declare the function static to make it local to the file. In C, every identifier name must be unique.

声明函数static以使其成为文件的本地函数。在C中,每个标识符名称必须是唯一的。

#5


2  

The elegant solution is namespaces introduced in C++. The solution, if there are few calls to func_name is take one, rename it and recompile.

优雅的解决方案是C ++中引入的命名空间。解决方案,如果对func_name的调用很少,则选择一个,重命名并重新编译。

Something hackerous but quick solution might be this:

一些黑客但快速的解决方案可能是这样的:

//In one of the two source files and any file that calls it

//if your functions is something like this
//void func_name(int) { ... }
//Add the following line
#define func_name SOME_UNIQUE_FUNC_NAME

#1


29  

In C, a function has global scope by default. To restrict its scope, use the static keyword to make it private to a the module.

在C中,函数默认具有全局范围。要限制其范围,请使用static关键字将其设置为模块的私有。

The role of the header file is just to publicize the function along with its signature to other modules.

头文件的作用只是将函数及其签名公布给其他模块。

All global names must (with some caveats) be unique. This makes sense because that name is what is used by the linker to connect a function call to implementation of the function itself.

所有全球名称必须(有一些警告)是唯一的。这是有道理的,因为该名称是链接器用于将函数调用连接到函数本身的实现的名称。

Names with static and local scope need only be unique within their scope.

具有静态和本地范围的名称只需在其范围内是唯一的。

#2


6  

Whether some thing is declared in header file or in source file makes absolutely no difference for the compiler. In fact, the compiler proper knows absolutely nothing about any "header files", since header files are embedded into source files by so called preprocessor, which does its work before the compiler proper. By the time the source files (with embedded header files) get to the actual compiler, there's no way to tell what was there originally and what was inserted from header files. The source file with all the header files embedded into it is called translation unit. I.e. the compiler proper works with translation units, not with some "source" or "header" files.

是否在头文件或源文件中声明某些东西对编译器没有任何区别。实际上,编译器本身对任何“头文件”一无所知,因为头文件被所谓的预处理器嵌入到源文件中,它在编译器正常之前完成它的工作。当源文件(带有嵌入的头文件)到达实际的编译器时,无法分辨最初的内容以及从头文件中插入的内容。嵌入了所有头文件的源文件称为翻译单元。即编译器适用于翻译单元,而不是某些“源”或“标题”文件。

In C language all objects and functions declared at file scope have external linkage by default, which means that they are global, unique for the entire program. So, you thought incorrectly. Functions are not local to one source file only.

在C语言中,在文件范围内声明的所有对象和函数默认都具有外部链接,这意味着它们是全局的,对于整个程序是唯一的。所以,你认为不正确。函数不仅仅是一个源文件的本地。

If you want to make a function (or an object) local to a single translation unit, you have to take some explicit steps. You have to declare it as static. Declaring it as static will give it internal linkage, which essentially means that it becomes internal to its translation unit.

如果要在单个翻译单元中创建本地函数(或对象),则必须采取一些明确的步骤。你必须将它声明为静态。将其声明为静态将使其内部链接,这实际上意味着它变为其翻译单元的内部。

Declaring your functions static will only work if both of them really have to be local to their own translation units. If this is not the case, i.e. if at least one of the functions should be a globally accessible (linkable) function, then you have no other choice but to rename one of functions.

将您的函数声明为静态只有在它们都必须是自己的翻译单元本地时才有效。如果不是这种情况,即如果至少有一个函数应该是全局可访问(可链接)函数,那么除了重命名其中一个函数之外别无选择。

#3


6  

Why could not I have two function with the same name in two differenct source file?

为什么我不能在两个不同的源文件中使用两个具有相同名称的函数?

Because the linker needs to know which is meant when you reference it.

因为链接器需要知道引用它时的含义。

Imagine that a.h and b.h both declare my_function(). The compiler generates code for both. Now, imagine that c.c calls my_function() - how does the linker know which of the two versions of the function should be called?

想象一下,a.h和b.h都声明了my_function()。编译器为两者生成代码。现在,假设c.c调用my_function() - 链接器如何知道应该调用函数的两个版本中的哪一个?

#4


4  

Declare the function static to make it local to the file. In C, every identifier name must be unique.

声明函数static以使其成为文件的本地函数。在C中,每个标识符名称必须是唯一的。

#5


2  

The elegant solution is namespaces introduced in C++. The solution, if there are few calls to func_name is take one, rename it and recompile.

优雅的解决方案是C ++中引入的命名空间。解决方案,如果对func_name的调用很少,则选择一个,重命名并重新编译。

Something hackerous but quick solution might be this:

一些黑客但快速的解决方案可能是这样的:

//In one of the two source files and any file that calls it

//if your functions is something like this
//void func_name(int) { ... }
//Add the following line
#define func_name SOME_UNIQUE_FUNC_NAME