1. Run gcc/egcs
GCC is the most important software development tool in Linux. GCC is the gnu c and C ++ compilers. In fact, GCC can compile three languages: C, C ++, and objectc (an object-oriented extension of C ). The GCC command can be used to compile and connect the C and C ++ source programs at the same time.
# DEMO #: Hello. c
If you have two or a few C source files, you can easily use GCC to compile, connect, and generate executable files. For example, if you have two source files main. C and factorial. C, compile a program to calculate factorial.
List factorial. c
#include #include int factorial (int n){if (n <= 1)return 1;elsereturn factorial (n - 1) * n;} |
Listing main. c
#include #include int factorial (int n);int main (int argc, char **argv){int n;if (argc < 2) {printf ("Usage: %s n ", argv [0]);return -1;}else {n = atoi (argv[1]);printf ("Factorial of %d is %d. ", n, factorial (n));}return 0;} |
The following command can be used to compile and generate executable files and execute programs:
$ gcc -o factorial main.c factorial.c $ ./factorial 5 Factorial of 5 is 120. |
GCC can be used to compile both C and C ++ programs. Generally, the C compiler uses the extension of the source file to determine whether it is a C program or a C ++ program. In Linux, the extension of the C source file is. C, and the extension of the C ++ source file is. C or. cpp.
However, the GCC command can only compile the C ++ source file, but cannot automatically connect to the library used by the C ++ program. Therefore, the G ++ command is usually used to compile and connect the C ++ program. The program automatically calls GCC for compilation.
Suppose we have the following C ++ source file (hello. C ):
#include void main (void){ cout << "Hello, world!" << endl;} |
You can call the G ++ command to compile, connect, and generate an executable file as follows:
$ g++ -o hello hello.C $ ./hello Hello, world! |
Ii. Main options of GCC/egcs
-ANSI only supports the ANSI standard C syntax. This option will disable certain characteristics of gnu c, such as ASM or typeof keywords.
-C only compiles and generates the target file.
-Dmacro defines the macro with the string "1.
-Dmacro = defn: Define the macro with the string "defn.
-E only runs the C pre-compiler.
-G generates debugging information. The GNU Debugger can use this information.
-Idirectory: specify an additional header file to search for the path directory.
-Ldirectory: specify an additional function library to search for the path directory.
-Search for the specified library when connecting to llibrary.
-Msung optimizes code for 486.
-O file: generate the specified output file. Used to generate executable files.
-O0 is not optimized.
-O or-O1 optimized code generation.
-O2 is further optimized.
-O3 is further optimized than-O2, including the inline function.
-Shared shared object generation. It is usually used to create a shared library.
-Static prohibit the use of shared connections.
-Umacro undefines macro macros.
-W does not generate any warning information.
-Wall generates all warning information.