I have same function in multiple files but compiling and linking is happening without error
Multiple definitions error compiler is not throwing
Collapse
X
-
Tags: None
-
-
Before actually compiling source the compilation unit is generated from .cpp files. This basically means that all preprocessor directives are computed: all #include will be replaces with content of the included files, all #define'd values will be substituted with corresponding expressions, all #if 0 ... #endif will be removed, etc. So after this step in your case you'll get two pieces of C++ code without any preprocessor directives that will both have definition of same function bool ifFileExist() that is why you get this multiple definition error.
The fast solution is to mark it as inline bool ifFileExist(). Basically you ask compiler to replace all corresponding function calls with content of the function itself.
Another approach is to live the declaration of your function in common_utility. h and move definition to common_utility. cppComment
-
A function is in the global scope by default and all global names must be unique.
I have same function in multiple files but compiling and linking is happening without error. Is there any problem with the compiler I am trying to understandComment
-
There are lots of possibilities here. Please show us what you have. Post the relevant filenames and function definitions, declarations, and prototypes like this:
Code:[B]header1.h[/B] float func(…); [B]file1.c[/B] static int func(…) {…} [B]file2.c[/B] #include "header1.h" float func(…) {…} [B]file3.c[/B] double func(); double func(…) {…}
Comment
-
A.c
*************** ********""
FUNC(void,a) ABC(void)
{
Random function():
}
*************** *"*"
B.c
FUNC(void,a) ABC(void)
{
Random function()
}
*************** ***
ABC is a function which is having the function definition in both
A.c and B.c files compiler is not throwing multiple definition error .as it expected to throw error.here my doubt is why it is not multiple definition errorComment
Comment