Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Two or more .cpp files in one project

Author
Message
Murloc
15
Years of Service
User Offline
Joined: 28th Dec 2008
Location: Utena,Lithuania
Posted: 10th Mar 2009 21:05
I always wondered how to do this, and what are the benefits of using this? I mean 2 or more .cpp files.

Theory-When you know everything,but nothing works.
Practice-When everything works,but you don't know why.
Programming merges these two-Nothing works,and you don't know why.
Mulderman
20
Years of Service
User Offline
Joined: 8th Jan 2004
Location: C:\\
Posted: 10th Mar 2009 22:25
I suppose you are using VC express. There is or should be Solution explorer in IDE and there you can see different icons.
One of them is like folder icon.
Some are named like: Header files, Resource files, Source files.
For example you can do a right click on Header files icon and then choose Add->New Item to add new file to your project or Existing item, if you already have it.

Same goes for source files.



Benefit is mainly compiling speed. If you use only on big .CPP file and all of your code is in this file, then compiler must compile all these lines again and again if you even change one line in your source.

If you split your code into different headers (.H) and source (.CPP) files then if you make some changes in them then compiler knows that only this particular file has changed and it doesn't have to recompile or touch others because they aren't modified.


This can really speed up compiling process.



And second benefit is that you can organize your code better, if you have split it into different parts, eg: state manager, audio, input etc etc.
Murloc
15
Years of Service
User Offline
Joined: 28th Dec 2008
Location: Utena,Lithuania
Posted: 10th Mar 2009 22:47
I mean how to split code in different CPP's?
I can create some of them,but how to merge code from 2 different CPP's to single one and so on?

Theory-When you know everything,but nothing works.
Practice-When everything works,but you don't know why.
Programming merges these two-Nothing works,and you don't know why.
Mulderman
20
Years of Service
User Offline
Joined: 8th Jan 2004
Location: C:\\
Posted: 10th Mar 2009 22:50
Copy paste is only chance i guess.

But it's a bit unclear what you want to do actually, first you said you need to add one or another new CPP file to project, now you're saying that you have to split the CPP and then asking how to merge the CPP.
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 10th Mar 2009 23:51
Split them however you like, for the most part. Just don't split them across functions. Rationally, each one would relate to a certain area of functionality or specifically define a class. It doesn't really matter what order the compiler compile them in or links them, as long as there's only one main() function. Each .cpp file (with its attendant .h file) will generate a separate object code module that only needs to be compiled after changes are made in the .cpp file. When a change is made in another file that modules is re-compiled and the linker takes care of forming the multiple files into a single executable.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Murloc
15
Years of Service
User Offline
Joined: 28th Dec 2008
Location: Utena,Lithuania
Posted: 11th Mar 2009 00:00
Ok.. Lilith has almost answered what I need.

So as I understand, every Cpp has to have .h of their own? And how to position code in them? I need a simple example to get into this thing..

TYAD

~Murloc.

Theory-When you know everything,but nothing works.
Practice-When everything works,but you don't know why.
Programming merges these two-Nothing works,and you don't know why.
HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 11th Mar 2009 00:15
Murloc this is a good question.

I am also learning how to use VC2008 and would like to know how this done when using arrays and function.

found lot of web pages doing it on one page but not over lots.

Dark Physics makes any hot drink go cold.
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 11th Mar 2009 01:20
Quote: "So as I understand, every Cpp has to have .h of their own?"


Header files can be used in a number of ways. The answer to your question is, "not necessarily." They can contain actual code of their own if necessary. They can declare variables used for other functions but at a global level. One of the most common uses of a header file is to present to other code segments the prototypes of the functions defined in the .cpp files.

Let's say you have a file main.cpp that contains your main() function and the bulk of your code. In another file called utility.cpp you have a number of functions you're defining. Let's call them

move (char *from, char *to){code}
copy (char *from, char *to){more code}
add (char *here, char *there){yet more code}

In the main () function in main.cpp you need to call these functions for one reason or another. By the time the compiler sees the calls to these functions it needs to know something about the function. It's possible that it's already compiled the code for utility.cpp and knows what the footprint of the three functions defined therein are. But that's not always the case so we have to let the compiler know what the footprint is of any functions it's apt to detect a call to before it sees the call.

To that end, we create a header file. In this case it would likely be utility.h and it would have

move (char *from, char *to);
copy (char *from, char *to);
add (char *here, char *there);

in it. These define the footprint of the functions defined in the utility.cpp file. It's probably advisory to put

#include "utility.h"

at the beginning of the utility.cpp file but not always necessary. Where you would really need to place it would be near the beginning of the main.cpp file. That way, when the compiler sees the directive to include the header file it reads in the prototypes before any calls in main() are made to the utility functions. You'd need to do this for any .cpp file that made calls to these functions. There is, however a bit of precaution that needs to be taken.

Since the utility.h file might be included in a number of source files it will get read in a number of times. Seeing a prototype more than once doesn't break the compile. However, if you declare variables, for example, in a header file that's read in more than once during a compile you'd find the compiler trying to declare the variable more than once and that would be a conflict.

That's why you should take precautions. Inside the utility.h file you might have something like:

#ifndef _UTILITY_H_
#define _UTILITY_H_

// prototypes
move (char *from, char *to);
copy (char *from, char *to);
add (char *here, char *there);

// necessary variables
int BigNumber;
char *Version = "1.2.2";

#endif

When the compiler encounters a directive to include utility.h the first time it sees that _UTILITY_H_ isn't defined and will include the remainder of the file in its input. If it encounters an include for utility.h a second time it will note that _UTILITY_H_ is now defined (it happened during the first pass) and skip to just after the #endif directive.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Murloc
15
Years of Service
User Offline
Joined: 28th Dec 2008
Location: Utena,Lithuania
Posted: 11th Mar 2009 09:30 Edited at: 11th Mar 2009 09:50
Ok,almost got it

Give me a sample of two cpp files code in one project, I need the sample usage, all things about headers are known.


Ok I have read it some more times and I got it! Thanks!

Theory-When you know everything,but nothing works.
Practice-When everything works,but you don't know why.
Programming merges these two-Nothing works,and you don't know why.
AlexZinn
15
Years of Service
User Offline
Joined: 17th Feb 2009
Location:
Posted: 11th Mar 2009 11:34
think like this.. the best way to ordenate the codes are functions... what i make.. i put the functions in diferent cpp file....

lets say this:

main.cpp
func.cpp
func.h

first we need in main.cpp
#include "func.h"

in the .h file we need to declate the functions like
void XXX();

and in the func.cpp

void XXX()
{
//code
}

If someone need something, add me to your msn
Was SuperKid , now AlexZinn

Login to post a reply

Server time is: 2024-11-25 17:33:49
Your offset time is: 2024-11-25 17:33:49