nesting .cpp files

Started by
3 comments, last by jpetrie 14 years ago
Program Organization For C++ programs that only use one source code file and the standard library, the only rule is to declare things before using them: type declarations before object declarations, and function declarations or definitions before calling them. However, implicitly inlined member functions may use members not yet declared, and templates may use names as long as they are declared before instantiation. class Complex { double real() const {return re;} // OK double re, im; }; Global and member functions (unless inlined or templated) and global or class static objects are separately compilable units, and may appear in separate source code (.cpp) files. If they are defined and used in different files, then a declaration is needed. To insure that the declaration and definition are consistent, the declaration should be in a shared header file. A shared header conventionally has a .h extension, and is inserted with a #include "filename.h", using double quotes to indicate that the file is in the current directory. Global variables are declared with extern without initialization. // prog.h // prog1.cpp // prog2.cpp extern int x; #include "prog.h" #include "prog.h" int f(); int x=0; int f() { int main() { return x; f(); } return 0; } To compile, g++ prog1.cpp prog2.cpp -o prog This produces two object files (prog1.o, prog2.o), and then links them to produce the executable prog. g++ also accepts .o files, which are linked only, saving time if the .cpp file was not changed. To compile without linking, use -c. To optimize (compile slower but run faster), use -O. The UNIX make command updates the executable as needed based on the timestamps of source and .o files. It requires a file named Makefile containing a set of dependencies of the form: file: files which should be older than file (tab) commands to update file Dependencies may be in any order. The Makefile is executed repeatedly until all dependencies are satisfied. # Makefile comment prog: prog1.o prog2.o g++ prog1.o prog2.o -o prog prog1.o: prog1.cpp prog.h g++ -c prog1.cpp prog2.o: prog2.cpp prog.h g++ -c prog2.cpp Compiler options for g++. Other compilers may vary. g++ file1.cpp Compile, produce executable a.out in UNIX g++ file1.cpp file2.o Compile .cpp and link .o to executable a.out g++ -Wall Turn on all warnings g++ -c file1.cpp Compile to file1.o, do not link g++ -o file1 Rename a.out to file1 g++ -O Optimize executable for speed g++ -v Verbose mode g++ -DX=Y Equivalent to #define X Y g++ --help Show all g++ options gxx file1.cpp Compile in Windows MS-DOS box (DJGPP) to A.EXE Anything which is not a separately compilable unit may appear in a header file, such as class definitions (but not function code unless inlined), templated classes (including function code), templated functions, and other #include statements.
Advertisement
Ok?
Seems to be a dump of part of
http://cs.fit.edu/~mmahoney/cse2050/how2cpp.html

Starting at the Program Organization heading
Quote:Original post by BosskIn Soviet Russia, you STFU WITH THOSE LAME JOKES!
Okay?

But note that "extern int x; #include "prog.h" #include "prog.h"" is invalid in C/C++ as per chapter 16, "Preprocessing Directives":

Quote:The first token in the sequence is a # preprocessing token that is either the first character in the source file (optionally after white space containing no new-line characters) or that follows white space containing at least one new-line character.
So, is there a question here, or should I close this?

This topic is closed to new replies.

Advertisement