Help! I'm having trouble building a project that uses d3dx9.h (errors LNK2019 and LNK1120)

Yanıt Help! I'm having trouble building a project that uses d3dx9.h (errors LNK2019 and LNK1120)

  • 20 Nisan 2012 Cuma 17:03
     
      Kod İçerir

    I'm working on an exercise from a book I recently bought that is based on a sample project from said book.  However, I am getting the following:

    main.obj : error LNK2019: unresolved external symbol _D3DXMatrixInverse@12 referenced in function _main
    main.obj : error LNK2019: unresolved external symbol _D3DXMatrixMultiply@12 referenced in function "public: struct D3DXMATRIX __thiscall D3DXMATRIX::operator*(struct D3DXMATRIX const &)const " (??DD3DXMATRIX@@QBE?AU0@ABU0@@Z)
    MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
    C:\Documents and Settings\*****\My Documents\Intro to 3D Game Programming with DirectX9c\Chapter2_Exercise4\Debug\Chapter2_Exercise4.exe : fatal error LNK1120: 3 unresolved externals
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    What is causing these errors?  Could it be how the project was created? (ie console application VS regular win32 project).  It's strange because the sample program from the book builds and runs just fine!  How do I fix this?  Am I missing something?

    I'm using Windows XP, so I believe that only DirectX 9.0c is supported

    I don't know if it's relevant, but here is the program:

    // CHAPTER 2 2.8 EXERCISE 4
    // Using S and T, verify that (ST)^-1 == (T^-1)(S^-1)
    #include <d3dx9.h>
    #include <iostream>
    using namespace std;
    // Overload the  "<<" operators so that we can use cout to 
    // output D3DXVECTOR4 and D3DXMATRIX objects.
    ostream& operator<<(ostream& os, D3DXVECTOR4& v)
    {
    	os << "(" << v.x << ", " << v.y << ", " << v.z << ", " << v.w << ")";
    	return os;
    }
    ostream& operator<<(ostream& os, D3DXMATRIX& m)
    {
    	for(int i = 0; i < 4; ++i)
    	{
    		for(int j = 0; j < 4; ++j)
    			os << m(i, j) << "  ";
    		os << endl;
    	}
    	return os;
    }
    int main()
    {
    	
    	D3DXMATRIX S(3.0f, 0.0f, 0.0f, 0.0f,
    				 0.0f,-2.0f, 0.0f, 0.0f,
    				 0.0f, 0.0f, 4.0f, 0.0f,
    				 0.0f, 0.0f, 0.0f, 1.0f);
    	D3DXMATRIX T(1.0f, 0.0f, 0.0f, 0.0f,
    				 0.0f, 1.0f, 0.0f, 0.0f,
    				 0.0f, 0.0f, 1.0f, 0.0f,
    				 2.0f,-5.0f,-1.0f, 1.0f);
    	D3DXMATRIX S_inverse;
    	D3DXMATRIX T_inverse;
    	D3DXMATRIX ST;
    	D3DXMATRIX ST_inverse;
    	D3DXMATRIX T_inverseS_inverse;
    	
    	ST = S * T;
    	
    	D3DXMatrixInverse(&S_inverse, 0, &S);
    	D3DXMatrixInverse(&T_inverse, 0, &T);
    	D3DXMatrixInverse(&ST_inverse, 0, &ST);
        	T_inverseS_inverse = T_inverse * S_inverse;
    	cout << "(ST)^-1 = "		  << endl << ST_inverse << endl;
    	cout << "(T^-1)(S^-1) = " << endl << T_inverseS_inverse << endl;  
    	return 0;
    }

Tüm Yanıtlar

  • 20 Nisan 2012 Cuma 17:09
     
     

    Add -

    #pragma comment(lib, "D3dx9.lib");

  • 20 Nisan 2012 Cuma 17:11
     
     

    Are you linking against the .lib file that goes along with it too.

    You should find in the DirectX SDK that there is a d3dx9.lib file too, which contains the symbols needed to resolve this.


    This is a signature

    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Do you want Visual Studio 11 Express to be freely installable on Windows 7 and able to write regular C++ applications? Please vote for this.

  • 20 Nisan 2012 Cuma 17:39
     
     

    Add -

    #pragma comment(lib, "D3dx9.lib");


    What is this exactly, and how do I add it? (sorry, I'm still new)
  • 20 Nisan 2012 Cuma 17:42
     
     

    This is a pragma directive. It allows the compiler to pass information to the linker. This particular statement tells the linker to link your program against d3dx9.lib. Add it to your source file anywhere at the top.

    --EDIT--

    I just thought, I must point out that not all pragma directives pass information to the linker you can see the full list here. But this particular one doe pass information to the linker.


    This is a signature

    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Do you want Visual Studio 11 Express to be freely installable on Windows 7 and able to write regular C++ applications? Please vote for this.


    • Düzenleyen Crescens2k 20 Nisan 2012 Cuma 17:48
    •  
  • 20 Nisan 2012 Cuma 17:49
     
     

    I did add the the path where the d3dx9 object file library is located (C:\Program Files\Microsoft DirectX SDK (June 2010)\Lib\x86)

    I added it by going to Properties/Configuration Properties/VC++ Directories/Library Directories/after a semicolon

    Unless of course "linking against the .lib file" like you said is something alltogether different!

    If not, what is still causing the errors inspite of this?


  • 20 Nisan 2012 Cuma 17:57
     
     Yanıt

    Well, linking against a library means that either you use as suggested above and use the #pragma statement to tell it to link to a library, or add d3dx9.lib to the command line in Project Properties->Configuration Properties->Linker->Input->Additional Dependencies. You see, just adding the path to the DirectX libraries will not cause it to link against the required library. The linker doesn't enumerate through all of the .lib files to see if one of them resolves the symbols that it needs, you need to tell the linker what to link against. If the linker did go through the list then there would be problems with even the basic VC linking process, since it is possible for two libraries to have the same symbols, and if two libraries are just as good which one should it use. This affects VC because both msvcrt.lib and msvcrtd.lib in the VC\lib directory under your Visual Studio install directory contain the same symbols, but they both link to different DLLs.

    So linking against the .lib file means explicitly telling the linker through some means to use the required library, in this case d3dx9.lib, in the linking process. The two methods given in this thread are the two ways of doing this.


    This is a signature

    Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Do you want Visual Studio 11 Express to be freely installable on Windows 7 and able to write regular C++ applications? Please vote for this.

    • Yanıt Olarak İşaretleyen Huaysepfan87 22 Nisan 2012 Pazar 02:23
    •  
  • 20 Nisan 2012 Cuma 18:14
     
     

    And this message - 

    >error LNK2019: unresolved external symbol _WinMain@16referenced in function ___tmainCRTStartup

    "You can also get LNK2019 if you are building a console application with /SUBSYSTEM:WINDOWS. The unresolved symbol will be _WinMain@16. In this case, just link with /SUBSYSTEM:CONSOLE."

    It is in Linker->System ->SubSystem