Thread: Linker Errors

  1. #1
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788

    Linker Errors

    --> x already defined in y.obj

    I've just about had it with this problem. How the hell do I fix this?

    I have a main.h file with all function defs, globals, macros etc.

    Then I have many .cpp files which include main.h.

    I took this type of file layout from CDX's code, and problem is, their code links, mine doesn't.

    There's virtually no difference between their code, and mine, in terms of file layout.

    I'm using VS .NET

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > x already defined in y.obj
    Means x is defined in a header file, and not merely declared in a header file (given your descriptions of the project)

    Or x is truly defined in two places by mistake
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787

    Re: Linker Errors

    Originally posted by The Dog
    Then I have many .cpp files which include main.h.
    did you try doing this:
    Code:
    #ifndef _MAIN_H
    #define _MAIN_H
    
    //main.h goes in here
    
    #endif //_MAIN_H
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Even with include guards I get the errors.

    Let's say my main.h looks like this:
    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <vector>
    
    using std::vector;
    
    //Define macros here
    #define PI  3.141592654
    //End Define macros
    
    //Globals here
    #ifdef MAIN_CPP
       #define DECLARE
    #else
       #define DECLARE extern
    #endif
    
    DECLARE int globalVariable;
    DECLARE char globalString;
    //End Globals
    
    //All function prototyes
    int WINAPI WinMain(...);
    LRESULT CALLBACK WndProc(...);
    
    int function();
    void function_2();
    //End Function Prototypes
    Now, in main.cpp, i'll have something like this:
    Code:
    #define MAIN_CPP
    #include "main.h"
    
    int WINAPI WinMain(...)
    {
    }
    
    LRESULT CALLBACK WndProc(...)
    {
    }
    Then I have about six other .cpp files which include main.h


    One other thing:
    If project A was originally built with VS 6, and project B is originally built with VS .NET, would there be any difference between the two projects if the VS 6 project was converted to a VS .NET project?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linker errors
    By jw232 in forum C++ Programming
    Replies: 3
    Last Post: 06-12-2009, 12:56 PM
  2. Linker errors in VC++ 2005
    By C+/- in forum C++ Programming
    Replies: 0
    Last Post: 05-18-2007, 07:42 AM
  3. Sneaky little linker errors...
    By Tozar in forum C++ Programming
    Replies: 8
    Last Post: 10-25-2006, 05:40 AM
  4. why am i getting linker errors?
    By dantestwin in forum C++ Programming
    Replies: 11
    Last Post: 07-08-2004, 10:34 AM
  5. Linker errors with Visual C++
    By codegirl in forum C++ Programming
    Replies: 4
    Last Post: 09-11-2003, 09:20 AM