Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 566
  • Last Modified:

Fatal Error

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

Please tell me what is going wrong.

Thanks!
0
dshrenik
Asked:
dshrenik
  • 9
  • 7
  • 5
  • +2
2 Solutions
 
ZoppoCommented:
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
 
käµfm³d 👽Commented:
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
 
Deepu AbrahamR & D Engineering ManagerCommented:
The /FORCE or /FORCE:MULTIPLE option overrides this error.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
dshrenikAuthor Commented:
I'm not using any #DEFINE
I have multiple #include (in different files) though
0
 
ZoppoCommented:
> 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
 
käµfm³d 👽Commented:
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
 
dshrenikAuthor Commented:

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

Open in new window

0
 
käµfm³d 👽Commented:
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
 
ZoppoCommented:
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
 
dshrenikAuthor Commented:
@Zippo:
I haven't done that.

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

Thanks!
0
 
ZoppoCommented:
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
 
dshrenikAuthor Commented:

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
 
ZoppoCommented:
Is this in a header?
0
 
dshrenikAuthor Commented:
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
 
ZoppoCommented:
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
 
dshrenikAuthor Commented:
Phew! Thanks!

Between, can you tell me what exactly does "#pragma one" do?
0
 
ZoppoCommented:
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
 
dshrenikAuthor Commented:
Thanka!
0
 
käµfm³d 👽Commented:
@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
 
evilrixSenior Software Engineer (Avast)Commented:
#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
 
ZoppoCommented:
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
 
evilrixSenior Software Engineer (Avast)Commented:
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
 
ZoppoCommented:
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
 
evilrixSenior Software Engineer (Avast)Commented:
Thanks my friend :)
0
 
käµfm³d 👽Commented:
So much love at EE  = )
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 9
  • 7
  • 5
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now