Solved

Fatal Error

Posted on 2011-03-01
25
557 Views
Last Modified: 2012-05-11
I get this error:
 fatal error LNK1169: one or more multiply defined symbols found

Please tell me what is going wrong.

Thanks!
0
Comment
Question by:dshrenik
  • 9
  • 7
  • 5
  • +2
25 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 35007147
Hi dshrenik,

in general this means you use a function which is found by the linker in two or more libraries. Most probably this error should be preceded by an LNK2005 error.

Could you post the complete error message? Then maybe we can tell you which libraries generate the problem - it even can have to do with wrong project/linker settings ...

ZOPPO

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35007165
Are you using #ifndef constructs to prevent multiple inclusions of your header files?
// In header file
#ifndef FILENAME_H
#define FILENAME_H

... .h file code ...

#endif

Open in new window

0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
ID: 35007177
The /FORCE or /FORCE:MULTIPLE option overrides this error.
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

Author Comment

by:dshrenik
ID: 35007182
I'm not using any #DEFINE
I have multiple #include (in different files) though
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 35007208
> The /FORCE or /FORCE:MULTIPLE option overrides this error.
That's right, but IMO it's better to find the cause for the error and, if possible, resolve it - /FORCE should only be used if there's no way to resolve this error in another suitable way.

@dshrenik: Please post relevant part of the linker output where this error is shown, this will help us finding the problem ...
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35007221
As I understand it, when you compile C/C++, any #include statements are basically just textual substitutions, where the substitution is the entire code from the file referenced by the include. When you #include in multiple places, you then have multiple copies of the code. Using the #ifndef construct prevents this, since you really only need one copy of the definition available to other parts of you code.

Try surrounding your header files' code sections using the method above. By convention, the "FILENAME_H" should be whatever the name of the header file is.
0
 

Author Comment

by:dshrenik
ID: 35007233

Unique.obj : error LNK2005: "bool __cdecl Unique(char *)" (?Unique@@YA_NPAD@Z) already defined in Main.obj

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35007245
The alternative would be to only include the #include in a single file, list that file first in your arguments to the compiler, and then list subsequent .cpp files using that header after the first; of course, other people using your files would need to know this as well! A lot simpler to use the #ifndef construct  = )
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 35007263
Could it be you have declared that function 'Unique' within a header like this:

bool __cdecl Unique(char *);

Open in new window


and included that header in different cpp files (unique.cpp and main.cpp)?

If you declare a (none class member-) function in a header which is used in multiple cpp files you'll have to declare it as extern:

extern bool __cdecl Unique(char *);

Open in new window


ZOPPO
0
 

Author Comment

by:dshrenik
ID: 35007283
@Zippo:
I haven't done that.

Please tell me how I can use #ifndef to include iostrream.h and conio.h

Thanks!
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 35007349
Sorry, my previous comment wasn't correct - this only is true if the complete function is implemented in the header.

>> Please tell me how I can use #ifndef to include iostrream.h and conio.h
Unfortunateley I don't see what this has to do with conio.h and iostream - both use either '#ifndef/endif' or '#pragma once' to avoid they're included more than once.

Is the function 'Unique' written by yourself in your code? If so, could you post the declaration and implementation?

ZOPPO


PS: It's Zoppo, not Zippo :o)
0
 

Author Comment

by:dshrenik
ID: 35007361

bool Unique(char *str)
{	
	char *temp = str;

	bool table[256] = {false};
	while(*temp)
	{
		int val = *temp;
		if(table[val])
		{
			return false;
		}
		table[val] = true;
		temp++;
	}

	return true;
}

Open in new window

0
 
LVL 31

Expert Comment

by:Zoppo
ID: 35007369
Is this in a header?
0
 

Author Comment

by:dshrenik
ID: 35007381
NO. I have stored this as a separate .cpp file
I have used #include"Unique.cpp" in the .cpp file that reference this function
0
 
LVL 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 35007415
Ah - that's a thing you IMO shouldn't do at all. It's at best confusing to include CPP files. In your case the 'unique.cpp' is compiled, then the 'main.cpp' is compiled including 'unique.cpp' - thus 'unique()' is compiled twice.

IMO you should add a header 'unique.h', implement it like this:

#pragma one
bool Unique(char *str);

Open in new window


and include this in main.cpp (or main.h) ...

ZOPPO
0
 

Author Comment

by:dshrenik
ID: 35007459
Phew! Thanks!

Between, can you tell me what exactly does "#pragma one" do?
0
 
LVL 31

Assisted Solution

by:Zoppo
Zoppo earned 500 total points
ID: 35007518
Sorry, this is a typo - it should be

#pragma once

Open in new window


That pragma tells the compiler it shouldn't include the file again if it was already included for the current compiled cpp file.

This is needed if i.e. you include multiple headers which themselve include identical headers, i.e. 'h1.h' and 'h2.h' both include 'h3.h', then you can include 'h1.h' and 'h2.h' in 'c1.cpp' only if you can tell the compiler to include the content of 'h3.h' once, otherwise the compiler would throw 'already defined' errors.

In older days there wasn't a #pragma once, therefor the above mentioned '#ifndef/#ifdef' technique was used, i.e.:

// file h3.h
#ifndef _H3_INCLUDED
#define _H3_INCLUDED

... // the code to include

#endif _H3_INCLUDED

Open in new window


Nowadays a simple #pragma once at the beginning of a file has the same effect.

ZOPPO
0
 

Author Comment

by:dshrenik
ID: 35007535
Thanka!
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35007558
@Zoppo

>>  Nowadays a simple #pragma once at the beginning of a file has the same effect.

Heheh...  can you tell I don't code in C/C++? I wasn't aware of the #pragma once directive. Is this all C/C++ compilers, or are there some that still defer to the old style?
0
 
LVL 40

Expert Comment

by:evilrix
ID: 35007614
#pragma once once is a compiler specific (Visual Studio) compile time directive. It is not the standard way to do this... for portability one should use an include guard.
http://en.wikipedia.org/wiki/Include_guard
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 35007728
http://en.wikipedia.org/wiki/Pragma_once says '#pragma once is a non-standard but widely supported preprocessor directive'.

I agree with evilrix that include guard should be/has to be used when writing portable code. On the other hand using include guards can lead to problems since the coders have to ensure each header file uses unique macro names to avoid clashes. I myself had this case once when a colleague copied/renamed a header without changing the include guards name.

I just used the pragma here since the linker error indicated it's for MSVC++ ...
0
 
LVL 40

Expert Comment

by:evilrix
ID: 35007770
Understood.... sorry Zoppo I was actually responding to kaufmed in http:#a35007558 and didn't mean to sound like I was disapproving of your solution. Under the circumstances it was just fine :)
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 35007837
No problem at all, evilrix - I didn't even think you wanted to critizise me. I interpreted your comment as a justified warning/hint about this issue in general and, since I know you're a thoroughly coding expert, I appreciated it.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 35007845
Thanks my friend :)
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35007926
So much love at EE  = )
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question