That blasted LNK 2001 error again...

This is a discussion on That blasted LNK 2001 error again... within the C++ Programming forums, part of the General Programming Boards category; Hi Guys. This is a bit of a newbiee question, but ive googled for ages and still no luck. When ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Melbourne, Australia
    Posts
    11

    That blasted LNK 2001 error again...

    Hi Guys.

    This is a bit of a newbiee question, but ive googled for ages and still no luck. When I comile all my souce files it works fine, its when I link it that it stuffs up. It comes up with these errors.

    Code:
    winmain.obj : error LNK2001: unresolved external symbol "void __fastcall Game_Run(struct HWND__ *)" (?Game_Run@@YIXPAUHWND__@@@Z)
    winmain.obj : error LNK2001: unresolved external symbol "int __fastcall Game_Init(struct HWND__ *)" (?Game_Init@@YIHPAUHWND__@@@Z)
    Debug/Trans_Sprite.exe : fatal error LNK1120: 2 unresolved externals
    Error executing link.exe.
    The trouble is that I've tried almost everything. Dont know how to use DUMPBIN, ive tried
    Code:
    extern "C" ...
    etc but it wont work

    Heres my code

    Code:
    // Beging Game Programing
    // Chapter 7
    // winmain.cpp -- windows code framework
    
    
    
    // header files
    
    #include <d3d9.h>
    #include <d3dx9.h>
    #include <time.h>
    #include <stdio.h>
    #include "dxgraphics.h"
    #include "game.h"
    
    
    
    //windows event call back function
    LRESULT WINAPI WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
    	{
    		switch ( msg )
    
    		{
    		case WM_DESTROY:
    			//release the direct 3d device
    			if (d3ddev != NULL)
    				d3ddev->Release();
    
    			//release the direct 3d object
    			if (d3d != NULL)
    				d3d->Release();
    
    			//call the 'front end' shut-down function
    			Game_End(hWnd);
    
    			// tell windows to kill this program
    			PostQuitMessage(0);
    			return 0;
    		}
    		
    	return DefWindowProc (hWnd, msg, wParam, lParam);
     
    	}
    
    
    //helper function to set up the window properties
    ATOM MyRegisterClass(HINSTANCE hInstance)
    	{
    		//create the window class structure
    		WNDCLASSEX wc;
    		wc.cbSize = sizeof(WNDCLASSEX);
    
    		//fill the struct with info
    		wc.style = CS_HREDRAW | CS_VREDRAW;
    		wc.lpfnWndProc = (WNDPROC)WinProc;
    		wc.cbClsExtra = 0;
    		wc.cbWndExtra = 0;
    		wc.hInstance = hInstance;
    		wc.hIcon = NULL;
    		wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    		wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    		wc.lpszMenuName = NULL;
    		wc.lpszClassName = APPTITLE;
    		wc.hIconSm = NULL;
    
    //set up the window with the class info
    return RegisterClassEx(&wc);
    }
    
    
    //entry point for a windows program
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    
    {
    	MSG msg;
    	HWND hWnd;
    
    	//Register the class
    	MyRegisterClass(hInstance);
    
    	//set up the screen in windowed or full-screen mode?
    	DWORD style;
    	if (FULLSCREEN)
    		style = WS_EX_TOPMOST | WS_VISIBLE | WS_POPUP;
    	else
    		style = WS_OVERLAPPED;
    
    	//Create new window
    	hWnd = CreateWindow(
    		APPTITLE,				//window class
    		APPTITLE,				//title bar
    		style,					//window style
    		CW_USEDEFAULT,			//x position of window
    		CW_USEDEFAULT,			//y position of window
    		SCREEN_WIDTH,			//width of the window
    		SCREEN_HEIGHT,			//height of the window
    		NULL,					//parent window
    		NULL,					//menu
    		hInstance,				//application instance
    		NULL);					//window parameters
    
    	//Was there an error creating the window?
    	if (!hWnd)
    		return false;
    
    	//display the window
    	ShowWindow(hWnd, nCmdShow);
    	UpdateWindow(hWnd);
    
    	if (!Init_Direct3D(hWnd, SCREEN_WIDTH, SCREEN_HEIGHT, FULLSCREEN))
    		{
    		MessageBox(hWnd, "Error initializing Direct 3D", "Error", MB_OK);
    		return 0;
    		}
    	//initalize the game
    	if (!Game_Init(hWnd))
    		{
    			MessageBox(hWnd, "Error initializing the game", "Error", MB_OK);
    			return 0;
    		}
    
    	//main message loop
    	int done = 0;
    	
    	while (!done)
    		{
    			if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    			{
    				//look for quit message
    				if (msg.message == WM_QUIT)
    					done = 1;
    
    				//decode and pass messages on to WndProc
    				TranslateMessage(&msg);
    				DispatchMessage(&msg);
    			}
    
    			else
    				//process game loop ('else' prevents running after close)
    				Game_Run(hWnd);
    		}
    	return msg.wParam;
    
    }
    And the header file that has the prototypes for those functions

    Code:
    //game.h
    
    #ifndef _GAME_H
    #define _GAME_H
    
    #include <d3d9.h>
    #include <d3dx9.h>
    #include <d3dx9core.h>
    #include <d3dx9math.h>
    #include <time.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include "dxgraphics.h"
    
    //app title
    #define APPTITLE "Trans_Sprite"
    
    
    //screen setup
    #define FULLSCREEN 0		//0 = windowed, 1 = fullscreen
    #define SCREEN_WIDTH 640
    #define SCREEN_HEIGHT 480
    
    //macros to read the keyboard asynchronously
    #define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
    #define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
    
    //function prototypes
    
    int Game_Init(HWND);
    void Game_Run(HWND);
    void Game_End(HWND);
    
    //sprite structure
    typedef struct {
    	int x,y;
    	int width,height;
    	int movex,movey;
    	int curframe,lastframe;
    	int animdelay,animcount;
    	} SPRITE;
    
    #endif
    Any help would be appreciated

    Cheers
    Manaxter

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    You need to add game.cpp (or game.c) to your project file.

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Melbourne, Australia
    Posts
    11
    game.cpp.

    Do you mean to the link Object/Module thingo?
    Its alread in the project, its one of the source files!

    I added it to the Object/Module thingo and it now just says...

    Code:
    game.cpp : fatal error LNK1136: invalid or corrupt file
    Error executing link.exe.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    > Do you mean to the link Object/Module thingo?
    Yes, like that, but there's a similar one for adding source files as well.

    Listing a .cpp in the object files will get that error.

    > Its alread in the project, its one of the source files!
    Oh - I see.
    If you do a "clean" then "rebuild all", do you see both game.cpp and winmain.cpp being compiled?

  5. #5
    Registered User
    Join Date
    Jan 2006
    Location
    Melbourne, Australia
    Posts
    11
    Code:
    Deleting intermediate files and output files for project 'Trans_Sprite - Win32 Debug'.
    --------------------Configuration: Trans_Sprite - Win32 Debug--------------------
    Compiling...
    dxgraphics.cpp
    game.cpp
    winmain.cpp
    Linking...
    winmain.obj : error LNK2001: unresolved external symbol "void __cdecl Game_Run(struct HWND__ *)" (?Game_Run@@YAXPAUHWND__@@@Z)
    winmain.obj : error LNK2001: unresolved external symbol "int __cdecl Game_Init(struct HWND__ *)" (?Game_Init@@YAHPAUHWND__@@@Z)
    Debug/Trans_Sprite.exe : fatal error LNK1120: 2 unresolved externals
    Error executing link.exe.
    
    Trans_Sprite.exe - 3 error(s), 0 warning(s)
    That is what happens when i do a 'clean' then 'rebuild all'.

    So yes they are both being compiled...

  6. #6
    Registered User
    Join Date
    Jan 2006
    Location
    Melbourne, Australia
    Posts
    11
    Not that it may make any difference, but when I try and build it set to Release mode (as apose to debug mode) it comes up with this.

    Code:
    --------------------Configuration: Trans_Sprite - Win32 Release--------------------
    Compiling...
    dxgraphics.cpp
    game.cpp
    winmain.cpp
    Linking...
    dxgraphics.obj : error LNK2001: unresolved external symbol _Direct3DCreate9@4
    dxgraphics.obj : error LNK2001: unresolved external symbol _D3DXGetImageInfoFromFileA@8
    dxgraphics.obj : error LNK2001: unresolved external symbol _D3DXCreateTextureFromFileExA@56
    game.obj : error LNK2001: unresolved external symbol _D3DXCreateSprite@8
    winmain.obj : error LNK2001: unresolved external symbol "void __cdecl Game_Run(struct HWND__ *)" (?Game_Run@@YAXPAUHWND__@@@Z)
    winmain.obj : error LNK2001: unresolved external symbol "int __cdecl Game_Init(struct HWND__ *)" (?Game_Init@@YAHPAUHWND__@@@Z)
    Release/Trans_Sprite.exe : fatal error LNK1120: 6 unresolved externals
    Error executing link.exe.
    
    Trans_Sprite.exe - 7 error(s), 0 warning(s)
    It seems to have even more linking errors this way. Do i actually have to bring all the files into my project? Or can I just include them as I am doing?

    EDIT: Nope, dont worry, that didnt help. And sorry for the double post.

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    void Game_Run(HWND);
    void Game_End(HWND);
    Do you include game.h in game.cpp?

    Are you really sure that the declarations of your functions actually match the prototypes?

  8. #8
    Registered User
    Join Date
    Jan 2006
    Location
    Melbourne, Australia
    Posts
    11
    Code:
    int Game_Int(HWND hwnd)
    	{.......
    its the same for the others,
    Code:
    void Game_Run(HWND hwnd) { ....
    game.cpp does include game.h.

    game.cpp

    Code:
    // Game.cpp -- Trans_Sprite
    
    #include "game.h"
    
    LPDIRECT3DTEXTURE9 kitty_image[7];
    SPRITE kitty;
    LPDIRECT3DSURFACE9 back;
    LPD3DXSPRITE sprite_handler;
    
    HRESULT result;
    
    //timing variable
    long start = GetTickCount();
    
    
    //////////////////////////////////////////////
    //                GAME INIT                 //
    //////////////////////////////////////////////
    
    //initalizes the game
    int Game_Int(HWND hwnd)
    	{
    		char s[20];
    		int n;
    
    		//set random seed number
    		srand(time(NULL));
    
    		//create sprite hadler object
    		result = D3DXCreateSprite(d3ddev, &sprite_handler);
    		if (result != D3D_OK)
    			return 0;
    
    		//Load the sprite animation
    		for (n=0; n<6; n++)
    			{
    				//set up the filename
    				sprintf(s, "cat%d.bmp", n+1);
    
    				//load the texture with the pink as the transparency
    				kitty_image[n] = LoadTexture(s, D3DCOLOR_XRGB(255,0,255));
    				if (kitty_image[n] == NULL)
    					return 0;
    			}
    
    		//load the background image
    		back = LoadSurface("background.bmp", NULL);
    
    		//initialize the sprites properties
    		kitty.x = 100;
    		kitty.y = 150;
    		kitty.width = 96;
    		kitty.height = 96;
    		kitty.curframe = 0;
    		kitty.lastframe = 5;
    		kitty.animdelay = 2;
    		kitty.animcount = 0;
    		kitty.movex = 5;
    		kitty.movey = 0;
    
    		//return ok
    		return 1;
    	}
    
    
    
    
    //////////////////////////////////////////////
    //                GAME RUN                  //
    //////////////////////////////////////////////
    
    
    // the main game loop
    void Gane_Run(HWND hwnd)
    	{
    		//make sure the direct3d device is valid
    		if (d3ddev == NULL)
    			return;
    
    		//after a short delay, ready for next frame?
    		// this keeps game running at a steady frame rate
    		if (GetTickCount() - start >= 30)
    			
    			{
    				//reset the timing
    				start = GetTickCount();
    
    				//move the sprite
    				kitty.x += kitty.movex;
    				kitty.y += kitty.movey;
    
    				//"warp" the sprite at the edges of screen
    				if (kitty.x > SCREEN_WIDTH - kitty.width)
    					kitty.x = 0;
    
    				if (kitty.x < 0)
    					kitty.x = SCREEN_WIDTH - kitty.width;
    
    				//has animation reached its end?
    				if (++kitty.animcount > kitty.animdelay)
    					{
    						//reset kitty counter
    						kitty.animcount = 0;
    
    						//animate the sprite to its start
    						if (++kitty.curframe > kitty.lastframe)
    							kitty.curframe = 0;
    					}
    			}
    
    		// start rendering
    		if (d3ddev->BeginScene())
    			{
    				//erase the entire background
    				d3ddev->StretchRect(back, NULL, backbuffer, NULL, D3DTEXF_NONE);
    
    				//start sprite handler
    				sprite_handler->Begin();
    
    				//create vector to update sprite possition (float in C++ form, not C)
    				D3DXVECTOR2 position((float)kitty.x, (float)kitty.y);
    
    				// draw the sprite
    				sprite_handler->Draw(
    					kitty_image[kitty.curframe],
    					NULL,
    					NULL,
    					NULL,
    					0,
    					&position,
    					D3DCOLOR_XRGB(255,255,255));
    
    				//stop drawing
    				sprite_handler->End();
    
    				//stop rendering
    				d3ddev->EndScene();
    			}
    
    		//display the back buffer on the screen
    		d3ddev->Present(NULL, NULL, NULL, NULL);
    
    		//check for the escape key
    		if (KEY_DOWN(VK_ESCAPE))
    			PostMessage(hwnd, WM_DESTROY, 0, 0);
    	}
    
    
    //////////////////////////////////////////////
    //                GAME END                  //
    //////////////////////////////////////////////
    
    // frees memory and cleans up before the game ends
    void Game_End(HWND hwnd)
    	{
    		int n;
    
    		for (n=0; n<6; n++)
    			if (kitty_image[n] != NULL)
    				kitty_image[n]->Release();
    
    			if (back != NULL)
    				back->Release();
    
    			if (sprite_handler != NULL)
    				sprite_handler->Release();
    	}
    Im using Visual C++ 6 with SP5 and im using DirectX SDK ... the latest (cant remember what its called, downloaded it from microsoft the other day...)

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    Quote Originally Posted by Manaxter
    Not that it may make any difference, but when I try and build it set to Release mode (as apose to debug mode) it comes up with this.

    Code:
    --------------------Configuration: Trans_Sprite - Win32 Release--------------------
    Compiling...
    dxgraphics.cpp
    game.cpp
    winmain.cpp
    Linking...
    dxgraphics.obj : error LNK2001: unresolved external symbol _Direct3DCreate9@4
    dxgraphics.obj : error LNK2001: unresolved external symbol _D3DXGetImageInfoFromFileA@8
    dxgraphics.obj : error LNK2001: unresolved external symbol _D3DXCreateTextureFromFileExA@56
    game.obj : error LNK2001: unresolved external symbol _D3DXCreateSprite@8
    winmain.obj : error LNK2001: unresolved external symbol "void __cdecl Game_Run(struct HWND__ *)" ...
    winmain.obj : error LNK2001: unresolved external symbol "int __cdecl Game_Init(struct HWND__ *)" ...
    Release/Trans_Sprite.exe : fatal error LNK1120: 6 unresolved externals
    Error executing link.exe.
    
    Trans_Sprite.exe - 7 error(s), 0 warning(s)
    It seems to have even more linking errors this way. Do i actually have to bring all the files into my project? Or can I just include them as I am doing?

    EDIT: Nope, dont worry, that didnt help. And sorry for the double post.
    More than likely your debug mode settings include the proper D3D library while your release mode settings do not. Look at the libraries that are being included in your debug version of the project (Project->Settings->Link tab->Object/Library modules) and make sure your release mode version is doing the same thing.

    Quote Originally Posted by Manaxter
    It comes up with these errors.

    Code:
    winmain.obj : error LNK2001: unresolved external symbol "void __fastcall Game_Run(struct HWND__ *)"...
    winmain.obj : error LNK2001: unresolved external symbol "int __fastcall Game_Init(struct HWND__ *)" ...
    ...


    Code:
    int Game_Int(HWND hwnd)
    	{
    		char s[20];
    		int n;
    Code:
    void Gane_Run(HWND hwnd)
    	{
    See the problem yet?
    "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

  10. #10
    Registered User
    Join Date
    Jan 2006
    Location
    Melbourne, Australia
    Posts
    11
    You have got to be kidding me!!!!!!!!!!!!

    I swear I checked that so many times!

    Thankyou very much for pointing out my stupidity!

    Moral of the Story: Allways check your speeling, it is likely to be the problem. Computers dont have problems, you have problems!
    Last edited by Manaxter; 01-10-2006 at 04:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 01-14-2008, 09:25 AM

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