Get the App
SLTechnology News&Howtos  ›  Development  › 

How to build your own function library

Shulou Source: shulou.com Published: 2022-06-03 01:31:38 09月16日 Update

This article introduces the relevant knowledge of "how to build your own function library". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

1. What does the gcc compiler do?

As the saying goes, know yourself and know your enemy, and you will never be defeated in a hundred battles.

First of all, with an example to take you to understand the gcc compilation process.

//main.cint sum (int *a, int n);int array[2] = {1, 2};int main(){ int val = sum(array, 2); return val;}//sum.cint sum(int *a, int n){ int i, s = 0; for(i=0; i < n; i++){ s += a[i]; } return s;}

Suppose a project consists of two source programs. The main function initializes an array of integers and then calls a sum function defined in another file to sum the elements of the array.

Normally, we would type gcc -Og -o prog main.c sum.c directly from the command line to compile the binary executable ELF object file prog directly. So what did the gcc do in the meantime?

(Note: You can add the parameter-v to the gcc command to observe the working process.)

See photo

static link

1.1 cpp pretreatment cpp main.c -o main.i

The preprocessor cpp translates the source code main.c into an ASCII intermediate file main.i.

1.2 ccl -Og main.i -o main.s

The C compiler ccl translates the intermediate file into an ASCII assembly language file main.s.

Note: -O parameter selection optimization level, from low to high g

Tags: File function compilation link target program statement static parameter command directory process phase project production run select binary memory content Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Tech Info Microsoft macOS Docker