Step 1: Preprocessor processing
Step 2: Compile from code file to object file
Step 3: Link from object file to executable program
A program can consist of several files, which are first subjected to steps 1 and 2 separately. Finally, the files are linked to a complete program in step 3.
# and are called directives.
Common directives include #include,
which inserts code from another file,
and #define, which defines a macro.
We have used the #include directive in the past
to use the standard library, giving the file name in angle
brackets (such as <iostream>).
When we include code from a file we have written ourselves, we put
the file name in quotation marks.
In the following example, the code from the file add.inc will be
inserted into the code in the file code.cpp by the precompiler.
// add.inc
cout << "Hello!\n";
// code.cpp
#include <iostream>
using namespace std;
int main() {
#include "add.inc"
}
This is the same as the following code:
// code.cpp
#include <iostream>
using namespace std;
int main() {
// add.inc
cout << "Hello!\n";
}
The #define directive defines a macro,
which means that a certain string in the code is replaced
by another string. For example, the following macro
replaces pb with push_back.
#define pb push_backThis macro will mean that the code
v.pb(1);changes in the precompiler processing to the following:
v.push_back(1);
code.cpp and test.cpp.
The test.cpp file has the test function,
which is presented in the header file test.h.
The idea is that code.cpp knows the header file
thanks to the test function, although it does not see the function definition.
// test.h void test();
// test.cpp
#include <iostream>
using namespace std;
void test() {
cout << "Hello!\n";
}
// code.cpp
#include <iostream>
#include "test.h"
using namespace std;
int main() {
test();
}
Compilation of the program takes both filenames
code.cpp and test.cpp:
$ g++ code.cpp test.cpp -o codeYou can also compile files separately:
$ g++ code.cpp -c $ g++ test.cpp -c $ g++ code.o test.o -o codeHere, the first two lines create the compiled object files
code.o and test.o
and the last line links these files together
into an executable program.
main function
definition with argc
(number of parameters)
and argv (the parameters themselves, in character arrays).
For example, the following code will print everything given to the program as parameters:
int main(int argc, char **argv) {
for (int i = 0; i < argc; i++) {
cout << i << " " << argv[i] << "\n";
}
}
Now we can start the program:
$ ./code monkey banana harpsichord 0 ./code 1 monkey 2 banana 3 harpsichordThe first parameter is always the program name, i.e.
argc is always at least 1,
and after that come any other parameters,
separated by spaces.
main function type is int,
it may return an integer.
If the main function does not have a return line,
it returns 0 by default,
whose usual interpretation is that the program ended successfully.
However, if the program returns something else (non-zero),
this is interpreted as an error.
For example, the following program returns 13 as an error:
int main() {
return 13;
}
After running the program, we can check its return value.
In Linux, for example, we have access to the return value
from the command line via variable $?:
$ ./code $ echo $? 13For example, we could use a return value in a script that executes the program and checks after execution whether the program ended successfully or an error occurred.