Sign in to follow this  
simbawoomba

error LNK2005

Recommended Posts

simbawoomba    122
Hi i am having a problem with a link error. i am splitting my main file into namespace files. basically i keep getting this error: Error 2 error LNK2005: "float logic::scale" (?scale@logic@@3MA) already defined in main.obj logic.obj amongst others which are the same for other floats/bools etc in my file I am having trouble identifying what is causing it so here is all of my files main.cpp:
// include the basic windows header files and the Direct3D header file
#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
#include <d3dx9.h>

// include the Direct3D Library files
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")

#include "logic.h" 
#include "graphics.h"


/***************
SCREEN RESOLUTION
***************/
#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768


/***************
INPUT
***************/
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)


// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);


// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    HWND hWnd;
    WNDCLASSEX wc;

    ZeroMemory(&wc, sizeof(WNDCLASSEX));

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC)WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.lpszClassName = L"WindowClass";

    RegisterClassEx(&wc);

    hWnd = CreateWindowEx(NULL, L"WindowClass", L"Our Direct3D Program",
                          WS_EX_TOPMOST | WS_POPUP, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
                          NULL, NULL, hInstance, NULL);

    ShowWindow(hWnd, nCmdShow);

    // set up and initialize Direct3D
	graphics::initD3D(hWnd);

    // enter the main loop:

    MSG msg;

    while(TRUE)
    {
        DWORD starting_point = GetTickCount();

        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            if (msg.message == WM_QUIT)
                break;

            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }


		/***************
		DRAW THE FRAME
		***************/
		graphics::render_frame();



		/***************
		GET INPUT
		***************/

        // check the 'escape' key
		if(KEY_DOWN(VK_ESCAPE)){
            PostMessage(hWnd, WM_DESTROY, 0, 0);
		}
		if(KEY_DOWN(VK_SPACE)){
			if (logic::lightcanchange){
				logic::light2on = !logic::light2on;
				logic::lightcanchange = false;
			}
		}
		if(KEY_UP(VK_SPACE)){
			logic::lightcanchange = true;
		}

		if(KEY_DOWN(VK_UP)){
			logic::scale+=0.1f;
		}

		if(KEY_DOWN(VK_DOWN)){
			if (logic::scale>0){
				logic::scale-=0.1f;
			}
		}



        while ((GetTickCount() - starting_point) < 25);
    }

    // clean up DirectX and COM
	graphics::cleanD3D();

    return msg.wParam;
}


// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
        case WM_DESTROY:
            {
                PostQuitMessage(0);
                return 0;
            } break;
    }

    return DefWindowProc (hWnd, message, wParam, lParam);
}



graphics.h
#ifndef GRAPHICS_H
#define GRAPHICS_H

#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
#include <d3dx9.h>



// include the Direct3D Library files
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")

namespace graphics{

	/***************
	SCREEN RESOLUTION
	***************/
	#define SCREEN_WIDTH 1024
	#define SCREEN_HEIGHT 768


	/***************
	My Custom Vertex Structure
	***************/
	struct CUSTOMVERTEX {FLOAT X, Y, Z; D3DVECTOR NORMAL; FLOAT U, V;};
	#define CUSTOMFVF (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)

	void initD3D(HWND hWnd);
	void init(void);
	void render_frame(void);
	void cleanD3D(void);

	void init_light(void);

}

#endif 



graphics.cpp:
#include "graphics.h"


#include "arithmatic.h"
#include "logic.h"
	

	LPDIRECT3D9 d3d;    // the pointer to our Direct3D interface
	LPDIRECT3DDEVICE9 d3ddev;    // the pointer to the device class
	LPDIRECT3DVERTEXBUFFER9 t_buffer = NULL;    // the pointer to the vertex buffer

	LPDIRECT3DTEXTURE9 texture_1;    // our first texture

void graphics::initD3D(HWND hWnd){
	d3d = Direct3DCreate9(D3D_SDK_VERSION);

    D3DPRESENT_PARAMETERS d3dpp;

    ZeroMemory(&d3dpp, sizeof(d3dpp));
    d3dpp.Windowed = FALSE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.hDeviceWindow = hWnd;
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
    d3dpp.BackBufferWidth = SCREEN_WIDTH;
    d3dpp.BackBufferHeight = SCREEN_HEIGHT;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

    // create a device class using this information and the info from the d3dpp stuct
    d3d->CreateDevice(D3DADAPTER_DEFAULT,
                      D3DDEVTYPE_HAL,
                      hWnd,
                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                      &d3dpp,
                      &d3ddev);

    init();    // call the function to initialize the cube
    init_light();    // call the function to initialize the light and material


	d3ddev->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);//automatically fix normal size
    d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE);    // turn on the 3D lighting
    d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);    // turn on the z-buffer
    d3ddev->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50));    // ambient light

    return;
}


void graphics::init(){

	// load the texture we will use
    D3DXCreateTextureFromFile(d3ddev,
                              L"textures/RMetal.png",
                              &texture_1);

    // create the vertices using the CUSTOMVERTEX struct
    CUSTOMVERTEX t_vert[] =
    {
      	// fuselage
		{ 3.0f, 0.0f, 0.0f,  normalX(3.0f, 0.0f, 0.0f), normalY(3.0f, 0.0f, 0.0f),normalZ(3.0f, 0.0f, 0.0f), 0,0,},
		{ 0.0f, 3.0f, -3.0f, normalX(0.0f, 3.0f, -3.0f), normalY(0.0f, 3.0f, -3.0f),normalZ(0.0f, 3.0f, -3.0f), 1,0,},
		{ 0.0f, 0.0f, 10.0f, normalX(0.0f, 0.0f, 10.0f), normalY(0.0f, 0.0f, 10.0f),normalZ(0.0f, 0.0f, 10.0f), 0,1,},
		{ -3.0f, 0.0f, 0.0f, normalX(-3.0f, 0.0f, 0.0f), normalY(-3.0f, 0.0f, 0.0f),normalZ(-3.0f, 0.0f, 0.0f), 1,1,},

		// left gun
		{ 3.2f, -1.0f, -3.0f,  normalX(3.2f, -1.0f, -3.0f), normalY(3.2f, -1.0f, -3.0f),normalZ(3.2f, -1.0f, -3.0f), 0,0,},
		{ 3.2f, -1.0f, 11.0f, normalX(3.2f, -1.0f, 11.0f), normalY(3.2f, -1.0f, 11.0f),normalZ(3.2f, -1.0f, 11.0f), 1,0, },
		{ 2.0f, 1.0f, 2.0f, normalX(2.0f, 1.0f, 2.0f), normalY(2.0f, 1.0f, 2.0f),normalZ(2.0f, 1.0f, 2.0f), 0,1, },

		// right gun
		{ -3.2f, -1.0f, -3.0f, normalX(-3.2f, -1.0f, -3.0f), normalY(-3.2f, -1.0f, -3.0f),normalZ(-3.2f, -1.0f, -3.0f), 0,0,},
		{ -3.2f, -1.0f, 11.0f, normalX(-3.2f, -1.0f, 11.0f), normalY(-3.2f, -1.0f, 11.0f),normalZ(-3.2f, -1.0f, 11.0f), 1,0, },
		{ -2.0f, 1.0f, 2.0f, normalX(-2.0f, 1.0f, 2.0f), normalY(-2.0f, 1.0f, 2.0f),normalZ(-2.0f, 1.0f, 2.0f), 0,1, },

		// bottom of fuselage
		{ 0.0f, 3.0f, -3.0f, normalX(0.0f, 3.0f, -3.0f), normalY(0.0f, 3.0f, -3.0f),normalZ(0.0f, 3.0f, -3.0f), 0,0, },
		{ 3.0f, 0.0f, 0.0f, normalX(3.0f, 0.0f, 0.0f), normalY(3.0f, 0.0f, 0.0f),normalZ(3.0f, 0.0f, 0.0f), 1,0, },
		{ -3.0f, 0.0f, 0.0f, normalX(-3.0f, 0.0f, 0.0f), normalY(-3.0f, 0.0f, 0.0f),normalZ(-3.0f, 0.0f, 0.0f), 0,1, },
		{ 0.0f, 0.0f, 10.0f, normalX(0.0f, 0.0f, 10.0f), normalY(0.0f, 0.0f, 10.0f),normalZ(0.0f, 0.0f, 10.0f), 1,1, },

		// opposite side of left gun
		{ 3.2f, -1.0f, -3.0f, normalX(3.2f, -1.0f, -3.0f), normalY(3.2f, -1.0f, -3.0f),normalZ(3.2f, -1.0f, -3.0f), 0,0, },
		{ 2.0f, 1.0f, 2.0f, normalX(2.0f, 1.0f, 2.0f), normalY(2.0f, 1.0f, 2.0f),normalZ(2.0f, 1.0f, 2.0f),1,0, },
		{ 3.2f, -1.0f, 11.0f, normalX(3.2f, -1.0f, 11.0f), normalY(3.2f, -1.0f, 11.0f),normalZ(3.2f, -1.0f, 11.0f), 0,1, },

		// opposite side of right gun
		{ -3.2f, -1.0f, -3.0f, normalX(-3.2f, -1.0f, -3.0f), normalY(-3.2f, -1.0f, -3.0f),normalZ(-3.2f, -1.0f, -3.0f), 0,0,},
		{ -2.0f, 1.0f, 2.0f, normalX(-2.0f, 1.0f, 2.0f), normalY(-2.0f, 1.0f, 2.0f),normalZ(-2.0f, 1.0f, 2.0f),1,0, },
		{ -3.2f, -1.0f, 11.0f, normalX(-3.2f, -1.0f, 11.0f), normalY(-3.2f, -1.0f, 11.0f),normalZ(-3.2f, -1.0f, 11.0f), 0,1,},
    };  

    // create a vertex buffer interface called t_buffer
    d3ddev->CreateVertexBuffer(20*sizeof(CUSTOMVERTEX),
                               0,
                               CUSTOMFVF,
                               D3DPOOL_MANAGED,
                               &t_buffer,
                               NULL);

    VOID* pVoid;    // a void pointer

    // lock t_buffer and load the vertices into it
    t_buffer->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, t_vert, sizeof(t_vert));
    t_buffer->Unlock();

    return;
}
void graphics::render_frame(){

	d3ddev->LightEnable(1, logic::light2on);    // turn on/off light 2
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

    d3ddev->BeginScene();

    // select which vertex format we are using
    d3ddev->SetFVF(CUSTOMFVF);

    // set the view transform
    D3DXMATRIX matView;    // the view transform matrix
    D3DXMatrixLookAtLH(&matView,
    &D3DXVECTOR3 (0.0f, 8.0f, 25.0f),    // the camera position
    &D3DXVECTOR3 (0.0f, 0.0f, 0.0f),      // the look-at position
    &D3DXVECTOR3 (0.0f, 1.0f, 0.0f));    // the up direction
    d3ddev->SetTransform(D3DTS_VIEW, &matView);

    // set the projection transform
    D3DXMATRIX matProjection;    // the projection transform matrix
    D3DXMatrixPerspectiveFovLH(&matProjection,
                               D3DXToRadian(45),    // the horizontal field of view
                               (FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT, // aspect ratio
                               1.0f,    // the near view-plane
                               100.0f);    // the far view-plane
    d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);

    // set the world transform
    static float index = 0.0f; index+=0.03f;    // an ever-increasing float value
    D3DXMATRIX matRotateY;    // a matrix to store the rotation for each triangle
    D3DXMatrixRotationY(&matRotateY, index);    // the rotation matrix

	D3DXMATRIX matScale;    // a matrix to store the scaling information

	// build a matrix to double the size of the model
	// store it to matScale
	D3DXMatrixScaling(&matScale, logic::scale, logic::scale, logic::scale);

    d3ddev->SetTransform(D3DTS_WORLD, &(matRotateY*matScale));    // set the world transform

    // select the vertex buffer to display
    d3ddev->SetStreamSource(0, t_buffer, 0, sizeof(CUSTOMVERTEX));

    // set the texture
    d3ddev->SetTexture(0, texture_1);

    d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);    // fuselage
	d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 4, 1);    // left gun
	d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 7, 1);    // right gun
	d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 10, 2);    // bottom of fuselage
	d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 14, 1);    // other side of left gun
	d3ddev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 17, 1);    // other side of right gun

    d3ddev->EndScene(); 

    d3ddev->Present(NULL, NULL, NULL, NULL);

    return;
}

void init_light(void)
{
    D3DLIGHT9 light[2];    // create the light struct
    D3DMATERIAL9 material;    // create the material struct

    ZeroMemory(&light[0], sizeof(light[0]));    // clear out the struct for use
    light[0].Type = D3DLIGHT_DIRECTIONAL;    // make the light type 'directional light'
    light[0].Diffuse.r = 0.9f;    // .5 red
    light[0].Diffuse.g = 0.9f;    // .5 green
    light[0].Diffuse.b = 0.9f;    // .5 blue
    light[0].Diffuse.a = 1.0f;    // full alpha (we'll get to that soon)

	ZeroMemory(&light[1], sizeof(light[1]));    // clear out the struct for use
    light[1].Type = D3DLIGHT_DIRECTIONAL;    // make the light type 'directional light'
    light[1].Diffuse.r = 0.3f;    // .5 red
    light[1].Diffuse.g = 0.3f;    // .5 green
    light[1].Diffuse.b = 0.9f;    // .5 blue
    light[1].Diffuse.a = 1.0f;    // full alpha (we'll get to that soon)

    D3DVECTOR vecDirection1 = {-1.0f, -0.3f, -1.0f};    // the direction of the light
    light[0].Direction = vecDirection1;    // set the direction

	D3DVECTOR vecDirection2 = {1.0f, -0.3f, -1.0f};    // the direction of the light
	light[1].Direction = vecDirection2;    // set the direction

    d3ddev->SetLight(0, &light[0]);    // send the light struct properties to light #0
    d3ddev->LightEnable(0, TRUE);    // turn on light #0

	d3ddev->SetLight(1, &light[1]);    // send the light struct properties to light #0
	d3ddev->LightEnable(1, logic::light2on);    // turn on light #0

    ZeroMemory(&material, sizeof(D3DMATERIAL9));    // clear out the struct for use
    material.Diffuse.r = material.Ambient.r = 1.0f;    // set the material to full red
    material.Diffuse.g = material.Ambient.g = 1.0f;    // set the material to full green
    material.Diffuse.b = material.Ambient.b = 1.0f;    // set the material to full blue
    material.Diffuse.a = material.Ambient.a = 1.0f;    // set the material to full alpha

    d3ddev->SetMaterial(&material);    // set the globably-used material to &material

    return;
}

void graphics::cleanD3D(void)
{
    t_buffer->Release();    // close and release the vertex buffer
	texture_1->Release();    // close and release the texture
    d3ddev->Release();    // close and release the 3D device
    d3d->Release();    // close and release Direct3D

    return;
}



logic.h:
#ifndef LOGIC_H
#define LOGIC_H

namespace logic{

		/***************
		Global Variables for Lighting
		***************/
		bool light2on;
		bool lightcanchange;

		/***************
		Global Variable for scale of the ship
		***************/
		float scale;

}


#endif 


logic.cpp
#include "logic.h"



bool light2on = true;
bool lightcanchange = true;
float scale = 1.0f;


PLEASE HELP ASAP Thankyou There sorry about the dodgey looking code before [Edited by - simbawoomba on May 10, 2007 10:35:36 AM]

Share this post


Link to post
Share on other sites
simbawoomba    122
i have found and fixed the problem

basically i had to set the variables to be externals in the logic.h file then declare them in the logic.cpp file like this

logic.h

#ifndef LOGIC_H
#define LOGIC_H

namespace logic{

/***************
Global Variables for Lighting
***************/

extern bool light2on;
extern bool lightcanchange;

/***************
Global Variable for scale of the ship
***************/

extern float scale;

}


#endif



logic.cpp

#include "logic.h"

namespace logic{

bool light2on = true;
bool lightcanchange = true;
float scale = 1.0f;

}



I hope this will help anyone having the same problem

Share this post


Link to post
Share on other sites
simbawoomba    122
i have found and fixed the problem

basically i had to set the variables to be externals in the logic.h file then declare them in the logic.cpp file like this

logic.h

#ifndef LOGIC_H
#define LOGIC_H

namespace logic{

/***************
Global Variables for Lighting
***************/

extern bool light2on;
extern bool lightcanchange;

/***************
Global Variable for scale of the ship
***************/

extern float scale;

}


#endif



logic.cpp

#include "logic.h"

namespace logic{

bool light2on = true;
bool lightcanchange = true;
float scale = 1.0f;

}



I hope this will help anyone having the same problem

Share this post


Link to post
Share on other sites
Evil Steve    2017
Quote:
Original post by simbawoomba
to give you a little help i believe the problem lies in the logic.h file, i think it is due to the way that the declarations have been made.
Exactly. You're effectively declaring the light2on, lightcanchange and scale variables once in every file that includes logic.h. The correct way to use globals like that is to use the extern keyword. In the header, declare all 3 variables as extern (E.g. extern bool light2on;), and then implement them in ONE source file (logic.cpp makes sense) (E.g. bool logic::light2on = false;).

I think that's correct, I'm not entirely used to namespaces (Cue objects being hurled [smile])

EDIT: Too slow

Share this post


Link to post
Share on other sites
__ROB_D__    100
The Problem is that you are declaring the Globle Varialbe twice... This happens be cause you have declared it the in the Logic header file and every time you #include the file the compiler this you've delcared a new varialbe...

To Solve this change

float Scale

to

extern float scale

in the Logic.h file,
and update to logic.cpp to the float Scale is in the namespace or you'll get an Error.

Hope that helped

Rob:-D

EDIT : Everyone Fixed this problem befor my reply when up!! :-) But there all basically the same solution!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this