🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

multiple cpps

Started by
8 comments, last by SKSlayer 23 years, 11 months ago
There is still something I don''t get, thought, it''s really stupid How do I use multiple .cpp files into a project so I can use the functions defined in one in the other (I mean call a function) or put the class functions defined in a header ???
(you can find me on IRC : #opengl on undernet)
Advertisement
    main.hvoid a();void b();------------------file1.cpp#include "main.h"void a() {}------------------file2.cpp#include "main.h"void b() { a(); }    

I am going to write this, because I don't realy understand what the inmature New Zealander has writen, though I understand the actual question:

First, do you know how define functions for use else where. If not it goes:

        extern void bob(); // pre-definition (i think thats what it's called)void bob(); // some compilers let you omit thisvoid SomeOtherStuff(){  bob(); // function can be called before its real definition}void bob() // real definition{  blah();  yada();} 


OK, to use a *.h file you must include the actual *.cpp (where the read definitions are) file in your project or whatever (VC and BCB).

then write your pre-definitions into your header file (*.h) and use

    #include "whatever.h" 


Also, some times (most of the time) you may need to use the folowing around your *.h file:

    #ifndef _bob_was_here__or_whatever_text_you_want_to_use_#define _bob_was_here__or_whatever_text_you_want_to_use_/*blahblahblah*/#endif 


this basicly stops the file from being included twice.

(one note: (i think) you can also put real function defintions into a header if you do it with that last bit. Also, I think you need it to use it for class definitions. Basicly it's eaiser just to use that code anyway)

then to use your definitions here is an example of what you do:

    #include "bob.h"int main(){  bob();  return 0;} 


----------------------------------------------------------------

this is basicly what you would end up with in a simple yet working program:

First, times3.cpp:

int times3(int num){  return num*3;} 


Then, you make times3.h

#ifndef _TIMES3_H_#define _TIMES3_H_int times3(int num);#endif 


then the main.cpp file:
#include "times3.h"int main(){  return times3(5);} 


For your project you should have the 2 *.cpp files in the project (if you use command line tools, these are the files you type in).

Although this does nothing exciting, it is a good example. You can also put variable definitions into *.h files, if you make them a global in a *.cpp file, and then use:

extern int bob; 


Andrew

Edited by - slartibartfast00 on August 15, 2000 6:19:57 AM
SLARTIBARTFAST00 PLEASE SELECT WITTY .SIG: NoneWho is General Failure and why is he reading my disk?Microwave: Signal from a friendly micro...Multitasking: Screwing up several things at once...How do I set my laser printer on stun?Sarr! The spellchecker kinna take this abuse!
Errr, I just mean :

What''s the point of use multiple .cpp files into a project ?
What does it bring to you and how to do it (I guess it helps organising stuff).
(you can find me on IRC : #opengl on undernet)
When you have thousands lines of codes, you really bless having multiple files as it keeps things organized and you only need to recompile files you changed.

I'll try to clarify slartibartfast00's informations, as they're a little confusing.

When you want to use a function from anywhere in your code, whether it is in the same file or not, you have to declare it before it is used trough a function prototype. By putting these declarations in a header file, it frees you from having to type each functions in each files you use, which is nice to get rid of typos, and is really a good thing when you use dozens and dozens of such functions. You don't need to use the "extern" keyword for functions. Their use is only for global variables.
When using global variables across many files, you want to declare it into a header file, for the same reasons as above. However, each time you'll include this header file, a variable of the same type and name will be declared, which is bad, because you'll end up having multiple declarations. That's where the "extern" keyword comes in. It tells the compiler that this variable exists, not to declare it. You still need to declare your global variable in one of your file, though.
Last, the
#ifndef _SOMETHING_
#define _SOMETHING_
bla bla bla
#endif
is called a "sentinel". It is useful when you include files that include already included files, because it will get rid of duplicate declarations. It might seem as a dumb idea to include already included files, but this sometimes come in handy.

Here is some sample code:
/* MyHeader.h */#ifndef _MYHEADER_H_#define _MYHEADER_H_#define SOME_CONSTANT 10 // A constantint Add(int a, int b);   // Function prototypeextern int myNumber;     // External declaration#endif /* _MYHEADER_H_ *//* AddFile.cpp */#include "MyHeader.h"    // Appends MyHeader.hint MyNumber = 5;        // Declare and define the external varint Add(int a, int b){  return a+b;}/* Main.cpp */#include "MyHeader.h"    // Appends MyHeader.hint main(){  return Add(SOME_CONSTANT, MyNumber);}  


EL

(Edition: Stupid message board formatting)

----------------------------------------
"Inash neteia haeg joa kavari quilm..." SD4

Edited by - Demon Lord on August 15, 2000 1:51:33 PM
----------------------------------------"Inash neteia haeg joa kavari quilm..." SD4
Well, when let''s say I have 2 cpp in my project a.cpp and b.cpp
a.cpp contains main() and add(...)
b.cpp contains sub(...)

How does it knows which has the main ? Where do I declare
sub(...).
Do I have to make a common header which would have int sub(...)
and the b.cpp contains the body of the sub function ???



(you can find me on IRC : #opengl on undernet)
It is up to the linker to determine where to begin your program and where your functions are located. The compiler only blindly compiles your files and does not care whether you have a main function or not, or whether you have defined a variable twenty times in twenty different files. You don''t have to worry about where to place your entry function, your linker will find it.

For your example, if you want to use your "int sub()" function in file a.cpp when it is declared in b.cpp, you should use a common header containing the line "int sub();". This only tells your compiler that such a function exists. You must then define your function where you want to, in this case in b.cpp.
You could do without a header by placing the line "int sub();" directly into a.cpp, as the preprocessor command "#include "MyHeader.h"" only role is to insert the content of MyHeader.h at this place, but it would be a bad habbit to do so.

Just for fun:
/* Header.h */int sub(int a, int b);int add(int a, int b);/* a.cpp */#include "Header.h"int main(){  int a = add(1, 2);  int b = sub(a, 3);  return b;}int add(int a, int b){  return a+b;}/* b.cpp */#include "Header.h"int sub(int a, int b){  return add(a, -b);} 


EL

----------------------------------------
"Inash neteia haeg joa kavari quilm..." SD4
----------------------------------------"Inash neteia haeg joa kavari quilm..." SD4
Okay, BIG BIG thank you for explaining this to me (my stoopid C++ book doesn''t cover that). I am now able to go into bigger projects :o) THANK YOU
(you can find me on IRC : #opengl on undernet)
My pleasure

EL

----------------------------------------
"Inash neteia haeg joa kavari quilm..." SD4
----------------------------------------"Inash neteia haeg joa kavari quilm..." SD4
If you have a billion lines of code in 1 .cpp file and make a change, you need to recompile the whole thing. Time to go get pizza!

Split that into many files, and you only have to re-compile the small file that changed.

It also makes source control possible. If you have 10 people on the coding team, how can person 1 do work if all of it is in the file that person 2 has checked out?

Edited by - Buster on August 17, 2000 2:12:16 PM

This topic is closed to new replies.

Advertisement