当前位置:网站首页>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:产生带有调试信息的目标代码
边栏推荐
- How to modify a node_ Files in modules
- 浏览器cookie转selenium cookie登录
- 行业洞察 | 新零售业态下,品牌电商应如何重塑增长?
- PyCharm汉化
- Building crud applications in golang
- Configuration of thymeleaf
- log4j.properties的配置详解
- Industry insight - how should brand e-commerce reshape growth under the new retail format?
- 聊聊 Go 语言与云原生技术
- Minimum editing distance (linear DP writing method)
猜你喜欢

Snipaste, the world's strongest screenshot software

esp32s3 IPERF例程测试 esp32s3吞吐量测试

What's the matter with Amazon's evaluation dropping and failing to stay? How to deal with it?

想学好C语言,操作符也很重要

亚马逊测评掉评、留不上评是怎么回事呢?要如何应对?

面试突击60:什么情况会导致 MySQL 索引失效?
![Dynamic programming [4] (counting class DP) example: integer partition](/img/06/4b3863bbb85582348c6f4ea7c5511e.png)
Dynamic programming [4] (counting class DP) example: integer partition

树莓派 3b+ 学习

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

JMeter connection DM8
随机推荐
Custom multithreading base class threading Event
对象序列化
秒云荣获《2022爱分析 · IT运维厂商全景报告》智能运维AIOps市场代表厂商
log4j的详情配置
Dynamic programming [III] (interval DP) stone merging
picocli-入门
微服务拆分
AI for Science: scientific research paradigm, open source platform and industrial form
Object serialization
The browser enters the URL address, and what happens to the page rendering
[fans' welfare] today, I'd like to introduce a method to collect money for nothing - convertible bonds. I personally verified that each person can earn 1500 yuan a year
LeetCode_ Fast power_ Recursion_ Medium_ 50.Pow(x, n)
Raspberry pie 3b+ learning
Thymeleaf的相关知识
号称史上最难618,淘宝数据盘点你做对了吗?
yml的配置
What is the next step in the recommendation system? Alispacetime aggregates GNN, and the effect is to sling lightgcn!
alibaba jarslink
PyCharm汉化
. Net6 access skywalking link tracking complete process
