Jump to content
  • Advertisement
Sign in to follow this  

c++ extern command

This topic is 2350 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

Hey all!




I was starting to learn Frank Luna's dx9 shader approach book, and i have some problems to understand few things.

For example, the extern command. Like this:

extern D3DApp* gd3dApp;

Now, In first chapters, you have basicly 3 files.

- d3dUtil.h

- d3dApp.h

- d3dApp.




now, d3dUtil.h has following lines:

// Globals for convenient access.
extern D3DApp* gd3dApp;
extern IDirect3DDevice9* gd3dDevice;





d3dapp.h includes this d3dUtil.h file, yet, it also, at the end, use same command:

// Globals for convenient access.
extern D3DApp* gd3dApp;
extern IDirect3DDevice9* gd3dDevice;


And then you have d3dApp.cpp, wich has this code

h

D3DApp* gd3dApp = 0;

IDirect3DDevice9* gd3dDevice = 0;





And this is what confuses me.




Ok so, has i understand, he is trying to make global variable D3DApp*;

and iirc, when you use extern command; there is no memory saved for this variable, as you say, that it is defined externally from that file.

Now, if that is the case.

If d3dapp.h includes d3dUtil.h.....why do BOTH of them have same command,

extern D3DApp* gd3dApp;

would the one in d3dUtil file suffice, since we included that file into d3dapp.h anyway?




Tnx







Share this post


Link to post
Share on other sites
Advertisement
Externs seem like a really bad practice to send a pointer to your directx interface.
I generally just pass it with a function during my init.

And I have not used extern for awhile but I *think* you need to specify extern for the variable in each cpp file that is not the one it originates from.

Share this post


Link to post
Share on other sites
The extern keyword declares a variable or function and specifies that it has external linkage (its name is visible from files other than the one in which it's defined). When modifying a variable, extern specifies that the variable has static duration (it is allocated when the program begins and deallocated when the program ends). The variable or function may be defined in another source file, or later in the same file. Declarations of variables and functions at file scope are external by default.
http://msdn.microsoft.com/en-us/library/0603949d%28v=VS.100%29.aspx

Share this post


Link to post
Share on other sites


And I have not used extern for awhile but I *think* you need to specify extern for the variable in each cpp file that is not the one it originates from.

even if you include file that already used extern command on the same variable? Looks to me like you would use extern command two times on the same variable, since you already include the first file?

Share this post


Link to post
Share on other sites

[quote name='yewbie' timestamp='1323719650' post='4893228']
And I have not used extern for awhile but I *think* you need to specify extern for the variable in each cpp file that is not the one it originates from.

even if you include file that already used extern command on the same variable? Looks to me like you would use extern command two times on the same variable, since you already include the first file?


[/quote]

Read my post carefully; the key words being "external linkage"

Also, "The variable or function may be defined in another source file, or later in the same file." you are creating a link to the variable definitions and not actually defining them.

This will make for sloppy code once your code base starts getting large. Avoid externs and pass them via the ctor or something.

Share this post


Link to post
Share on other sites
Hidden
afaik you don't need to declare the variables more than once. Its probably a typo. The important thing is that the source files that need access to the variables all have to include the header declaring them.

Share this post


Link to post

[quote name='yewbie' timestamp='1323719650' post='4893228']
And I have not used extern for awhile but I *think* you need to specify extern for the variable in each cpp file that is not the one it originates from.

even if you include file that already used extern command on the same variable? Looks to me like you would use extern command two times on the same variable, since you already include the first file?


[/quote]


You don't want to use extern if you're including a file that declares that variable. Extern is for when something is declared in a file that you're linking with. If you include a file that makes a global declaration, then you don't need to declare it again.

Share this post


Link to post
Share on other sites
well, that is what is confusing the hell out of me.

I'll try to make more simple example.

I have:

- A.h

- B.h

- B.cpp

B.h has : extern D3DApp* gd3dApp; Do note the EXTERN keyword.

Now, B.cpp has D3DApp* gd3dApp = 0; No extern keyword.

This, i think, i understand. In B.h, you declare an external variable, wich means, that it is define / initialized somewhere farther in the code or in other file.




But what gave me a problem, is that A.h already has the " extern D3DApp* gd3dApp", wich B.h also has. Not definition, but (Extern) declaration. B.h then includes this A.h, and as such, you call extern two times on same variables within same file (while definition is in third file - B.cpp).

So if understand Kobo correctly, you don't need to call extern command in B.h, because it includes A.h, wich already did that (as in, it already used extern command on that variable)? And B.cpp actually then defines this varible?




tnx all for help btw.

Share this post


Link to post
Share on other sites
if I'm getting this right, the extern command doesn't get "called on" a variable, but it merely tells the compiler that "hey yeah, this variable over here is defined somewhere else."

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!