Archived

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

Thrakhath

A little help with external source code

Recommended Posts

Up till now I''ve pretty much put all my code in one big file (called main/cpp or somesuch) and then compiling it into my exe. The problem (as I''m sure some of you know) is that this gets really big really quick and it becomes hard / tedious to find things. So, my question to you is, how do I move stuff out to other files. For example, initalization in one file, 2d primitives in a second, Physics in a third, etc? How do I get them to work together? Share the same globals, call on functions from eachother, etc. In short, act like it''s all one source file. Thanks in advance

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quick guide

When you compile a file, the compiler needs to know the type of every variable that you use, and the signature of every function you call. So say you have 2 functions in 2 seperate .cpp files which are both included in your project.


//first.cpp

int firstfunc(const int& i)
{
return i;
}

//second.cpp
int secondfunc()
{
return firstfunc(10); //returns 10
}


Both of these functions are in seperate files, and so when the compiler trys to compile secondfunc(), it will see that it calls a function firstfunc() with an int in the parameter, but if it because the firstfunc is inside another .cpp file, it doesnt know that it exists, and it doesnt know the parameters that the function takes, or its return type. So in order to make this function call to work, you need to tell the compiler about firstfunc() inside the second.cpp. You do this by prototyping the function


//second.cpp
int firstfunc(const int& i);//function prototype

int secondfunc()
{
return firstfunc(10); //returns 10
}


The prototype tells the compiler all it needs to know about the function to verify that it is correct to use it in the way its being used(i.e. it needs to confirm that the function should take an int as a parameter). The compiler then leaves a message for the linker that a function called firstfunc() is called in secondfunc(), and the linker will then try and find the function(if it doesnt you will get a linker error)

The code for firstfunc() is compiled when first.cpp is compiled, and the linker then links the call to the function call in secondfunc().

global variables work in a similar manner, you must prototype the global function in every .cpp file that you use, with a slight difference that you must use the extern keyword to tell the compiler that the variable has external linkage(that the linker should try and find the variable and that it is declared in another .cpp file)


//third.cpp
int global_variable = 10;

//second.cpp
int firstfunc(const int& i);//function prototype
extern int global_variable;

int secondfunc()
{
return firstfunc(global_variable); //returns global_variable
}


Again the compiler needs to know the type that global_variable is in order to be sure its being used correctly, and so you must declare it with the extern keyword to give it external linkage(functions have external linkage by default, so you dont need to use the extern keyword for functions, but the process is the same)

So finally, you must declare every global variable which is defined in another .cpp file as extern variable_type variable_name in the .cpp file in which you want to use it, and you must do the same with functions, but without the extern keyword.

hope that helps

Share this post


Link to post
Share on other sites
Without knowing too much about the subject:

Put the class header in a class.h file and the class implementation in a class.cpp file. If you want to use the class in another file, include the .h file (NOT the .cpp file) at the top of that file. Globals can be put in a separate file named globals.h or something. If you get errors with your global variables, do a google search on the "extern" keyword.

Share this post


Link to post
Share on other sites
Wow, thank you (whoever you are) Anonymous, that''s immensly helpful. Thank you Miserable as well, for the link.

Also, if I compile with just prototypes, is the Linker going to find everything alright, or do I need some #include statments as well?

The final confusing bit (for me) is those #include statments, along with the .h files and the .c / .cpp files. What code do I put in which, and where do I put #include''s?

If I don''t get an answer, I hope I''ll find it in Miserable''s link.

Thanks again.

Share this post


Link to post
Share on other sites
I strongly recommend you read Kylotan''s article (the one I linked to). It tells you everything you need to know about dividing code into headers and source files, from why through how to what to watch out for and how to solve the common problems.

Share this post


Link to post
Share on other sites