Getting Started with GCC

Source: Internet
Author: User

Getting Started with GCC 1. What is GCC

The full name of GCC is the gun Compiler Collection, a compiler capable of compiling multiple languages. At first, GCC was the C compiler (GNU C Compiler) and now supports C + +, Java, Pascal and other languages in addition to the C language. GCC supports a variety of hardware platforms.

2. Characteristics of GCC
    • GCC is a portable compiler that supports a variety of hardware platforms. such as arm, X86 and so on.
    • GCC is not only a local compiler, it can also cross-build cross-platform. The so-called Local compilers refer to compiled programs that can only be run in the local environment. and GCC compiled programs can be run on other platforms. For example, an embedded program can be compiled on x86 and then run on arm.
    • GCC has multiple language front ends that are used to parse different languages.
    • GCC is modular in design and can be added to support new languages and new CPU architectures.
    • GCC is free software. Anyone can use or change this software.
3. Procedures for GCC compilers

The GCC compiler consists of four procedures:

    • Pretreatment (pre-processing)
    • Compiling (compiling)
    • Compilation (assembling)
    • Links (linking)

Preprocessing actually expands the header file and the macro. During the compile phase, GCC calls compilers of different languages, such as C calling compiler CCL. GCC is actually a tool chain that invokes different tools during the process of compiling a program. Assembly phase, GCC calls assembler to assemble. The linking process links the target files required by the program to executable files. The assembler generated a relocatable target file, learned the operating system, we know that in the source program, the address is starting from 0, this is a relative address, and the program really run in memory when the address is not starting from 0, and in writing the source code can not know the absolute address of the program, so reposition ability to position source code, variables, etc. as memory-specific addresses. The following diagram shows this process, noting that the file suffix changes during the process, and that the compilation options are related to these suffixes.

This is a four-step process for GCC compilation.

4. GCC Common options

Take a look at GCC common options

Option Name function
-O Generate targets (. I,. s,. O, executables, etc.)
-E Only run the C pre-compiler
-S Tells the compiler to stop compiling after the assembler file is generated, resulting in an assembly language file extension called. s
-C Notifies GCC to cancel the connection step, which is to compile the source and generate the target file at the end
-wall Warn GCC where the source file code is problematic
-idir Directory path to add the Dir directory to the search header file
-ldir Directory path to join the Dir directory to the search library
-llib Connect to Lib Library
-G Embed debug information in the destination file so that debugging programs like GDB debug

Now that we have the source file hello.c, here are some examples of how GCC is used:

gcc -E hello.c -o hello.i   对hello.c文件进行预处理,生成了hello.i 文件gcc -S hello.i -o hello.s    对预处理文件进行编译,生成了汇编文件gcc -c hello.s -o hello.o  对汇编文件进行编译,生成了目标文件gcc hello.o -o hello 对目标文件进行链接,生成可执行文件gcc hello.c -o hello 直接编译链接成可执行目标文件gcc -c hello.c 或 gcc -c hello.c -o hello.o 编译生成可重定位目标文件

You can add the-wall option when using GCC. In the following example, if you do not add the-wall option, the compiler will not report any errors or warnings, but the results of the program are not expected:

//bad.c#include<stdio.h>int main(){ printf("the number is %f ",5); //程序输出了the number is 0.000000,结果错误 return 0; }

Using the-wall option:

Gcc-wall Bad.c-o Bad

GCC will output a warning message:

Warning:format '%f ' expects argument of type ' double ', but argument 2 have type ' int ' [-wformat=]
printf ("The number is%f\n", 5);

5. GCC compiles multiple files

Suppose there are now three files: hello.c hello.h main.c, three files are as follows:

//hello.c #include <stdio.h>#include" hello.h "void printhello () { printf ( "Hello world! \n       
//main.c #include <stdio.h>#include" hello.h "int main () {Printhello (); return 0;}        
//hello.h//仅包含函数声明#ifndef _HELLO_#define _HELLO_void printHello();#endif

Compiling these three files, you can compile them one at a time:

GCC hello.c main.c-o main generate executable file main

can also be compiled independently:

Gcc-wall-c Main.c-o MAIN.O
Gcc-wall-c Hello.c-o hello.o
Gcc-wall MAIN.O Hello.o-o Main

The benefit of standalone compilation is that when one of the modules sends a change, only the module needs to be compiled, and all files need not be recompiled, which saves compilation time.

6. Using an external library

When programming in C and other languages, we need header files to provide definitions of constants and declarations of system and library function calls. Library files are pre-compiled sets of functions that are written in terms of reusable principles. They are usually written by a set of interrelated, reusable principles that typically consist of a set of interrelated functions that are used to accomplish a common task. The advantages of using libraries are:

    • Modular development
    • Re-usability
    • Maintainability

Libraries can also be divided into static and dynamic libraries:

    • Static library (. A): The program links the library's code to the executable when it compiles the link. The static library is no longer needed when the program is running. Static libraries compare disk space, and programs cannot share static libraries. The runtime is also a comparison of memory, because each program contains a copy of the static library.

    • Dynamic libraries (. So or. sa): Programs run to link shared library code, and multiple programs share code that uses libraries, reducing the volume of the program.

The location of the general header file or library file is:

    • The Include folder under/usr/include and its subdirectories
    • The Include folder under/usr/local/include and its subdirectories
    • /usr/lib
    • /usr/local/lib
    • /lib
7. Generating a static library

In order to generate the. A file, we need to have Mr.. o file. The following line of command packages our hello.o into a static library LIBHELLO.A:

AR RCS LIBHELLO.A hello.o

AR is a gun archive tool, RCS represents replace and create, and if Libhello exists before, a new libhello.a is created and replaced.

Then you can use the static library LIBHELLO.A

Gcc-wall main.c Libhello.a-o Main

There is another way to use it:

Gcc-wall-l. Main.c-o Main-lhello "Lhello is the abbreviation of Libhello"

where -L. Indicates the location of the library file in the current directory, because LIBHELLO.A is generated by ourselves, and stored under the current record, so we need to add the-L. option. The default library file is searched under the system's directory. Similarly, the-i option is used for header file searches.

8. Generating a shared library

To generate a shared library, the rule for name is libxxx.so. The command to generate libhello.so just hello.o is:

Gcc-shared-fpic Hello.o-o libhello.so

After a shared library is generated, you can use the shared library as follows:

Gcc-wall main.o-o main-l.-lhello

This command is the same as the command that uses the static library, but the shared library is preferred when the shared library coexists with the static library.

Shared libraries are sometimes not in the current directory, and in order for GCC to be able to find shared libraries, there are several ways to do this:

    1. Copy. So files to the system shared library path, generally referred to as/usr/lib
    2. In the ~/.bash_profile file, configure the Ld_library_path variable
    3. Configure/etc/ld.so.conf, call Ldconfig Update after configuration is complete Ld.so.cache

Where the shared option represents the generation of the Share library format. The fpic indicates that the location-independent code (position independent code) is generated, and the location-independent code indicates that it runs, loads independently of the memory location, and can be loaded at any memory address.

9. Search Paths for libraries

The search path for the library follows several search principles: Search from left to right for the directory specified by-i-l, and if not found in these directories, then GCC will find it from the directory specified by the environment variable . The environment variable for the header file is C_include_path, and the environment variable of the library is Library_path. If it is still not found, the search is made from the specified directory specified by the system .

Article Link: http://www.cnblogs.com/QG-whz/p/5456720.html

Category: Linux development tools

Getting Started with GCC

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.