当前位置:网站首页>gcc编译动态库和静态库
gcc编译动态库和静态库
2022-06-27 12:17:00 【xiongsiyu979】
gcc常见选项和动态库静态库
一、gcc概述
- 编译器(Compiler)把源代码转换成其他的更低级的代码(例如二进制码、机器码),但是不会执行它。
- 解释器(Interpreter)会读取源代码,并且直接生成指令让计算机硬件执行,不会输出另外一种代码。
- gcc最开始是作为C语言的编译器,但后来功能得到扩展,变得能够支持编译更多的语言,例如C++、Java、Go、Objective -C等,所以后来gcc又改名为GNU编译器套件(GNU Compiler Collection 编译器集合)。
- gcc在编译过程中是根据文件后缀名的不同来调用不同编译器的
- .c文件生成可执行文件需要经过四个步骤预编译、汇编、编译、链接
二、gcc常见选项
test.c
#include <stdio.h> int main(int argc, char *argv[]) { printf("Hello world!\n"); return 0; }
-E :预编译
gcc -E test.c执行到预编译,进行代码文本的替换操作。输出预编译结果到标准输出(一般是显示器)

-S :汇编
gcc -S test.c执行到汇编,进行源代码到汇编代码的转换,输出汇编代码(.s文件)

-c :编译
gcc -c test.c执行到编译,输出目标文件(.o文件)


- -o :更改gcc输出的文件名
如果想要使用gcc生成可执行文件,可以直接
gcc test.o生成可执行文件a.out,gcc test.o -o test则可以重命名生成的可执行文件a.out为我们指定的test(从.o文件到可执行文件的过程实际是链接的过程)
-shared与-fPIC:创建动态库
-fPIC:(Position-Independent Code)它作用于编译阶段,告诉编译器要生成与位置无关的代码(.o文件)-shared:将多个.o文件链接成一个.so动态库文件
现在将打印”Hello world!“的功能封装成一个函数,并将hello.c分别制作成动态库和静态库,在test.c的main()中调用静态库或动态库里的hello()来打印“Hello world!”
hello.h
#ifndef _HELLO_H_ //防卫式的声明 #define _HELLO_H_ extern int hello(); #endifhello.c
#include <stdio.h> int hello() { printf("Hello world!\n"); return 0; }test.c
#include <stdio.h> #include "hello.h" int main(int argc, char *argv[]) { hello(); return 0; }

- -I(大写的i):向gcc中加入头文件搜索路径

- -l(小写的L):指定需要链接的库
- -L:向gcc中加入库路径

动态链接在运行时需要export 引入动态库路径到查找路径来进行链接,而静态链接则不需要
9. -static:链接静态库


- -w:关闭警告
- -Wall:开启所有警告
- -g:产生带有调试信息的目标代码
边栏推荐
- application. Configuration information of properties
- alibaba jarslink
- Review summary of database
- Utilisation de la file d'attente des messages
- 居家办公被催之后才明白的时间管理
- Operators are also important if you want to learn the C language well
- AI for Science: scientific research paradigm, open source platform and industrial form
- Getting started with go web programming: validators
- yml的配置
- 深入理解 happens-before 原则
猜你喜欢

一个有趣的网络掩码的实验

剑指 Offer 04. 二维数组中的查找

ACL 2022 | 中科院提出TAMT:TAMT:通过下游任务无关掩码训练搜索可迁移的BERT子网络

How to participate in openharmony code contribution

Microservice splitting

行业洞察 | 新零售业态下,品牌电商应如何重塑增长?

AI for Science: scientific research paradigm, open source platform and industrial form

SSH workflow and principle

Uniapp drop-down layer selection box effect demo (sorting)

Dm8: Dameng database - lock timeout
随机推荐
DM8:达梦数据库-锁超时
Uni app sends request instructions using the escook / request miniprogram plug-in
Dm8: Dameng database - lock timeout
如何修改 node_modules 裏的文件
mysql学习1:安装mysql
SSH workflow and principle
First encounter with dynamic programming
application.properties 的配置信息
Nifi from introduction to practice (nanny level tutorial) - identity authentication
mybaitis生成器详解
How to close windows defender Security Center
中证500股指期货怎么开户,国内正规的股指期货平台有哪些,在哪里开户最安全?
How to participate in openharmony code contribution
二叉树的三种遍历方式
Configuration of thymeleaf
MIT6.031 软件构造 Reading7阅读笔记Designing Specifications(设计规范)
log4j的详情配置
Mathematical knowledge -- ideas and examples of game theory (bash game, Nim game, wizov game)
对象序列化
号称史上最难618,淘宝数据盘点你做对了吗?
