|
-
Jul 30th, 2001, 12:52 PM
#1
big projects
Hi.
I would like to know what is the "standard" way of dividing a project into separate files.
Until this point i only made small projects with standard libraries and never had so many lines of code to be compiled together, so i don't know how to do it.
No DLL's or so, just .h & .cpp
Anybody knows what is the right way to do it? An address to a tutorial would be fine too.
I thought that the #include preprocessor directive only pasted the contents of the file in place of the #include statment, but it seems to do something else. Does that relate to the extern keyword?
Also i have observed that a file included in my project (Borland)also gets compiled even though i don't #include it. How come?
thanks
-
Aug 28th, 2001, 07:11 AM
#2
(supposing you use vc++)
Some jargon first in case you don't know it (I don't think so but whatever...)
"Source files" are the files with the extensions .cpp (C++) and .c (C)
"header files" usually have the extension .h, but can have others too (.hh, .hpp, none at all....). Those files are included in source files
VC++ compiles every source file in your project unless you have explicitly told it otherwise (right-click a file in the file overview and select properties, where you can exclude the file from compilation). It does not compile header files, but rather inserts them into the source files before compilation. Therefore, a header file that is not included anywhere will not be compiled. You don't need to (and can't) include source files. Header files can include other header files.
A good way to divide projects is to have one source file for each class implementation, one source file for the main function and maybe one more for global helpers. You should have one header file for each group of classes, or even for a single class if it's a large one. You should have one or more header files for the helper functions.
About variable scopes:
"Global Scope" means that this variable/function is available in each source file if the name is given (function prototypes). Those variables have "global linkage".
"Local Scope" means that this variable/function is available only in the source file where it is declared. Those variables have "local linkage".
Global functions have always external scope, unless you declare them as static.
Global variables also have external scope unless you declare them as static.
To access functions from another source file, you have to provide the function prototype. To access variables, you have to declare them with the keyword extern. Extern may not be combined with static.
Some examples:
file1.cpp:
Code:
int a; // failure, double defined
static int b; // ok, local scope
int c; // ok
static int someFunc()
{
masses of code
}
file2.cpp
Code:
int a; // failure, double defined
static int b; // ok, local scope
extern int c; // ok, only declaration
int someFunc();
int func()
{
int i = someFunc(); // error, out of scope
}
Some notes on c++:
Methods usually have global linkage, this is why you define them in a seperate source file. Inline methods have only local linkage, so you have to define them in the header file.
To seperate inline functions from the declarations, you have to write code like this:
Code:
// C.h
class C
{
// methods and member functions
};
#include "C.inl"
The .inl is a naming convention nut not a requirement. It says that inside this file are definitions of inline functions (don't forget that the #include directive is replaced by the content of the file, so the functions are "in" the header file).
I hope that helps
All the buzzt
CornedBee
-
Aug 28th, 2001, 08:08 PM
#3
thanks
Thank you CornedBee. This is the sort of thing i've been looking for but didn't find til now.
I am running on Borland but i guess there won't be much difference anyway.
Thankye you've been really helpful.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|