GCC Study (i)

Source: Internet
Author: User
Tags gdb debugger

 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?
  1. //test Procedures   
  2. #include <stdio.h>   
  3. #define HELLO "Hello World!!! \ n "
  4. int Main (int argc,char *argv[])
  5. {
  6. printf (HELLO);
  7. return  0;
  8. }
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?
    1. //bad.c   
    2. #include  <stdio.h>   
    3.   
    4. int   Main ( int  argc, char  *argv[])   
    5. {  
    6.     printf ( "2 + 2 = %f\n" , 4);   
    7.   
    8.     return  0;  
    9. }  
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?
    1. //hello_fu.h   
    2. #ifndef _hello_fun_h   
    3. #define _HELLO_FUN_H   
    4. void  Hello (const char *str);
    5. #endif   
Hello_fu.h#ifndef _hello_fun_h#define _hello_fun_hvoid HELLO (const char *str); #endif

[CPP]View Plaincopyprint?
  1. //hello_fu.c   
  2. #include <stdio.h>   
  3. #include "hello_func.h"   
  4. void Hello (const char *str)
  5. {
  6. printf ("Hello%s\n", str);
  7. printf ("Compiled:"__date__"at"__time__"\ n");
  8. printf ("This was line%d of file%s\n", __line__,__file__);
  9. return  ;
  10. }
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?
    1. //main.c   
    2. #include "hello_func.h"   
    3. int Main ()
    4. {
    5. Hello ("World");
    6. return  0;
    7. }
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.