Linker errors when compiling

This is a discussion on Linker errors when compiling within the C++ Programming forums, part of the General Programming Boards category; Hi I'm writing a basic game, but when I try to compile it I get ~80 linker errors. Those linker ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92

    Linker errors when compiling

    Hi
    I'm writing a basic game, but when I try to compile it I get ~80 linker errors. Those linker errors are caused by some symbols that I include in a standard header file and gets redifend all the time.
    I've tried to stop this by writing
    Code:
    #pragma once
    and the normal
    Code:
    #ifndef _HEADER_FILE 
    #define...
    and rebuilt the whole thing 4 times but nothing seems to work.

    This is an example of a linker error:
    Code:
    CGameApp.obj : error LNK2005: "struct D3DXCOLOR g_Black" (?g_Black@@3UD3DXCOLOR@@A) already defined in main.obj
    All are quite similar. Everyone is about a global variable that I define in the header.
    Would apreciate some help with this.

    Thanks

    Daniel

  2. #2
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Post the header... in full.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    Here is the code:
    Code:
    #pragma once
    #ifndef _STD_INC_H_
    #define _STD_INC_H_
    
    #include <wchar.h>
    #include <windows.h>
    #include <list>
    #include <map>
    #include <vector>
    
    
    #include "math.h"
    #include "optional.h"
    
    #include <d3d9types.h>
    #include <d3dx9.h>
    
    #include "DXUT\Common\DXUT.h"
    #include "DXUT\Common\DXUTmisc.h"
    #include <boost\config.hpp>
    #include <boost\shared_ptr.hpp>
    
    using boost::shared_ptr;
    #ifdef _DEBUG
    #define W_NEW new (_NORMAL_BLOCK, __FILE__, __LINE__)
    
    #else
    
    #define W_NEW new
    #define WIN32_LEAN_AND_MEAN
    #define VC_LEANMEAN
    #endif
    
    const double SIXTYHZ = 1000.0f/60.0f;
    
    typedef unsigned int ActorId;
    
    struct AppMsg
    {
    	HWND m_Hwnd;
    	UINT m_uMsg;
    	WPARAM m_wParam;
    	LPARAM m_lParam;
    };
    
    
    enum AlphaType
    {
    	AlphaOpaque,
    	AlphaTexture,
    	AlphaMaterial,
    	AlphaVertex
    };
    typedef D3DXCOLOR Color;
    
    class Material
    {
    private:
    	D3DMATERIAL9 m_Material;
    public:
    	Material();
    	~Material() {}
    	void SetAmbient(const Color &color);
    	void SetDiffuse(const Color &color);
    	void SetSpecular(const Color &color, const float power);
    	void SetEmissive(const Color &color);
    	void Set(const Color &color);
    	void SetAlpha(const float alpha);
    	bool HasAlpha() const;
    	float GetAlpha() const;
    	void Use();
    };
    
    class SceneNodeProperties
    {
    	friend class SceneNode;
    protected:
    	optional<ActorId>	m_ActorId;
    	std::string			m_Name;
    	mat4x4				m_ToWorld, m_FromWorld;
    	float				m_Radius;
    	Material			m_Material;
    	AlphaType			m_AlphaType;
    };
    
    #include "texture.h"
    #include "SceneNode.h"
    
    #define fOPAQUE (1.0f)
    #define fTRANSPARENT (0.0f)
    
    class ISceneNode;
    typedef std::vector<shared_ptr<ISceneNode>> SceneNodeList;
    
    //Those constants is getting redifined
    vec3 g_Up(0, 1, 0);
    vec3 g_Forward(0, 0, 1);
    vec3 g_Right(1, 0, 0);
    
    Color g_White(1.0f, 1.0f, 1.0f, fOPAQUE);
    Color g_Black(0.0f, 0.0f, 0.0f, fOPAQUE);
    Color g_Red(1.0f, 0.0f, 0.0f, fOPAQUE);
    Color g_Green(0.0f, 1.0f, 0.0f, fOPAQUE);
    Color g_Blue(0.0f, 0.0f, 1.0f, fOPAQUE);
    Color g_Grey50(0.5f, 0.5f, 0.5f, fOPAQUE);
    Color g_Transparent(0.0f, 0.0f, 0.0f, fTRANSPARENT);
    
    #include "core_interfaces.h"
    #endif
    Some of the pieces is inspired by the book: Game Coding Complete by Mike McShaffry

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    These should be in ONE source file only
    Code:
    //Those constants is getting redifined
    vec3 g_Up(0, 1, 0);
    vec3 g_Forward(0, 0, 1);
    vec3 g_Right(1, 0, 0);
    
    Color g_White(1.0f, 1.0f, 1.0f, fOPAQUE);
    Color g_Black(0.0f, 0.0f, 0.0f, fOPAQUE);
    Color g_Red(1.0f, 0.0f, 0.0f, fOPAQUE);
    Color g_Green(0.0f, 1.0f, 0.0f, fOPAQUE);
    Color g_Blue(0.0f, 0.0f, 1.0f, fOPAQUE);
    Color g_Grey50(0.5f, 0.5f, 0.5f, fOPAQUE);
    Color g_Transparent(0.0f, 0.0f, 0.0f, fTRANSPARENT);
    Things like #pragma once and include guards only work on the single source file. Include them in another source file and it all starts all over again.

    Look at your error message
    CGameApp.obj : ... already defined in main.obj
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    Ahaa, I see. Thanks for the help I'll include them in main or something and make them external there. Again, thanks a lot for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linker error when compiling Qt program
    By shashidhara_01 in forum C Programming
    Replies: 2
    Last Post: 04-29-2008, 09:30 AM
  2. Replies: 4
    Last Post: 03-10-2008, 07:03 AM
  3. Linker Errors
    By beene in forum Game Programming
    Replies: 6
    Last Post: 11-18-2006, 11:09 AM
  4. Dev C++/mySQL linker errors
    By WDT in forum C Programming
    Replies: 2
    Last Post: 03-30-2004, 02:40 PM
  5. MSVis-Studio C++ libraries and linker errors
    By kellydj in forum Windows Programming
    Replies: 10
    Last Post: 03-12-2002, 01:03 PM

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