473,396 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Multiple definitions error compiler is not throwing

I have same function in multiple files but compiling and linking is happening without error
May 12 '20 #1
6 3392
Is there any problem with the compiler I am trying to understand.Thanks in advance
May 12 '20 #2
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.cpp
May 12 '20 #3
dev7060
636 Expert 512MB
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 understand
How do you know those multiple files are indeed being linked together and able to produce an ambiguity issue that could lead to a situation when the linker doesn't know which version of the function to call?
May 12 '20 #4
donbock
2,426 Expert 2GB
There are lots of possibilities here. Please show us what you have. Post the relevant filenames and function definitions, declarations, and prototypes like this:
Expand|Select|Wrap|Line Numbers
  1. header1.h
  2. float func(…);
  3.  
  4. file1.c
  5. static int func(…) {…}
  6.  
  7. file2.c
  8. #include "header1.h"
  9. float func(…) {…}
  10.  
  11. file3.c
  12. double func();
  13. double func(…) {…}
  14.  
The dot-dot-dots are deliberate - no need to give us the argument list or the body of the functions.
May 12 '20 #5
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 error
May 13 '20 #6
donbock
2,426 Expert 2GB
What is this?
Expand|Select|Wrap|Line Numbers
  1. FUNC(void,a)
Is it a macro?
May 22 '20 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: prettysmurfed | last post by:
Hi all I have this, probably stupid question, how to avoid multiple definitions when a header file is included more than once. I thought, when you wrote the header-file and used the...
7
by: Ittay Dror | last post by:
Hi I have this peculiar situation. I have a library of utility classes, which I use to compile a binary. I want to reimplement a method in class. The class is only a collection of static...
2
by: Martin Magnusson | last post by:
I have a problem with multiple definitions that I can't quite straighten out. I have a templated class defined inside a namespace, and I want to create a function in that namespace that works on...
5
by: Charles L | last post by:
Can someone explain to me what the following means? "C permits multiple definitions of a variable in any given namespace, provided the definitions are the same and it generates only a single...
4
by: Andrew | last post by:
Hello, I am recieving a multiple definition error from the linker when I try to build a project I am working on. The message states that the functions I defined within an external .c source file...
9
by: lbj137 | last post by:
I have two files: A.c and B.c. In both files I define a global variable, int xxxx; When I compile with a green hills compiler (and also i think with a GNU compiler) I get no errors or warnings....
8
by: yossi.kreinin | last post by:
Hi! When are multiple definitions of global variables with the same name considered legal in C, and how is it different from C++? It appears that in terms of assembly language, some C...
13
by: a.zeevi | last post by:
free() multiple allocation error in C ==================================== Hi! I have written a program in C on PC with Windows 2000 in a Visual C environment. I have an error in freeing...
10
by: zfareed | last post by:
Similar problem to the previous post. I have created a project with about 7 files including 3 header files and 3 implementation files. I am getting a multiple definition error when compiling for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.