Variables already defined while linking.

This is a discussion on Variables already defined while linking. within the C++ Programming forums, part of the General Programming Boards category; I've not written C in quite a while, sorry if I'm looking over something obvious. Here is camera.h Code: float ...

  1. #1
    Registered User xconspirisist's Avatar
    Join Date
    Nov 2003
    Posts
    44

    Variables already defined while linking.

    I've not written C in quite a while, sorry if I'm looking over something obvious.

    Here is camera.h

    Code:
    float angle=0.0, deltaAngle = 0.0, ratio;
    float x=0.0f, y=1.75f, z=5.0f;
    float lx=0.0f, ly=0.0f, lz=-1.0f;
    
    int deltaMove = 0;
    Then I'm just using

    Code:
    #include "camera.h"
    in each file where I need those variables. However, during link, I get these errors:

    keys.obj : error LNK2005: "int deltaMove" (?deltaMove@@3HA) already defined in camera.obj
    keys.obj : error LNK2005: "float lz" (?lz@@3MA) already defined in camera.obj
    keys.obj : error LNK2005: "float ly" (?ly@@3MA) already defined in camera.obj
    keys.obj : error LNK2005: "float lx" (?lx@@3MA) already defined in camera.obj
    keys.obj : error LNK2005: "float z" (?z@@3MA) already defined in camera.obj
    keys.obj : error LNK2005: "float y" (?y@@3MA) already defined in camera.obj
    keys.obj : error LNK2005: "float x" (?x@@3MA) already defined in camera.obj
    keys.obj : error LNK2005: "float deltaAngle" (?deltaAngle@@3MA) already defined in camera.obj
    keys.obj : error LNK2005: "float angle" (?angle@@3MA) already defined in camera.obj
    keys.obj : error LNK2005: "float ratio" (?ratio@@3MA) already defined in camera.obj
    render.obj : error LNK2005: "int deltaMove" (?deltaMove@@3HA) already defined in camera.obj
    render.obj : error LNK2005: "float lz" (?lz@@3MA) already defined in camera.obj
    render.obj : error LNK2005: "float ly" (?ly@@3MA) already defined in camera.obj
    render.obj : error LNK2005: "float lx" (?lx@@3MA) already defined in camera.obj
    render.obj : error LNK2005: "float z" (?z@@3MA) already defined in camera.obj
    render.obj : error LNK2005: "float y" (?y@@3MA) already defined in camera.obj
    render.obj : error LNK2005: "float x" (?x@@3MA) already defined in camera.obj
    render.obj : error LNK2005: "float deltaAngle" (?deltaAngle@@3MA) already defined in camera.obj
    render.obj : error LNK2005: "float angle" (?angle@@3MA) already defined in camera.obj
    render.obj : error LNK2005: "float ratio" (?ratio@@3MA) already defined in camera.obj
    To surpress any confusion, camera.h is included in camera.cpp, keys.cpp and render.cpp

    Please help, I'm quite clueless to the issue. :/
    Last edited by xconspirisist; 06-10-2005 at 04:15 AM.

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Post all your include statements in all your files. List the include statements next to the file name in which they appear. Don't forget the file with main() in it.

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Variables should not be defined in a header file. Variables should only be defined once. They can be declared multiple times. C FAQ. Defining a variable sets aside space and optionally provides an initial value. Declaring a variable simply provides the compiler with the name and type of the variable. No space is set aside.

    Declare your variables in the header file like this:
    Code:
    extern float angle, deltaAngle, ratio;
    extern float x, y, z;
    extern float lx, ly, lz;
    
    extern int deltaMove;
    Then, in one cpp file, define your global variables:
    Code:
    float angle=0.0, deltaAngle = 0.0, ratio;
    float x=0.0f, y=1.75f, z=5.0f;
    float lx=0.0f, ly=0.0f, lz=-1.0f;
    
    int deltaMove = 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 12:02 PM
  2. Replies: 15
    Last Post: 09-30-2008, 03:12 AM
  3. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 04:25 AM
  5. Linking images to variables
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 10-12-2001, 01:07 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21