如何用gcc编译动态库

如何用gcc编译动态库,第1张

今天要用到静态库和动态库,于是写了几个例子来巩固一下基础。

hello1.c ————————————————————

#include <stdio.h>

void print1(int i) { int jfor(j=0j<ij++) { printf("%d * %d = %d\n",j,j,j*j)}}

hello2.c _________________________________________________

#include <stdio.h>

void print2(char *arr) { char cint i=0while((c=arr[i++])!='\0') { printf("%d****%c\n",i,c)} }

hello.c ____________________________________________________

void print1(int)void print2(char *)

int main(int argc,char **argv) { int i=100char *arr="THIS IS LAYMU'S HOME!"print1(i)print2(arr)

return 0}

可以看到hello.c要用到hello1.c中的print1函数和hello2.c中的print2函数。所以可以把这两个函数组合为库,以供更多的程序作为组件来调用。

方法一:将hello1.c和hello2.c编译成静态链接库.a

[root@localhost main5]#gcc -c hello1.c hello2.c

//将hello1.c和hello2.c分别编译为hello1.o和hello2.o,其中-c选项意为只编译不链接。

[root@localhost main5]#ar -r libhello.a hello1.o hello2.o

//将hello1.o和hello2.o组合为libhello.a这个静态链接库

[root@localhost main5]#cp libhello.a /usr/lib

//将libhello.a拷贝到/usr/lib目录下,作为一个系统共享的静态链接库

[root@localhost main5]#gcc -o hello hello.c -lhello

//将hello.c编译为可执行程序hello,这个过程用到了-lhello选项,这个选项告诉gcc编译器到/usr/lib目录下去找libhello.a的静态链接库

以上的过程类似于windows下的lib静态链接库的编译及调用过程。

方法二:将hello1.o和hello2.o组合成动态链接库.so

[root@localhost main5]#gcc -c -fpic hello1.c hello2.c

//将hello1.c和hello2.c编译成hello1.o和hello2.o,-c意为只编译不链接,-fpic意为位置独立代码,指示编译程序生成的代码要适合共享库的内容这样的代码能够根据载入内存的位置计算内部地址。

[root@localhost main5]#gcc -shared hello1.o hello2.o -o hello.so

//将hello1.o和hello2.o组合为shared object,即动态链接库

[root@localhost main5]#cp hello.so /usr/lib

//将hello.so拷贝到/usr/lib目录下

[root@localhost main5]#gcc -o hello hello.c hello.so

//将hello.c编译链接为hello的可执行程序,这个过程用到了动态链接库hello.so

在这里要废话几句,其实一切的二进制信息都有其运作的机制,只要弄清楚了它的机制,并能够实现之,则任何此时此刻无法想象之事都将成为现实。当然,这两者之间的巨大鸿沟需要顶级的设计思想和顶级的代码来跨越。

1、双击GCC安装包,mingw-get-setup.exe,点击Install安装

2、点击Change选择安装路径,尽量不要有中文和空格。

3、将package 下面的mingw-***和mingw32-***和msys-**。都勾选上,等待下载安装,直到勾选框全部变成土灰色。

4、找到安装目录的bin目录,将路径右键复制下来。

5、找到计算机,右键,属性。点击  高级系统设置。

6、找到下面的  环境变量  点击。

7、在系统变量里面找到 Path变量,双击出现下面输入框,在变量值的末尾添加刚刚复制的bin目录,并在最后加上英文状态下 的分号()。点击确定。

8、在cmd命令提示符里面输入 gcc --help 回车键,EditPlus 3 安装完成之后的界面点击File 下的 new   C/C++。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12168635.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存