Pretreatment, dynamic and static library

  1. c language compilation process

    • gcc -E hello.c -o hello.i 1. Pretreatment

    • gcc -S hello.i -o hello.s 2.编译

    • gcc -c hello.s -o hello.o 3. 汇编

    • gcc hello.o -o hello.elf 4. 链接

  2. Precompiled

    • .C file in the head start, macro expansion

    • The resulting file is a file .i

  3. Compile

    • After the pretreatment .i file assembler file generated .s

  4. compilation

    • .S assembly files to generate object files .o

  5. link

    • The link .o files into object files

  6. include

    • #include <> // include file in angle brackets, the header file to find the path specified in the system

    • #include "" // use double quotes to include the header file, first look for header files in the current directory, can not find, and then find the system specified in the Road King.

    • Note: include is often used to include the header file that can contain .c file, but we do not include .c

    • Because the file will include contains precompiled be deployed, if a .c included several times, expanded many times, will lead to the definition of duplicate functions, so we do not include the .c file

    • Note: just include pre-processing operations such as pre-processed and syntax checking will not, at this stage there are grammatical errors will not be saved, the second stage of the compilation stage before syntax checking

  7. define

    • Define the macro using the define to define

    • Macro with no parameters

      • #define PI 3.14 PI name is usually capitalized

      • Then pre-compile time if the code appears in the PI will use to replace 3.14

      • Benefits macro: As long as modifying the macro, and then the rest of the time will be re-precompiled replacement

      • Note: Do not add a semicolon behind the macro definition.

      • The role of the macro definitions, defined from the place to the end of this document

      • If you want to stop in the middle of the macro is defined

        • #undef PI // terminate the role of PI

    • Parameterized macro

      • #define S (a, b) a * b (may set up their own)

        • Note parameterized macro and parameter b is not a type name

        • S (2,4) and then replaced in the future when pretreated alternative argument string into a parameter, other reserved characters, 2 * 4

        • usage

          • int main () {

          • int num;

          • Surely = S (2.4); // num = 2 * 4;

          • num = S (2 + 3,5) // num = 2 + 3 * 5 (2 + 3) Alternatively Alternatively a 5 b

          • }

    • Parameterized macro and the difference with a function parameter

      • Parameterized macro is called many times will unfold many times, when the process of execution of the code is not a function call, you do not need to push popped. So take a macro parameter is a waste of space, because they were expanded many times, saving time

      • Parameterized function code is only one, has a code segment, when called to fetch the code segment, when the push is called ,, popped. There is a process called

      • So, with the reference function is a waste of time, saves space

      • Parameter with the function parameter is typed with no reference macro formal parameter type name

    • Selective compilation

      • #ifdef AAA

      • A code segment

      • #else

      • Two code segments

      • #endif

      • If we had defined the current .c ifdef top AAA, compile the code on a segment, or compile the code section two

      • Note the difference and if else statements, if else statements will be compiled, executed by selective conditions, selectively compiled, only one code is compiled ???

      • 2

      • #ifndef AAA If you do not define this macro statements in the code, or executing code two

      • A code segment

      • #else

      • Two code segments

      • #endif

      • And the first complementary

      • This method is often used to prevent the further header file contains duplicate

      • usage

        • #ifndef _def _ h _

        • #define _def _ h _

        • Block

        • #endif This prevents duplicate headers

      • 3

      • #if expression

      • A block

      • #else

      • Blocks two

      • #endif

      • If the expression is true, the first piece of code compile, or compile the second paragraph of the code

      • Selective compilation are precompiled stage doing things

  8. Static and dynamic libraries

    • Dynamic compilation

      • Dynamic compiler is used to compile a dynamic library files

      • gcc hello.c -o hello

      • We are using the default dynamic compilation method

    • Statically compiled

      • Static library files used to compile statically compiled

      • gcc -static hello.c -o hello

    • The difference between static and dynamic compilation compilation

      • Library file format used is not the same

        • Dynamic compiled using dynamic libraries, statically compiled using static library

      • note

        • Statically compiled static library should be compiled into an executable file packing to go

        • Dynamic compilation package will not dynamically compiled into an executable program file, it just compiled and linked relationship

    • Production static library

      • gcc -c mylib.c -o mylib.o

      • ar rc libtestlib.a mylib.o

      • Note: when static library named lib must begin with ending .a

      • Compiler:

        • method 1

          • gcc -static mytest.c libtestlib.a -o mytest

        • Method Two: Road King can specify the header files and library files

          • For example we will move to the next libtestlib.a mylib.h / home / teacher

          • mv libtestlib.a mylib.h /home/teacher

          • Compiler command

            • gcc -static mytest.c -o mytest -L/home/teacher -ltestlib -I/home/tearcher

          • note

            • -L is specified library file path

            • -l specify which library to find the specified file name in front of the library just behind lib .a part

            • Specifies the path to the header file

          • Method Three

            • It can libraries and header files stored in the default path specified under

            • Road King is the default library file / lib or / usr / lib

            • Header file default path is / usr / include

              • sudo mv libtestlib.a /usr/lib

              • sudo mv mylib.h /usr/include

            • Compiler command

              • gcc -static mytest.c -o mytest -Itestlib

              •  

Guess you like

Origin www.cnblogs.com/fengzi759/p/11618706.html