New gamer directX - linker issues...

Started by
4 comments, last by DjMaSh 17 years, 6 months ago
Hi all. Ive just started getting into directX3d and im part way to drawing a triangle (lol OMG!!!). Well actually, i have already drawn a triangle, and am now in the stages of adding a camera that i can shift around. Anyway... I have just added these lines of code into my program to create my matricies (I think): D3DXMatrixLookAtLH(&m_View, &m_EyePos, &m_TargetPos, &m_UpVector); D3DXMatrixPerspectiveFovLH(&m_Projection, D3DX_PI/4, 500/500, 1, 50); and now I am getting linker errors when I compile: [Linker error] undefined reference to `D3DXMatrixLookAtLH@16' [Linker error] undefined reference to `D3DXMatrixPerspectiveFovLH@20' I have included -ld3d9 and -ld3d9x to the linker (im using dev-C++) and included the relevant header files. What now??? I have no idea.
Advertisement
Include d3d9.lib and d3dx9.lib in the linker input, should work fine :-)
Steven ToveySPUify | Twitter
to do this you simple tpye in either
#pragma(lib, "d3d9.lib");
#pragma(lib, "d3d9x.lib");

or in the seetings for the project (linker settings as mentioned above)
"Try not. Do, or do not. There is no try" - Yoda- Software Engineer, - Computer Game Developer
Also note that for debug builds you should link to d3dx9d.lib, since it performs a few more debug checks.
I already have already included the d3d9.lib and d3dx9.lib in the linker though...

I thought thats what passing tha commands "-ld3d9" and "-ld3dx9" did? I have copied all the lib files from the dxsdk/lib directory into my devcpp/lib/ directory (header files too) so it is not having trouble finding them.

If this is not how you link the lib files to the linker, can someone please give me a quick tutorial on how to do this using dev-c++?

Im not on my windows machine right now so i cant test that #pragma stuff mentioned up there^^.
Ok I have include those two "#pragma" commands and I am still getting the exact same errors. Please someone help me!
Here is my code :D

#include <windows.h>#include <iostream>#include<d3d9.h>#include<d3dx9.h> #pragma(lib, "d3d9.lib");#pragma(lib, "d3dx9.lib");struct OURCUSTOMVERTEX {     float x,y,z;     DWORD color; }; int int_AppRunning = 1;/////////////////////////////////////////////////////////////////////////*Prototyping functions so i dont have to put main at the bottom. I like my style*/int WINAPI WinMain(HINSTANCE hInstance, HINSTANCEhPreviousInstance, LPSTR lpcmdline, int nCmdShow);LRESULT CALLBACK OurWindowProcedure(HWND han_Wind,UINT uint_Message,WPARAM parameter1,LPARAM parameter2);HWND NewWindow(LPCTSTR str_Title,int int_XPos, int int_YPos, int int_Width, int int_Height);LPDIRECT3DDEVICE9 InitializeDevice(HWND han_WindowToBindTo);void DrawScene(LPDIRECT3DDEVICE9 p_dx_Device, LPDIRECT3DVERTEXBUFFER9 p_dx_VertexBuffer);LPDIRECT3DVERTEXBUFFER9 FillVertices(HWND han_Window, LPDIRECT3DDEVICE9 p_dx_Device);void SetUpCamera(LPDIRECT3DDEVICE9 p_dx_Device);/////////////////////////////////////////////////////////////////////using namespace std;/*  Entry point for a windows program.*/    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCEhPreviousInstance, LPSTR lpcmdline, int nCmdShow){ MSG msg_Message; HWND han_Window = NewWindow("DirectX C++ Tutorial",100,100,500,500);//create our window LPDIRECT3DDEVICE9 p_Device = InitializeDevice(han_Window);//connect to g-card(device) LPDIRECT3DVERTEXBUFFER9 p_dx_VB = FillVertices(han_Window, p_Device);//fill verticies //SetUpCamera(p_Device); while(int_AppRunning) {  DrawScene(p_Device, p_dx_VB); //draw us a scene  if(PeekMessage(&msg_Message, han_Window, 0, 0, PM_REMOVE))  {     if(!IsDialogMessage(han_Window,&msg_Message))   {       DispatchMessage(&msg_Message);   }  } } p_Device->Release(); //free up device DestroyWindow(han_Window);//empty the memory our window used return 0;}/*Creates a new Window*/HWND NewWindow(LPCTSTR str_Title,int int_XPos, int int_YPos, int int_Width, int int_Height){    //a window structure WNDCLASSEX wnd_Structure;  //fill that structure with goodies lol wnd_Structure.cbSize = sizeof(WNDCLASSEX); wnd_Structure.style = CS_HREDRAW | CS_VREDRAW; wnd_Structure.lpfnWndProc = OurWindowProcedure; wnd_Structure.cbClsExtra = 0; wnd_Structure.cbWndExtra = 0; wnd_Structure.hInstance = GetModuleHandle(NULL); wnd_Structure.hIcon = NULL; wnd_Structure.hCursor = NULL; wnd_Structure.hbrBackground = GetSysColorBrush(COLOR_BTNFACE); wnd_Structure.lpszMenuName = NULL; wnd_Structure.lpszClassName = "WindowClassName"; wnd_Structure.hIconSm = LoadIcon(NULL,IDI_APPLICATION);  //register the window RegisterClassEx(&wnd_Structure);  //create the window. This returns a handle to the window (pointer) return CreateWindowEx(WS_EX_CONTROLPARENT,                      "WindowClassName",                      str_Title,                      WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_VISIBLE,                      int_XPos,                      int_YPos,                      int_Width,                      int_Height,                      NULL,                      NULL,                      GetModuleHandle(NULL),                      NULL);}/*Function for handling windows events*/LRESULT CALLBACK OurWindowProcedure(HWND han_Wind,UINT uint_Message,WPARAM parameter1,LPARAM parameter2){       switch(uint_Message) {     case WM_KEYDOWN:     {         int_AppRunning = 0;         break;     }     break; } //return default windows procedure. Dont want to process anything @ tha mo return DefWindowProc(han_Wind,uint_Message,parameter1,parameter2);}/*Links me program to me graphics card pretty much yo*/LPDIRECT3DDEVICE9 InitializeDevice(HWND han_WindowToBindTo){LPDIRECT3D9 p_dx_Object;//Try to get an object to find out if directX is installed  p_dx_Object = Direct3DCreate9(D3D_SDK_VERSION); //If not, warn user! if (p_dx_Object == NULL) {    //Simple message box! this is rad. parameters: handle to window, text, title, and buttons to be displayed     MessageBox(han_WindowToBindTo,"DirectX Runtime library not installed!","InitializeDevice()",MB_OK); } //About to link to device, gotta sort some parameter shit out first. D3DPRESENT_PARAMETERS dx_PresParams;  //device parameter struct  ZeroMemory( &dx_PresParams, sizeof(dx_PresParams) );//clear memory dx_PresParams.Windowed = TRUE;                      //dont want full screen... dx_PresParams.SwapEffect = D3DSWAPEFFECT_DISCARD; dx_PresParams.BackBufferFormat = D3DFMT_UNKNOWN;  //now we about to link to device LPDIRECT3DDEVICE9 p_dx_Device; //stores a pointer to our device(this is my g-card WOW!)  //call create device on that object we created up there ^^^ (no not that one) HAHA /*parameters:              !0 if multiple graphic adapters              Have a tru hardware g-card              Window im binding device to              Im gonna do my processing with hardware vertex processor              Pass that thing i created up there, the device linker thing              Memory where to put this stuff */               p_dx_Object->CreateDevice(D3DADAPTER_DEFAULT,  D3DDEVTYPE_HAL,  han_WindowToBindTo,  D3DCREATE_HARDWARE_VERTEXPROCESSING,  &dx_PresParams,  &p_dx_Device); p_dx_Device->SetRenderState(D3DRS_LIGHTING,false);//turn the lights off //If i didn't have a hardware device (shitty g-card), then link to software emulator //NOTE: reference device is ALOT slower than hardware. Screw that. - lets hope it works aye =) if (FAILED(p_dx_Object->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, han_WindowToBindTo, D3DCREATE_HARDWARE_VERTEXPROCESSING, &dx_PresParams, &p_dx_Device))) {     if (FAILED(p_dx_Object->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, han_WindowToBindTo, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &dx_PresParams, &p_dx_Device)))     {     //Something funny is going on, cant even hardware device with software...uhoh         MessageBox(han_WindowToBindTo,"Failed to create even the reference device!","InitializeDevice()",MB_OK);     } } p_dx_Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE); //turn off 'Culling" return p_dx_Device; //return pointer to our device}/*This is going to draw a new screen for us. How kind :D*/void DrawScene(LPDIRECT3DDEVICE9 p_dx_Device, LPDIRECT3DVERTEXBUFFER9 p_dx_VertexBuffer) {     //Clear the screen to a particular color b4 we draw.     p_dx_Device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);     p_dx_Device->BeginScene();//We're about to draw......          p_dx_Device->SetStreamSource(0, p_dx_VertexBuffer, 0, sizeof(OURCUSTOMVERTEX));     p_dx_Device->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE);     p_dx_Device->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);          p_dx_Device->EndScene();//And we're done drawing now.     p_dx_Device->Present(NULL, NULL, NULL, NULL);//Show us the picture please. }  /* I think this is going to make a triangle using 3 verticies. */ LPDIRECT3DVERTEXBUFFER9 FillVertices(HWND han_Window, LPDIRECT3DDEVICE9 p_dx_Device) {     OURCUSTOMVERTEX cv_Vertices[3];      cv_Vertices[0].x = 5.0f;     cv_Vertices[0].y = 10.0f;     cv_Vertices[0].z = 0.0f;     cv_Vertices[0].color = 0xffff0000;          cv_Vertices[1].x = 10.0f;     cv_Vertices[1].y = 0.0f;     cv_Vertices[1].z = 0.0f;     cv_Vertices[1].color = 0xff00ff00;          cv_Vertices[2].x = 0.0f;     cv_Vertices[2].y = 0.0f;     cv_Vertices[2].z = 0.0f;     cv_Vertices[2].color = 0xff00ffff;               LPDIRECT3DVERTEXBUFFER9 p_dx_VertexBuffer;//DirectX memory in our g-card     //try to allocate that memory      if (FAILED(p_dx_Device->CreateVertexBuffer(3*sizeof(OURCUSTOMVERTEX), 0, D3DFVF_XYZ|D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, &p_dx_VertexBuffer, NULL)))      {       MessageBox(han_Window,"Error while creating VertexBuffer",//if it failed display message       "FillVertices()",MB_OK);      }            VOID* p_Vertices;       //have to lock memory before trying to write to it.      if (FAILED(p_dx_VertexBuffer->Lock(0, 3*sizeof(OURCUSTOMVERTEX), (void**)&p_Vertices, 0)))      {       MessageBox(han_Window,"Error trying to lock","FillVertices()",MB_OK);     }else{     memcpy(p_Vertices, cv_Vertices, 3*sizeof(OURCUSTOMVERTEX));//copy mem     p_dx_VertexBuffer->Unlock();//unlock mem     }     return p_dx_VertexBuffer; //return vertex buffer }  /* Sets up a camera for us */void SetUpCamera(LPDIRECT3DDEVICE9 p_dx_Device) { D3DXVECTOR3 m_EyePos(0, 0, -30); //pos of camera D3DXVECTOR3 m_TargetPos(0, 0, 0); //direction D3DXVECTOR3 m_UpVector(0, 1, 0);  //"up" vector for camera D3DXMATRIXA16 m_View;  //matrix representing camera D3DXMatrixLookAtLH(&m_View, &m_EyePos, &m_TargetPos, &m_UpVector); //create matrx p_dx_Device->SetTransform(D3DTS_VIEW, &m_View); //pass camera matrix to device  D3DXMATRIX m_Projection; //matrix representing the lens D3DXMatrixPerspectiveFovLH(&m_Projection, D3DX_PI/4, 500/500, 1, 50);//near plane far plane and lens matrix p_dx_Device->SetTransform(D3DTS_PROJECTION, &m_Projection);//pass lens matrix to device }


And this is what the compiler is doing:

Compiler: Default compiler
Executing g++.exe...
g++.exe "E:\Mashs Shit\programming\FirstProject\main.cpp" -o "E:\Mashs Shit\programming\FirstProject\main.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -ld3d9 -ld3dx9
C:\DOCUME~1\DjMa$h\LOCALS~1\Temp/cc4uaaaa.o(.text+0x844):main.cpp: undefined reference to `D3DXMatrixLookAtLH@16'
C:\DOCUME~1\DjMa$h\LOCALS~1\Temp/cc4uaaaa.o(.text+0x8ae):main.cpp: undefined reference to `D3DXMatrixPerspectiveFovLH@20'
collect2: ld returned 1 exit status

Execution terminated

Please please please help me. I have no idea what I am doing wrong. Its a linking error but im linking everything correctly!

This topic is closed to new replies.

Advertisement