Sign in to follow this  
Pero83

c++ extern command

Recommended Posts

Pero83    102
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:

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




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

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

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

[code]h

D3DApp* gd3dApp = 0;

IDirect3DDevice9* gd3dDevice = 0;

[/code]



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
yewbie    677
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 [b]each[/b] cpp file that is not the one it originates from.

Share this post


Link to post
Share on other sites
UltimaX    468
[i]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.
[url="http://msdn.microsoft.com/en-us/library/0603949d%28v=VS.100%29.aspx"]http://msdn.microsoft.com/en-us/library/0603949d%28v=VS.100%29.aspx[/url]
[/i]

Share this post


Link to post
Share on other sites
Pero83    102
[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 [b]each[/b] cpp file that is not the one it originates from.
[/quote]
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
UltimaX    468
[quote name='Pero83' timestamp='1323719883' post='4893231']
[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 [b]each[/b] cpp file that is not the one it originates from.
[/quote]
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 "[i]external linkage[i]"

[/i][/i]Also, [i][i]"[/i][/i][i]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.
[/i]

Share this post


Link to post
Share on other sites
pulpfist    528
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
Kobo    128
[quote name='Pero83' timestamp='1323719883' post='4893231']
[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 [b]each[/b] cpp file that is not the one it originates from.
[/quote]
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
Pero83    102
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
UltimaX    468
Without knowing the code or the circumstances I am thinking this: Circular dependency.

d3dUtil.h needs to use those externs for various utility functions so it relies on those externs (in d3dApp.h).
d3dApp.h makes use of some of those utility functions, so it relies on d3dUtil.h.

If d3dUtil.h includes dadApp.h to use those two externs and then d3dApp.h turns around and includes d3dUtil.h to use the utility functions then you can see where it gets ugly.

Share this post


Link to post
Share on other sites
Kobo    128
[quote name='Pero83' timestamp='1323804222' post='4893606']
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.
[/quote]

No, you need to use the extern keyword once and not declare it again. If you include a header that has extern D3DApp* gd3dApp, that is the same as putting extern D3DApp* gd3dApp in the file that has the #include directive.

if A.h declares "int x = 5;" in it, then if B.cpp includes A.h it will know x = 5. Everything you include gets put into a translation unit by the preprocessor, which is sort of like having all your text from each file you include smashed into a giant wall of text. If you want to use an external variable like gd3dApp, you declare it exactly one time in a header somewhere with the extern keyword. Putting interface in headers that get included and implementation in source files that don't get included in other source files will help you. It will start getting tricky and tempt you to include headers in headers if you are putting logic in some of your headers that might want to know more about gd3dApp than its type (D3DApp *).

You want to have "extern D3DApp* gd3dApp" in a header somewhere that gets included by source files. You do not want to declare D3DApp* gd3dApp without the extern keyword.

Share this post


Link to post
Share on other sites

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