How to resolve error LNK2005 - function already defined

A

Angus Comber

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
(e-mail address removed)
 
M

Martin Ambuhl

Angus said:
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
where they deal with that heretical language, not here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top