Brief knowledge of Linux GCC**

Li Zhuming 2020-11-09 16:59:10
brief knowledge linux gcc



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 **

  1. The role of header file :
    1. Statement (declare)
  2. c Role of documents
    1. Definition 、 Realization (define)
  3. Header file addressing
    1. The default path : In the compiler include Catalog
    2. Can be specified :
      1. #include "xx/xx.h" : From the current file path
      2. Compile with "-I" Option assignment
  4. c Where are the external functions in the file , Such as printf function :
    1. library :
      1. The default path : In the compiler lib Catalog
      2. Can be specified :
        1. Compile with "-I" Specify the library file
        2. Compile with "-L" Specify Library Directory
  5. How to determine the default path of header file in cross compiler ?
    1. Go to the cross compiler Directory , Use find Command to find , Such as
      • perform "find -name "stdio.h""
  6. How to determine the default path of Library in cross compiler ?
    1. 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

  1. Cross compile... In the toolchain include Contents and lib Catalog :
    1. include Store header file
      1. These header files are usually function declarations , There are also some variable declarations , Namespace , Macro definition ,typedef wait
    2. lib Deposit obj Of documents ( Yes gcc Say for .o)
      1. 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

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

  1. 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 .
  2. 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 .
  3. 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 .
  4. 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 .

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

  1. Compile together 、 link :
    Such as :
gcc -o hello main.c hello.c
  1. 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

  1. 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 *)
  1. function
    1. hold libhello.so Put it in PC Or board /lib Under the table of contents , And then run test Program .
    2. 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
版权声明
本文为[Li Zhuming]所创,转载请带上原文链接,感谢

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云