Thread: Linking problems in Visual Studio

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    Linking problems in Visual Studio

    Hallo,

    I have been stuggeling for a while now to get my project to work. I tried to add my Vector2D class to one of the others, but that turned out to create a lot of unforeseen problems.

    I have #ifndef in front of all by header files, but this still happens. Any ideas on what I am doing wrong?

    Here is what the evil compiler screams at me:
    >Linking...
    1>Object.obj : error LNK2005: "int __cdecl Dot(class Vector2D,class Vector2D)" (?Dot@@YAHVVector2D@@0@Z) already defined in BulletMgr.obj
    1>Object.obj : error LNK2005: "public: __thiscall BoundingBox::BoundingBox(int,int,int,int)" (??0BoundingBox@@QAE@HHHH@Z) already defined in BulletMgr.obj
    1>Object.obj : error LNK2005: "public: __thiscall BoundingBox::BoundingBox(void)" (??0BoundingBox@@QAE@XZ) already defined in BulletMgr.obj
    1>Object.obj : error LNK2005: "public: int __thiscall BoundingBox::getWidth(void)" (?getWidth@BoundingBox@@QAEHXZ) already defined in BulletMgr.obj
    1>Object.obj : error LNK2005: "public: int __thiscall BoundingBox::getHeight(void)" (?getHeight@BoundingBox@@QAEHXZ) already defined in BulletMgr.obj
    1>Object.obj : error LNK2005: "public: int __thiscall BoundingBox::getXpos(void)" (?getXpos@BoundingBox@@QAEHXZ) already defined in BulletMgr.obj
    1>Object.obj : error LNK2005: "public: int __thiscall BoundingBox::getYpos(void)" (?getYpos@BoundingBox@@QAEHXZ) already defined in BulletMgr.obj
    1>Object.obj : error LNK2005: "public: struct vertex __thiscall BoundingBox::getVertex(int)" (?getVertex@BoundingBox@@QAE?AUvertex@@H@Z) already defined in BulletMgr.obj
    1>Object.obj : error LNK2005: "public: void __thiscall BoundingBox::rotate(int)" (?rotate@BoundingBox@@QAEXH@Z) already defined in BulletMgr.obj
    1>Object.obj : error LNK2005: "public: void __thiscall BoundingBox::move(int,int)" (?move@BoundingBox@@QAEXHH@Z) already defined in BulletMgr.obj
    1>Object.obj : error LNK2005: "public: class BoundingBox __thiscall BoundingBox::allignToAxis(void)" (?allignToAxis@BoundingBox@@QAE?AV1@XZ) already defined in BulletMgr.obj
    1>Object.obj : error LNK2005: "public: bool __thiscall BoundingBox:ointIntersection(struct vertex &)" (?pointIntersection@BoundingBox@@QAE_NAAUvertex@@@ Z) already defined in BulletMgr.obj
    1>Object.obj : error LNK2005: "public: class Rectangle2D __thiscall BoundingBox::convertToRec(void)" (?convertToRec@BoundingBox@@QAE?AVRectangle2D@@XZ) already defined in BulletMgr.obj
    1>Sprite.obj : error LNK2005: "int __cdecl Dot(class Vector2D,class Vector2D)" (?Dot@@YAHVVector2D@@0@Z) already defined in BulletMgr.obj
    1>Sprite.obj : error LNK2005: "public: __thiscall BoundingBox::BoundingBox(int,int,int,int)" (??0BoundingBox@@QAE@HHHH@Z) already defined in BulletMgr.obj
    1>Sprite.obj : error LNK2005: "public: __thiscall BoundingBox::BoundingBox(void)" (??0BoundingBox@@QAE@XZ) already defined in BulletMgr.obj
    1>Sprite.obj : error LNK2005: "public: int __thiscall BoundingBox::getWidth(void)" (?getWidth@BoundingBox@@QAEHXZ) already defined in BulletMgr.obj
    1>Sprite.obj : error LNK2005: "public: int __thiscall BoundingBox::getHeight(void)" (?getHeight@BoundingBox@@QAEHXZ) already defined in BulletMgr.obj
    1>Sprite.obj : error LNK2005: "public: int __thiscall BoundingBox::getXpos(void)" (?getXpos@BoundingBox@@QAEHXZ) already defined in BulletMgr.obj
    1>Sprite.obj : error LNK2005: "public: int __thiscall BoundingBox::getYpos(void)" (?getYpos@BoundingBox@@QAEHXZ) already defined in BulletMgr.obj
    1>Sprite.obj : error LNK2005: "public: struct vertex __thiscall BoundingBox::getVertex(int)" (?getVertex@BoundingBox@@QAE?AUvertex@@H@Z) already defined in BulletMgr.obj
    1>Sprite.obj : error LNK2005: "public: void __thiscall BoundingBox::rotate(int)" (?rotate@BoundingBox@@QAEXH@Z) already defined in BulletMgr.obj
    1>Sprite.obj : error LNK2005: "public: void __thiscall BoundingBox::move(int,int)" (?move@BoundingBox@@QAEXHH@Z) already defined in BulletMgr.obj
    1>Sprite.obj : error LNK2005: "public: class BoundingBox __thiscall BoundingBox::allignToAxis(void)" (?allignToAxis@BoundingBox@@QAE?AV1@XZ) already defined in BulletMgr.obj
    1>Sprite.obj : error LNK2005: "public: bool __thiscall BoundingBox:ointIntersection(struct vertex &)" (?pointIntersection@BoundingBox@@QAE_NAAUvertex@@@ Z) already defined in BulletMgr.obj
    1>Sprite.obj : error LNK2005: "public: class Rectangle2D __thiscall BoundingBox::convertToRec(void)" (?convertToRec@BoundingBox@@QAE?AVRectangle2D@@XZ) already defined in BulletMgr.obj
    1>Visualisation.obj : error LNK2005: "int __cdecl Dot(class Vector2D,class Vector2D)" (?Dot@@YAHVVector2D@@0@Z) already defined in BulletMgr.obj
    1>Visualisation.obj : error LNK2005: "public: __thiscall BoundingBox::BoundingBox(int,int,int,int)" (??0BoundingBox@@QAE@HHHH@Z) already defined in BulletMgr.obj
    1>Visualisation.obj : error LNK2005: "public: __thiscall BoundingBox::BoundingBox(void)" (??0BoundingBox@@QAE@XZ) already defined in BulletMgr.obj
    1>Visualisation.obj : error LNK2005: "public: int __thiscall BoundingBox::getWidth(void)" (?getWidth@BoundingBox@@QAEHXZ) already defined in BulletMgr.obj
    1>Visualisation.obj : error LNK2005: "public: int __thiscall BoundingBox::getHeight(void)" (?getHeight@BoundingBox@@QAEHXZ) already defined in BulletMgr.obj
    1>Visualisation.obj : error LNK2005: "public: int __thiscall BoundingBox::getXpos(void)" (?getXpos@BoundingBox@@QAEHXZ) already defined in BulletMgr.obj
    1>Visualisation.obj : error LNK2005: "public: int __thiscall BoundingBox::getYpos(void)" (?getYpos@BoundingBox@@QAEHXZ) already defined in BulletMgr.obj
    1>Visualisation.obj : error LNK2005: "public: struct vertex __thiscall BoundingBox::getVertex(int)" (?getVertex@BoundingBox@@QAE?AUvertex@@H@Z) already defined in BulletMgr.obj
    1>Visualisation.obj : error LNK2005: "public: void __thiscall BoundingBox::rotate(int)" (?rotate@BoundingBox@@QAEXH@Z) already defined in BulletMgr.obj
    1>Visualisation.obj : error LNK2005: "public: void __thiscall BoundingBox::move(int,int)" (?move@BoundingBox@@QAEXHH@Z) already defined in BulletMgr.obj
    1>Visualisation.obj : error LNK2005: "public: class BoundingBox __thiscall BoundingBox::allignToAxis(void)" (?allignToAxis@BoundingBox@@QAE?AV1@XZ) already defined in BulletMgr.obj
    1>Visualisation.obj : error LNK2005: "public: bool __thiscall BoundingBox:ointIntersection(struct vertex &)" (?pointIntersection@BoundingBox@@QAE_NAAUvertex@@@ Z) already defined in BulletMgr.obj
    1>Visualisation.obj : error LNK2005: "public: class Rectangle2D __thiscall BoundingBox::convertToRec(void)" (?convertToRec@BoundingBox@@QAE?AVRectangle2D@@XZ) already defined in BulletMgr.obj
    1>main.obj : error LNK2005: "int __cdecl Dot(class Vector2D,class Vector2D)" (?Dot@@YAHVVector2D@@0@Z) already defined in BulletMgr.obj
    1>main.obj : error LNK2005: "public: __thiscall BoundingBox::BoundingBox(int,int,int,int)" (??0BoundingBox@@QAE@HHHH@Z) already defined in BulletMgr.obj
    1>main.obj : error LNK2005: "public: __thiscall BoundingBox::BoundingBox(void)" (??0BoundingBox@@QAE@XZ) already defined in BulletMgr.obj
    1>main.obj : error LNK2005: "public: int __thiscall BoundingBox::getWidth(void)" (?getWidth@BoundingBox@@QAEHXZ) already defined in BulletMgr.obj
    1>main.obj : error LNK2005: "public: int __thiscall BoundingBox::getHeight(void)" (?getHeight@BoundingBox@@QAEHXZ) already defined in BulletMgr.obj
    1>main.obj : error LNK2005: "public: int __thiscall BoundingBox::getXpos(void)" (?getXpos@BoundingBox@@QAEHXZ) already defined in BulletMgr.obj
    1>main.obj : error LNK2005: "public: int __thiscall BoundingBox::getYpos(void)" (?getYpos@BoundingBox@@QAEHXZ) already defined in BulletMgr.obj
    1>main.obj : error LNK2005: "public: struct vertex __thiscall BoundingBox::getVertex(int)" (?getVertex@BoundingBox@@QAE?AUvertex@@H@Z) already defined in BulletMgr.obj
    1>main.obj : error LNK2005: "public: void __thiscall BoundingBox::rotate(int)" (?rotate@BoundingBox@@QAEXH@Z) already defined in BulletMgr.obj
    1>main.obj : error LNK2005: "public: void __thiscall BoundingBox::move(int,int)" (?move@BoundingBox@@QAEXHH@Z) already defined in BulletMgr.obj
    1>main.obj : error LNK2005: "public: class BoundingBox __thiscall BoundingBox::allignToAxis(void)" (?allignToAxis@BoundingBox@@QAE?AV1@XZ) already defined in BulletMgr.obj
    1>main.obj : error LNK2005: "public: bool __thiscall BoundingBox:ointIntersection(struct vertex &)" (?pointIntersection@BoundingBox@@QAE_NAAUvertex@@@ Z) already defined in BulletMgr.obj
    1>main.obj : error LNK2005: "public: class Rectangle2D __thiscall BoundingBox::convertToRec(void)" (?convertToRec@BoundingBox@@QAE?AVRectangle2D@@XZ) already defined in BulletMgr.obj
    1>Debug/HelloWorld.exe : fatal error LNK1169: one or more multiply defined symbols found

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Does the header file containing "BoundingBox" have include guards? If so, do they look like this?
    Code:
    #ifndef MY_HEADER_H
    #define MY_HEADER_H
    ...
    #endif
    Let's see what BoundingBox::getWidth() looks like in the header.

    gg

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest posting the smallest and simplest code that demonstrates the error. Look out for things like class templates: did you try and separate the class template definition from its implementation?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    These are double definition errors. The problem is probably that you didn't separate class definition from method implementation. In other words, I bet your header file is full of
    Code:
    return_type BoundingBox::methodName()
    {
      foo!
    }
    Alternatively, did you #include a .cpp file?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Thanks for the replies.

    CornedBee and LaserLight, you were right(as always) separating it into a .h and a .cpp file fixed the problem.

    EDIT:
    I used the same math class for several projects and it worked fine. Any ideas on why it blew up in my face now?
    Last edited by h3ro; 03-04-2008 at 02:32 PM.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    If you have a single source file project of some kind and #include another source file then your code should still link fine. The problem typically comes in when you have multiple source files (as you do) that all #include that source file. In this case, each individually compiled object file winds up with copies of the functions that were in the #included file. When the linker tries to combine all of these object files into a single executable, the matching bits of code cause the "already defined" problems you are seeing.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  2. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  3. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 10:41 PM
  4. Why?
    By gprime in forum C++ Programming
    Replies: 5
    Last Post: 06-13-2004, 04:44 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 05:27 AM