How to resolve error LNK2005 - function already defined

Discussion in 'C Programming' started by Angus Comber, Jan 8, 2005.

  1. Angus Comber

    Angus Comber Guest

    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
     
    Angus Comber, Jan 8, 2005
    #1
    1. Advertising

  2. 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.
     
    Martin Ambuhl, Jan 8, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon
    Replies:
    3
    Views:
    11,984
  2. ivan

    Visual Studio 2003 linker error LNK2005

    ivan, Jul 18, 2006, in forum: C Programming
    Replies:
    2
    Views:
    7,027
    pavan_2804
    Aug 4, 2006
  3. Replies:
    1
    Views:
    2,696
    =?ISO-8859-1?Q?Stefan_N=E4we?=
    Jan 9, 2006
  4. fcvcnet
    Replies:
    6
    Views:
    19,358
    fcvcnet
    Mar 13, 2007
  5. Replies:
    1
    Views:
    10,180
Loading...

Share This Page