• Create Account

# c++ extern command

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

10 replies to this topic

### #1Pero83  Members   -  Reputation: 102

Like
0Likes
Like

Posted 12 December 2011 - 01:32 PM

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

### #2yewbie  GDNet+   -  Reputation: 665

Like
0Likes
Like

Posted 12 December 2011 - 01:54 PM

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.

### #3UltimaX  Members   -  Reputation: 467

Like
0Likes
Like

Posted 12 December 2011 - 01:54 PM

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

### #4Pero83  Members   -  Reputation: 102

Like
0Likes
Like

Posted 12 December 2011 - 01:58 PM

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?

### #5UltimaX  Members   -  Reputation: 467

Like
1Likes
Like

Posted 12 December 2011 - 02:06 PM

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?

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.

### #6Kobo  Members   -  Reputation: 128

Like
0Likes
Like

Posted 13 December 2011 - 12:01 PM

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?

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.

### #7Pero83  Members   -  Reputation: 102

Like
0Likes
Like

Posted 13 December 2011 - 01:23 PM

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.

### #8boogyman19946  Members   -  Reputation: 1031

Like
0Likes
Like

Posted 13 December 2011 - 02:13 PM

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."
"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ

- Java API Documentation - For all your Java info needs
- C++ Standard Library Reference - For some of your C++ needs ^.^

### #9Pero83  Members   -  Reputation: 102

Like
0Likes
Like

Posted 13 December 2011 - 02:43 PM

Yes, but why would you basicly say that two times?

### #10UltimaX  Members   -  Reputation: 467

Like
0Likes
Like

Posted 14 December 2011 - 08:35 AM

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.

### #11Kobo  Members   -  Reputation: 128

Like
0Likes
Like

Posted 14 December 2011 - 11:43 AM

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.

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.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS