CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Error Linking

  1. #1
    Join Date
    Oct 2008
    Posts
    6

    Error Linking

    Hi,

    I'm receiving a linking error, which says my symbols are multiply defined.

    These symbols are defined in "one.hpp"

    My "main.cpp" includes "two.h", and in "two.h" I have included "one.hpp".

    The functions defined in "one.hpp" are used in both "main.cpp" and "two.cpp"

    What am I getting these errors? Also all my .h files are wrapped with #ifndef etc.

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Error Linking

    Are all #ifndef's properly made? If you're using Visual Studio you can try putting a #pragma once at first line in all headers. If this helps something is wrong with the #ifndef's
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Error Linking

    Neither #pragma once nor include guards (#ifndefs) can protect you against linker errors! They're only useful during the compile, not the link.

    In general, non-template functions and variables cannot be defined in a header file or else you're likely to end up with this error. You should put them in a cpp file, and if necessary in the case of global variables, extern-declare them in the header.

    There are a few other options as well. Declaring functions inline usually allows them to be placed in the header. Declaring variables static works the same way (but may be misleading, because each translation unit will have a different version of the "global" variable!). Singleton classes, classes with static members, etc are also options.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured