Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Guest Anonymous Poster

Beginner : extern expression

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

File1.h:
------------

extern int xyz ();

File1.cpp:
--------------

int xyz ()
{
}

File2.cpp:
--------------

#include "file1.h"
int main ()
{
xyz();
}

I hope this helps.

Share this post


Link to post
Share on other sites
Advertisement
Umm... correct me if I'm wrong, but you shouldn't need to use the extern keyword for functions. You only need to extern global variables from another file.


Six

Share this post


Link to post
Share on other sites
You're right, you don't need to put the keyword 'extern' before the prototype if it's in a header file.
However you can also extern a function by typing
extern int xyz(void);
In file2.cpp instead of the header.

(just to confuse you)

[This message has been edited by MikeD (edited September 27, 1999).]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I have two .cpp file and want to use the function xyz from File1 in File2 .
I wrote extern functio xyz in the File2 header to let the compiler know that where is an function in another file.
but he said : function xyz already declared in File1.obj .
How can I you function from other files ???
I've no C-Reference book and dont know when to use extern or whatever .

Hope for answer and Thank you

Share this post


Link to post
Share on other sites
I'm not confused at all; I just like to write solid, readable code. Using 'extern' when the language doesn't enforce it doesn't make bad code, it makes readable code. In this case, it makes it OBVIOUS that it's a declaration, so someone glancing at your code knows right away without looking anywhere else.

There, I'm done

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!