Click to See Complete Forum and Search --> : merging C++ source files?


marderiii
October 3rd, 2005, 12:39 AM
As a C++ file gets increasingly long, it becomes harder and harder to edit. Is there anyway I can take sections of the program and put them into another file which the pre-processor will re-insert before compiling?

I tried doing this with headers, but it wants me to declare lots of variables and stuff; isn't there some way it can just put the text back in as if it were all one file?

Marc G
October 3rd, 2005, 02:11 AM
You could create a file for example "mycode_part1.ipp" and use #include "mycode_part1.ipp" somewhere in your cpp file where you want the contents to be inserted, BUT I don't recommend this.
What you should do is refactor your code. Put related things in separate classes or separated namespaces instead of in 1 big CPP file.

SuperKoko
October 3rd, 2005, 02:13 AM
I tried doing this with headers, but it wants me to declare lots of variables and stuff; isn't there some way it can just put the text back in as if it were all one file?

Yes, there is a way : the "#include" preprocessor directive, exactly replace the directive with the text contained in the specified path.

But, that is not the way to do things.
You should program more than one module:

Generally, you write one header for each class (or maybe two or three related classes)).
This header contains only declarations (class declaration, and/or functions declarations, and/or variable declarations) and has the .h (or .hpp) extension.

Then, you write one or more module (typically one module for each class, but more if the class is complex) containing the definitions of the class's members, of the functions and of the variables.
That is, the bodies of all functions and methods (static and non-static).
The definitions of all static variables and non-member variables.
The definitions of global variables.
Definitions of non-exported functions and variables used by the module.
These modules include the header.

Any module which wants to access the classes and functions declared in the header, must include this header.

When you write a project in an IDE, you must add all the modules (.cpp) in the project's file list.
You can also add the headers file (.h) in the project's file list, it allows you to edit these files, and will probably not harm the compilation process.

If you use a command-line compiler, you must specify a list of modules where you habitually specify one module.
If the compiler does not accepts to compile more than one file at a time, you should compile each .cpp file, in a .obj file with the "-c" compiler option, and then link all the .obj files, calling explicitly the linker (you must also link to the startup object file, and to the RTL static library).

With the preprocessor, you don't need to declare all the stuff in each module using it!
You just need to include declare once in the headers, and then include the needed headers.
Moreover, you don't need to change the declarations everywhere, when a class's interface is modified; you just need to modify the class's declaration in the header.

Programing with multiple modules makes the project more maintainable, and faster to compile, and helps for encapsulation.

marderiii
October 3rd, 2005, 09:15 AM
Yes, there is a way : the "#include" preprocessor directive, exactly replace the directive with the text contained in the specified path.

Could you give an example of how to do this? I'm using Dev-C++ and I've tried several times but it is not working. What do I call the file? Is it #include <file.cpp> or #include <file.h> or something else?


Generally, you write one header for each class (or maybe two or three related classes)).
Im not using any classes at the moment, so if you give an examples; keep them really simple.


With the preprocessor, you don't need to declare all the stuff in each module using it! You just need to include declare once in the headers, and then include the needed headers.
I've been trying this, but I can't get it to work.

Marc G
October 3rd, 2005, 09:24 AM
Like I posted in my reply, you could call it "blah.ipp" and then simply use:
#include "blah.ipp"
at the exact place where you want to include the blah.ipp.
#include will simply inject the contents on the spot where it is being called.
But again, this is definitely not the preferred solution.

marderiii
October 3rd, 2005, 09:36 AM
Like I posted in my reply, you could call it "blah.ipp" and then simply use:
#include "blah.ipp"
at the exact place where you want to include the blah.ipp.
#include will simply inject the contents on the spot where it is being called.
Well, I did just that -- I made the file char.h and its only contents was char command[256]; and then I inserted #include "char.h" where the declaration had been -- and when I tried to compile, it said command was undeclared! Then I tried again as .ipp but still no luck.


But again, this is definitely not the preferred solution.
I dunno, with C++ there always seems to be a "better" way to code something -- but for now, Id like the simplest way to break my program up into separate files... and then later Ill try to figure out more complex ways of doing it.

Marc G
October 3rd, 2005, 12:42 PM
Strange, that should work.
What compiler are you using?
Try to let the compiler generate a pre-processed file and take a look at how it looks.

marderiii
October 3rd, 2005, 02:58 PM
What compiler are you using?
Dev-C++ -- in any case, if you would create a simple example that works for you; that would at least allow me to see whether I am doing it the correct way.


Try to let the compiler generate a pre-processed file and take a look at how it looks.
I have no idea how, I've never generated a pre-processed file.

Marc G
October 4th, 2005, 02:24 AM
// Code for main.cpp
#include <iostream>
using namespace std;
int main()
{
#include "to_include.ipp"

strcpy(command, "Test string");
cout << command;
return 1;
}
// Code in to_include.ipp"
char command[256];