"unresolved external symbol _Direct3DCreate9@4"

36b416ed76cbaff49c8f6b7511458883
0
poita 101 Jun 08, 2006 at 13:48

I’m creating my direct 3d interface object by using:

// in WinMain ...
if((pD3D = Direct3DCreate9(D3D_SDK_VERSION)) == NULL)
    return E_FAIL;

pD3D is defined globally as a LPDIRECT3D9 and I’ve included d3d9.h but I’m getting this error message:

“1>main.obj : error LNK2019: unresolved external symbol _Direct3DCreate9@4 referenced in function _WinMain@16”

Any ideas what’s wrong? I remember I got the same error before when I did this but I can’t remember how I solved it.

Thanks in advance

11 Replies

Please log in or register to post a reply.

6f0a333c785da81d479a0f58c2ccb203
0
monjardin 102 Jun 08, 2006 at 14:02

It looks like you aren’t linking to the D3D libraries…

Eece94fe9aba4fd2f8b12f108548187d
0
gmax136 101 Jun 08, 2006 at 14:38

add to object/libraries: d3d9.lib

Afb4377d78290e589e946c5862757ccf
0
Qlone 101 Jun 09, 2006 at 15:06

… or add the .lib file to your solution, or #pragma comment(lib, “d3d9.lib”) in the header file of your pogram… so may ways :)

36b416ed76cbaff49c8f6b7511458883
0
poita 101 Jun 10, 2006 at 07:16

Oh ok. I was under the impression that including the header file allowed you to use those functions.

What exactly does linking to the library do?

[EDIT] Thanks btw! :)

0684f9d33f52fa189aad7ac9e8c87510
0
baldurk 101 Jun 10, 2006 at 09:40

@poita

Oh ok. I was under the impression that including the header file allowed you to use those functions.

What exactly does linking to the library do?

[EDIT] Thanks btw! :)

The way the build system works is in a couple of major parts (not counting the preprocessor and suchlike).

First, the compiler will take the raw source code and convert it into an object file. This is why you need to include header files. The header files contain the declarations of the functions you’re using. ie. the header files say to the compiler, “there is such a function matching this prototype - if it gets used, don’t give an error”. If you didn’t include the header, or used the function wrongly, the compiler would tell you about it.

However, these object files themselves don’t make your program. That’s why you need the linker. The linker takes the object files and combines them into the final executable. It also defines in the executable every single function your program calls. So if you call printf, it defines that your program needs to link to the standard c library at runtime (which I think is msvcrt on windows - MS visual c run time?). Then, if you call Direct3DCreate9 the linker will define that you need to link against d3d. However, it can’t work that out automatically. You need to tell it that you’re using d3d in your program. That way it can resolve all the symbols in the object files and properly link your program.

When your program gets run then, all these defined libraries get loaded (or, if already loaded, linked in) and so you can call these funtions as normal.

That’s assuming you’re using dynamic linking, which I guess you probably are :).

36b416ed76cbaff49c8f6b7511458883
0
poita 101 Jun 11, 2006 at 11:02

Thanks for that baldurk, makes perfect sense now. :)

Off-topic:

Regarding you sig, I always heard that with 2 more lines:
“He that knows and knows not that he knows is asleep. Awaken him.
He that knows and knows that he knows is wise. Follow him.”

Don’t know if you already knew them but just though I’d put that out there.

6f0a333c785da81d479a0f58c2ccb203
0
monjardin 102 Jun 12, 2006 at 14:02

Those sayings would be easier for me to follow in C++ rather than in English. ;)

void Person::FollowBaldurkAdvice(Person & other)
{
  if (!other.Knows() && !other.ThinksHeKnows())
    this->Teach(other);
  else if (!other.Knows() && other.ThinksHeKnows())
    this->Shun(other);
  else if (other.Knows() && !other.ThinksHeKnows())
    this->Awaken(other);
  else if (other.Knows() && other.ThinksHeKnows())
    this->Follow(other);
}

… or perhaps a truth table would help.

Knows | Thinks He Knows || Action
-----------------------------------
   0  |        0        || Teach
   0  |        1        || Shun
   1  |        0        || Awaken
   1  |        1        || Follow
0684f9d33f52fa189aad7ac9e8c87510
0
baldurk 101 Jun 12, 2006 at 17:35

hah. I did originally have the entire quote, but generally the first two lines are very relevant to forums, the second two aren’t :). Besides, 4 lines is a bit much for a sig.

monjardin: your JwN meter has gone down I see :).

6f0a333c785da81d479a0f58c2ccb203
0
monjardin 102 Jun 12, 2006 at 18:48

Yes, the JwN meter has gone down. Lately, the MMO noobs are getting shot down before I even see their posts. This one cheered me up a bit. The poster took the constructive criticism for what it was.

Otherwise, the standard procedure has been to degrade the DevMaster forum members. In light of the answer you just gave poita, I don’t know how anyone could claim this forum is inhospitable to beginners. It’s a good question from someone that doesn’t have much programming experience, but a lot of places would flame him/her anyway.

Also, did you notice that the FollowBaldurkAdvice method does’t work with a const Person? ;)

73252e48eef330c0b210e0ff9303b8f5
0
enderleit 101 Oct 17, 2009 at 07:31

I have this same problem, except when I check the Options->Directories, the directx includes and library folders are already added to the appropriate Directory tabs…

Is there something else I need to do to use DirectX in my code?

/Sincerely
Martin Enderleit

73252e48eef330c0b210e0ff9303b8f5
0
enderleit 101 Oct 17, 2009 at 07:49

Never mind, I figured it out! :)