about GCC
1, GCC is the GNU Compiler collection abbreviation. Originally a C language compiler (GNU C Compiler), the author was Richard Stallman, the founder of the GNU Project, and now supports multiple languages such as C, C + +, Java, Pascal, Ada, COBOL, etc.
2. Main Features:
GCC is a portable compiler that supports a variety of hardware platforms, and even provides complete support for mmix such as the Don Knuth design.
GCC is more than just a local compiler, it can cross-platform compile (locally compiled programs that can run on top of other platforms).
GCC has multiple language front ends that are used to parse different languages.
GCC is modular and can be added to support new languages and new CPU architectures
GCC is free software
procedures for GCC compilers
1) pretreatment (pre-processing) [-e] #头文件展开
2) compile (compiling) [-s]
3) Assembly (assembling) [-c]
4) Link (linking) [no option]
Common Options
Options |
Role |
-E |
preprocessing , generating. I files |
-S |
compiling , generating. S assembly files |
-C |
compilation , compiling source code generation. O Target file |
Null |
Links, do not require any options |
-O |
Generate target files, such as. i,.o,.s, executables, default to a.out files, etc. |
-wall |
Warn GCC where source code is problematic |
-i[dir] |
To add dir to the search path for the header file |
-l[dir] |
Search path to add dir to the library file |
-l[lib] |
Link Lib library |
-G |
Embed debug Information in the destination file for GDB debugger debugging |
-O |
Optimize the compiled code |
-W |
Turn off all warning messages [not recommended] |
Example
[CPP]View Plaincopyprint?
- //test Procedures
- #include <stdio.h>
- #define HELLO "Hello World!!! \ n "
- int Main (int argc,char *argv[])
- {
- printf (HELLO);
- return 0;
- }
Test Program # # # <stdio.h> #define HELLO "Hello World!!! \ n "int main (int argc,char *argv[]) { printf (HELLO); return 0;}
Operation:
GCC-E hello.c-o hello.i (pretreatment)
Gcc-s hello.i-o Hello.s (compile, generate assembly code)
Gcc-c Hello.s-o hello.o (compilation, generating binary code)
GCC hello.o-o Hello (link: No additional options are required to generate the executable file)
GCC hello.c-o Hello (direct compile link to executable target file)
Gcc-c hello.c or Gcc-c hello.c-o hello.o (build to relocate target file)
Use of-wall
It is recommended to add the-wall option to beginners. Some programs do not add the-wall option, the compiler does not report any errors, but the results are not expected. As follows:
[CPP]View Plaincopyprint?
- //bad.c
- #include <stdio.h>
-
- int Main ( int argc, char *argv[])
- {
- printf ( "2 + 2 = %f\n" , 4);
-
- return 0;
- }
Bad.c#include <stdio.h>int Main (int Argc,char *argv[]) { printf ("2 + 2 =%f\n", 4); return 0;}
gcc compiles multiple files
Compile once
gcc [-wall] hello_fn.c main.c–o Newhello
Standalone compilation
Gcc-wall-c Main.c-o MAIN.O
Gcc-wall-c Hello_fn.c-o HELLO_FN.O
Gcc-wall main.o hello_fn.o-o Newhello #链接生成可执行文件
Pros: If you just change a module, you don't have to compile all the modules again to save compilation time [recommended]
Attached-Test procedure
[CPP]View Plaincopyprint?
- //hello_fu.h
- #ifndef _hello_fun_h
- #define _HELLO_FUN_H
- void Hello (const char *str);
- #endif
Hello_fu.h#ifndef _hello_fun_h#define _hello_fun_hvoid HELLO (const char *str); #endif
[CPP]View Plaincopyprint?
- //hello_fu.c
- #include <stdio.h>
- #include "hello_func.h"
- void Hello (const char *str)
- {
- printf ("Hello%s\n", str);
- printf ("Compiled:"__date__"at"__time__"\ n");
- printf ("This was line%d of file%s\n", __line__,__file__);
- return ;
- }
Hello_fu.c#include <stdio.h> #include "hello_func.h" void Hello (const char *str) { printf ("Hello%s\n", str ); printf ("Compiled:" __date__ "at" __time__ "\ n"); printf ("This was line%d of file%s\n", __line__,__file__); return;}
[CPP]View Plaincopyprint?
- //main.c
- #include "hello_func.h"
- int Main ()
- {
- Hello ("World");
- return 0;
- }
Main.c#include "hello_func.h" int main () { Hello ("World"); return 0;}
Attached-makefile
CC = gcc
CFLAGS =-wall-g
BIN = Main
SOURCES = $ (wildcard *.c)
OBJECTS = $ (SOURCES:.C=.O)
. Phony:all Clean
All: $ (BIN)
$ (BIN): $ (OBJECTS)
$ (CC) $ (CFLAGS)-o [email protected] $^
%.O:%.c
$ (CC) $ (CFLAGS)-C $<-o [email protected]
Clean
-RM-RF $ (BIN) $ (OBJECTS)
gcc needs to identify the file name extension
Extended Name |
Meaning |
. C |
C Source File |
. cpp/cc |
C + + source files |
. o |
Target file |
. s |
assembly language Source file |
. a/.so |
The compiled library file |
using GCC to find the wrong place
The discovery was wrong at the link stage!
The code is different after C + + compilation is complete
Reprint http://blog.csdn.net/zjf280441589/article/details/40017055
If you have copyright issues, please contact qq:858668791
GCC Study (i)