Solved

Fatal Error

Posted on 2011-03-01
25
553 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 30

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 74

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
 

Author Comment

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

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 74

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 74

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 30

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 30

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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 30

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 30

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 30

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 74

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 30

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 30

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 74

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now