Under the Windows platform, we generally use the VC's compiler CL to compile our source programs, but under the Linux platform, people generally use GCC (GNU Compiler Collection). GCC also has a transplanted version -djgpp under the Windows platform. With DJGPP, we can use GCC under the Windows platform.
GCC and CL, you can compile C, C code, then why should we use GCC? I think the most important thing is because she is open source Free Software!
The following text briefly introduces the method of use of GCC.
First, we start using the GCC to build the Hello World! Suppose we have written the following programs:
/ ************************************************** ****************
Main.c file
*********************************************************** *************** /
#include
Int main (int Argc, char * argv [])
{
PRINTF ("Hello World!");
Return 0;
}
To compile this program, we can:
GCC main.c -o hello.exe
The first parameter tells GCC to compile and link main.c this file, the second parameter -o specifies the file name of the generated executable, here is Hello.exe. In the current directory we enter the command hello, the screen will output Hello World!
This is the easiest way of use of GCC.
When our project is not only a file that needs to be compiled, we can pass the list of files to be compiled as a parameter to GCC, like this:
GCC file1.c file2.c file3.c -o out.exe
But there is a disadvantage that every other source file must be recompiled. The correct approach is to compile two steps.
The first step is to generate a target file. With the -c option, only the target file is generated when the GCC compiles the source, and does not link. Such as:
gcc -c main.c
After executing this command, you will generate a main.o target file in the current directory, you can also use -o to specify the generated file name.
Step 2, link the target file. For the target file generated in the first step, we can link these target files with GCC, and GCC calls LD to perform this work, link the target file, will generate a can be generated. Such as:
gcc main.o -o hello.exe
Once the command is executed, the executable of Hello.exe is generated in the current directory.
When our project has multiple source files, we can generate the corresponding target files for each source file to generate the corresponding target file, and finally generate the executable files. The above steps, we generally write a Makefile to let the computer execute automatically. This saves the time of compiler, and is especially useful for some major projects.
The following brief introduces the compilation options for GCC:
-o file: Specifies the generated file name; when generating the target file, the default is file.o, when generating an executable file, the default file name is a.exe; the corresponding CL option is / out: File
-c only compiles without link; the corresponding CL option is / c
-Dfoo = BAR Defines a value of a pre-compiled macro foo as bar; corresponding CL option is / dfoo = bar
-IdirName Defines the search directory of the include file; the corresponding CL option is / i-ldirname defines the search directory of the lib file; the corresponding CL option is / L
-lfoo links to the lib file foo, the corresponding CL option is -foo
-g generated executable contains debugging messages; the corresponding CL option is / zi
-ggdb generated executable contains GDB recognizable debugging (debug) message
-O Optimize compilation and generated code; the corresponding CL option is / O
-On Specifies the level of code optimization, 0 <= n <= 3
-Wall produces useful warning messages available for GCC
GCC's official website: gcc.gnu.org
DJGPP's official website: www.delorie.com/djgpp
MA811 at 2004/10/22