Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with duplicated function name within C?

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'.

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?

like image 891
Jichao Avatar asked Jun 23 '10 01:06

Jichao


5 Answers

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.

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.

like image 94
RBerteig Avatar answered Oct 24 '22 03:10

RBerteig


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.

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.

like image 15
AnT Avatar answered Oct 24 '22 03:10

AnT


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?

like image 8
Mawg says reinstate Monica Avatar answered Oct 24 '22 03:10

Mawg says reinstate Monica


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

like image 5
vpit3833 Avatar answered Oct 24 '22 03:10

vpit3833


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.

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
like image 4
user347594 Avatar answered Oct 24 '22 01:10

user347594