By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,827 Members | 813 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,827 IT Pros & Developers. It's quick & easy.

How to resolve error LNK2005 - function already defined

P: n/a
Hello

I have a function which I have defined in a .h file called LogError. I use
a sort of global .h file which is included in all cpp files - StdAfx.h -
this file #include's my .h file with this function LogError.

In my program I have several .c/.h files. If I use LogError in just one of
these .c files all is well. However, if I use LogError in two or more .c
files, then I get this sort of link error:

error LNK2005: "int __cdecl LogError(char * const)" (?LogError@@YAHQAD@Z)
already defined in Page3.obj

I had a go at putting - extern int LogError(TCHAR szErrorMsg[]) in stdafx.h
and even some other files - but that did not seem to help.

How can I resolve this?

Angus Comber
an***@NOSPAMiteloffice.com
Nov 14 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Angus Comber wrote:
Hello

I have a function which I have defined in a .h file called LogError. I use
a sort of global .h file which is included in all cpp files - StdAfx.h -
this file #include's my .h file with this function LogError.

In my program I have several .c/.h files. If I use LogError in just one of
these .c files all is well. However, if I use LogError in two or more .c
files, then I get this sort of link error:

error LNK2005: "int __cdecl LogError(char * const)" (?LogError@@YAHQAD@Z)
already defined in Page3.obj

I had a go at putting - extern int LogError(TCHAR szErrorMsg[]) in stdafx.h
and even some other files - but that did not seem to help.

How can I resolve this?


By not defining functions in headers. The definition of the function
should go into a translation unit (what you call ".c files"). The
header should have its prototype.

An unrelated problem: Since you refer to "cpp files" above, there it is
possible that you are invoking your compiler as a C++ compiler. If you
are writing C, then make sure that you are invoking your compiler as a C
compiler. If you are writing C++, your questions belong in
news:comp.lang.c++, where they deal with that heretical language, not here.
Nov 14 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.