Link to home
Start Free TrialLog in
Avatar of dshrenik
dshrenikFlag for United States of America

asked on

Fatal Error

I get this error:
 fatal error LNK1169: one or more multiply defined symbols found

Please tell me what is going wrong.

Thanks!
Avatar of Zoppo
Zoppo
Flag of Germany image

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

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

The /FORCE or /FORCE:MULTIPLE option overrides this error.
Avatar of dshrenik

ASKER

I'm not using any #DEFINE
I have multiple #include (in different files) though
> 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 ...
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.

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

Open in new window

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  = )
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
@Zippo:
I haven't done that.

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

Thanks!
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)

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

Is this in a header?
NO. I have stored this as a separate .cpp file
I have used #include"Unique.cpp" in the .cpp file that reference this function
ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Phew! Thanks!

Between, can you tell me what exactly does "#pragma one" do?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanka!
@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?
#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
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++ ...
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 :)
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.
Thanks my friend :)
So much love at EE  = )