Sign in to follow this  

Beginner question

This topic is 4308 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

At the moment i am trying to learn DirectX using C++. But i can't even create a D3D interface with the following code: #include <d3d9.h> IDirect3D9* g_pD3D; INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR, INT) { g_pD3D = Direct3DCreate9(D3D_SDK_VERSION); } I can't see whats wrong with the code i get allways this link error: "error LNK2019: unresolved external symbol _Direct3DCreate9@4 referenced in function _WinMain@16" I know what this error means, but i thought "Direct3DCreate9" is included in "d3d9.h". What am i doing wrong? thx in Advance shino

Share this post


Link to post
Share on other sites
hi,

as you can see from the error code (LNK2019) it's a linker error, not a compiler error. That means that it have found the function in the .h, but it cannot find the .lib containing the function.
You should go to the Project properties / Linker / Input, and add d3d9.lib there.
You should also have a look at the Linker inputs of the SDK samples, as you may need more libs (like dxerr.lib dxguid.lib d3dx9d.lib d3d9.lib winmm.lib comctl32.lib)

kp

Share this post


Link to post
Share on other sites
You have to add inputs to the linker in the project settings. The best thing to do to make sure you add the right one is to look at which ones the SDK includes, but i include these:

dxguid.lib dinput8.lib d3dx9dt.lib d3d9.lib winmm.lib

Hope that helps,

Dave

Share this post


Link to post
Share on other sites
Quote:
Original post by shino
I know what this error means, but i thought "Direct3DCreate9" is included in "d3d9.h". What am i doing wrong?

As a hopefully useful clarification, Direct3DCreate9 is declared in d3d9.h, but it is not defined. A declaration merely informs other parts of code (such as your own code) what the function looks like, its public interface; it says nothing about what it does internally, how it actually works.

When you compile your project, it is compiled in chunks that each get turned into a single object file. (Object files are basically machine code, but they aren't executable.) Each .c or .cpp file gets turned into an object file. A convenient thing about compiling is that if you use a function in SourceA.cpp that is declared in a header, but is defined in some other .cpp file (or .lib file in this case, which is more or less a nicely wrapped up group of object files), the definition is irrelevant. All the compiler needs is the declaration. It needs to know how to call the function, not what the function does internally.

But after all that, you need to merge all the object files into one single file (usually an .exe, sometimes a .dll, .lib, .so, .a, et cetera, depending on your platform). This is the linker's job, a tool that is quite seperate from the compiler. When the linker does its work, it does need to know how every function actually works internally, because that code needs to actually go into the final executable. How could a function be executed if its code doesn't exist? If one piece of code calls a function that is declared, but not defined, then an error occurs at this stage.

So you can see now how the compiler can do all of its work, using d3d9.h to know how to call Direct3DCreate9, but then the linker gets messed up, because the function hasn't been defined anywhere, as far as the linker knows. But it actually has been defined, we just have to tell it where. That's why we need to add d3d9.lib to the linker's list of input files. Then it can search that library file for the function's definition, and insert that into the executable.

Share this post


Link to post
Share on other sites

This topic is 4308 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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