gcc compilation process and simple actual combat

The gcc compilation process and simple project combat

1. The gcc compilation process

In fact, when we use c or cpp, we use a lot of libraries, which are written by others for us to use directly, which greatly facilitates us, but when we do projects, we often need to separate the functions ourselves, and then main Reference in the function, you need to use the process of compilation.
As shown in the figure: in the
Insert picture description here
above figure, our main file and input and calcu files are a folder. The input and calcu files are implemented separately. One is input and the other is calculation. Main implements digital addition calculation, but for function Clearly we will separate them, so that it is a low-coupling performance in large-scale projects.
Project source code: https://github.com/yjc-123/gcc-compile-process

First of all, we have to understand the process of gcc compilation:
Insert picture description here

There are four processes:
pre-compilation -> compilation -> assembly -> linking.
Here I mainly talk about the linking process
. The
reason why the static library becomes [static library] (.a) is because in the linking phase, the assembly is generated The object file .o and the referenced library are linked and packaged into an executable file. Therefore, the corresponding linking method is called static linking.

  • The linking of the static library to the function library is done at compile time.
  • The program has nothing to do with the function library when it is running, and it is easy to transplant.
  • Space and resources are wasted, because all related object files and the function libraries involved are linked into an executable file.
    Linux static library naming rules:
    must be "lib[your_library_name].a": lib is the prefix, the middle is the static library name, and the extension is .a

Dynamic library
Because the static library space is wasted more seriously, the dynamic library will not be linked to the target code when the program is compiled, but will be loaded when the program is running. If different applications call the same library, only one instance of the shared library is needed in memory, avoiding the problem of space waste. The dynamic library is loaded when the program is running, which also solves the trouble caused by the update, deployment and release page of the program by the static library. Users only need to update the dynamic library. Next, let’s look at the dynamic library.
Insert picture description here

Insert picture description here

These are the two ways of linking, and finally we see what is going on in actual combat.

2. Actual combat

Take my example:
Insert picture description here

First we write the main function.
main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "input.h" //导入库
#include "calcu.h"

extern int input(); //引入input.h中的函数
extern int calcu(int a,int b); //引入calcu中的函数

int main(int argc, char ** argv){
    
    

        int input_num_1 = input();
        int input_num_2 = input();

        printf("input over\n");
        int sum;
        sum = calcu(input_num_1, input_num_2);
        printf("calcu successful ,the sum is :%d\n",sum);
        return 0;
}

input.c

#include <stdio.h>
#include <stdlib.h>
int input(){
        int a;
        printf("input num:");
        scanf("%d",&a);
        return a;
} 

calcu.c

#include <stdio.h>
#include <stdlib.h>

int calcu(int a, int b){
    
    
        int sum;
        sum = a+b;
        return sum;
}

The above is the c file that mainly implements the function, but the main does not refer to the c file when it is quoted, but uses the header file written by them.
calcu.h

#ifndef _CALCU_H
#define _CALCU_H

int calcu(int a, int b);

#endif

input.h

#ifndef _INPUT_H
#define _INPUT_H

int input();

#endif

These two header files are responsible for declaring the function of the function, because these two header files are introduced in main.c, but the implementation is in input.c and calcu.c.
The next step is the Makefile for each asking price.
Makefile_input

CC = gcc

libinput.so:
        $(CC) -fPIC -shared input.c -o libinput.so
        sudo cp libinput.so /usr/lib
        sudo cp libinput.so /usr/bin
clean:
        rm -rf libinput.so

Makefile_calcu

CC = gcc
  
libcalcu.so:
        $(CC) -fPIC -shared calcu.c -o libcalcu.so
        sudo cp libcalcu.so /usr/lib
        sudo cp libcalcu.so /usr/bin

clean:
        rm -rf libcalcu.so                      

The function of the above two makefiles is to generate a dynamic library and copy the dynamic library to the system library.
Next, look at the file that generates the final Makefile:

CC = gcc
  
main:
        $(CC) main.c \
                -I ./input -L ./input -linput \
                -I ./calcu -L ./calcu -lcalcu \
                -o main
clean:
        rm -rf main

Here you need to use the three parameters of gcc -I, -L, and -l, which are the location of the joint file, the folder of the dynamic library, and the dynamic library respectively.
Why should emphasize that sudo cp libcalcu.so /usr/libwith this statement, because the default when we run, the program will usr/libgo looking for this dynamic library file, if the error will not exist, as to why we also need to specify the time to run because of the need to use.

Guess you like

Origin blog.csdn.net/qq_45125250/article/details/112182503