Cross compilation
Use different cross compiler toolchains to compile source code , Can run on processors of different frameworks .
Such as :
- X86
gcc -o hello hello.c
The file compiled by the above command can be in X86 Up operation , because gcc The compiler tool chain is for PC Compilation of .
- ARM
arm-linux-gnueabihf-gcc -o hello hello.c
The file compiled by the above command can be in ARM Up operation .
Brief knowledge points **
- The role of header file :
- Statement (declare)
- c Role of documents
- Definition 、 Realization (define)
- Header file addressing
- The default path : In the compiler include Catalog
- Can be specified :
#include "xx/xx.h"
: From the current file path- Compile with "-I" Option assignment
- c Where are the external functions in the file , Such as printf function :
- library :
- The default path : In the compiler lib Catalog
- Can be specified :
- Compile with "-I" Specify the library file
- Compile with "-L" Specify Library Directory
- library :
- How to determine the default path of header file in cross compiler ?
- Go to the cross compiler Directory , Use find Command to find , Such as
- perform "find -name "stdio.h""
- Go to the cross compiler Directory , Use find Command to find , Such as
- How to determine the default path of Library in cross compiler ?
- Go to the cross compiler Directory , perform "find -name lib", Go in and have a look , There's a lot of .so Inside the file is the path to be found .
Some of the concepts
- Cross compile... In the toolchain include Contents and lib Catalog :
- include Store header file
- These header files are usually function declarations , There are also some variable declarations , Namespace , Macro definition ,typedef wait
- lib Deposit obj Of documents ( Yes gcc Say for .o)
- in other words , Some library files , People don't want you to see the source code , It's just for you to generate in the middle obj file
- include Store header file
GCC compiler
PC The on-board compiler tool chain has gcc、ld、objcopy、objdump wait , They can compile programs in X86 Run on the platform .
To make the compiled program in ARM Up operation , You have to use cross compiler toolchains xxx-gcc、xxx-ld etc. ( Different versions of the compiler have different prefixes , Such as arm-linux-gcc).
GCC Briefly use
GCC The build process **
One C/C++ The document goes through Preprocessing 、 compile 、 assembly 、 link etc. 4 It takes three steps to become an executable file .( Use in daily communication compile Collectively, these four steps )
- Preprocessing , In the process of pretreatment , For files in the source code file, include (include)、 Precompiled sentences ( Such as macro definition define etc. ) Expand , Generate .i file . Can be understood as the header file code 、 Macro and the like are converted to more pure C Code , However, the generated files are .i For the suffix .
- compile , Put the pretreated .i Files are compiled into assembly language , Generate .s file , That is, code from C Language into assembly language , This is a GCC What the compiler does .
- assembly , Assemble the assembly language files , Generate target file .o file , Each source file corresponds to a target file . That is to convert assembly language code into machine code , This is a as What the assembler does .
- link , Finally, each source file corresponding to .o The files are linked up , Just generate an executable file , This is the linker ld Work done .
- There are two kinds of links
- Dynamic links
- GCC Default options at compile time .
- When an application is running, it loads the external code base .
- Static links
- Use options when linking
–static
- It will package all the libraries used in the compilation phase into its own executable program .
- Use options when linking
- Dynamic links
- There are two kinds of links
Common compilation options
Options | describe |
---|---|
-E | Preprocessing , During the development process, you want to quickly determine that a macro can be used “-E -dM” |
-c | It's been preprocessed 、 compile 、 assembly , But no link |
-o | Specify output file |
-I | Specify the header directory ( Capitalization i) |
-l | Specifies which library file to link to ( A lowercase letter L) |
-L | Specify the link time library file directory |
Compile multiple files
- Compile together 、 link :
Such as :
gcc -o hello main.c hello.c
- Compile separately , Unified link :
Such as :
gcc -c -o main.o main.c
gcc -c -o hello.o hello.c
gcc -o hello main.o hello.o
Make 、 Using dynamic libraries
- Make 、 compile :
gcc -c -o main.o main.c
gcc -c -o hello.o hello.c
gcc -shared -o libhello.so hello.o hello1.o // (* Multiple can be used .o Generate a dynamic library *)
gcc -o hello main.o -lhello -L < Directory path > // (* The path to libhello.so File directory *)
- function
- hold libhello.so Put it in PC Or board /lib Under the table of contents , And then run test Program .
- If you don't want to put libhello.so Put it in /lib, You can also put it in a new directory , Such as /mylib, And then execute :
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/a
./hello
( In fact, it's modification The environment variable of the library .)
Make 、 Use static libraries
gcc -c -o main.o main.c
gcc -c -o hello.o hello.c
ar crs libhello.a hello.o hello1.o // (* Multiple can be used .o Generate a static library *)
gcc -o hello main.o libhello.a // (* If .a Not in the current directory , You need to specify its absolute path or relative path *)
Be careful : There is no need to put hello.a Pull the static library file onto the board
A very useful option
gcc -E main.c // See the preprocessing results , For example, which header file is
gcc -E -dM main.c > h.txt // Expand all the macros , Save to h.txt In the document
gcc -Wp,-MD,yl.dep -c -c -o main.o main.c // Generate dependency file yl.dep, Back Makefile use
Reference resources
- Weidong mountain
- Wildfire
- gcc-gun
- Suggest : When you want to see the source code of the library file , Such as printf、malloc And so on. , Can enter the glibc Official website Study