views:

136

answers:

5

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 function with the same name in two differenct source file? 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 fuction name in the C programming language?

Thanks.

A: 

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
Declaring static is the best solution as suggested by RBerteig. I leave my answer in hope that it might be helpfull in anyway to someone.
+13  A: 

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.

RBerteig
Advice: Mark every function you can as `static`. It will also make it easier for the compiler's optimizer, for example to choose to inline functions and similar.
kaizer.se
+3  A: 

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

vpit3833
+2  A: 

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

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

Imagaien that a.h and b.h both defclare 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?

LeonixSolutions
+3  A: 

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.

AndreyT