zoukankan      html  css  js  c++  java
  • Linux静态库与动态库制作过程

    文件:tobigchar.c   mian.c   tobigchar.h

    //tobigchar.c
    char tos()
    {
    	char ch;
    	ch = getchar();
    	if(ch >= 'a' && ch <= 'z')
    		ch = ch - 32;
    	else if(ch >= 'A' && ch <= 'Z')
    		ch = ch + 32;
    	return ch;
    }
    //main.c
    #include<stdio.h> #include"tobigchar.h" int main() { printf("%c ",tos()); }
    //tobigchar.h
    #ifndef _H_
    #define _H_
    int tos();
    #endif
    

      

    静态库:

    生成目标文件:

    gcc -c -o tobigchar.o tobigchar.c

    生成静态库:

    ar rcs libtobigchar.a tobigchar.o

    编译程序:

    gcc -I./ -L./ main.c -ltobigchar

    运行程序:

    ./a.out

    动态库:

    生成目标文件:

    gcc -c -fpic -o tobigchar.o tobigchar.c

    生产动态库:

    gcc -shared -o libtobigchar.so tobigchar.o

    编译程序:

    gcc -I./ -L./ main.c -ltobigchar

    设置动态库环境变量:

    export LD_LIBRARY_PATH=/mnt/hgfs/Linux/Linuxshare/20190119/code:$LD_LIBRARY_PATH       //pwd打印出当前文件的绝对路径

    运行程序:

    ./a.out

    为什么要做库?

      在做项目的时候,一般都不是一个人可以完成的,需要团队合作,每个人的分工不一样,做底层函数的成员提供一个库,为上层应用提供函数接口就行。

    makefile脚本:静态库

    CFLAGS=-Wall -O3 
    OBJS= main.o libtobigchar.a
    main:$(OBJS)
    	@$(CC) $(CFLAGS) -I./ -L./ $< -o $@ -ltobigchar
    libtobigchar.a:tobigchar.o
    	@$(AR) rcs $@ $<
    %.o : %.c
    	@$(CC) -c $< -o $@
    clean: 	
    	@$(RM) a.out *.o *.a *.so
    

    makefile脚本:动态库

    OBJS=main.o libtobigchar.so
    main:$(OBJS)
    	$(CC) -I./ -L./ $< -o $@ -ltobigchar
    libtobigchar.so:tobigchar.o
    	$(CC) -shared -o $@ $<
    %.o:%.c
    	$(CC) -c -fpic $< -o $@
    clean:
    	$(RM) *.o *.so *.a a.out
    	
  • 相关阅读:
    在Visual Studio 2013中修改远程Git服务器的地址
    自定义TFS工作项“所有链接”列表中的列
    在权限受限制的AD域环境中部署SQL Server AlwaysOn高可用性
    spring boot常用注解
    在线编辑器(WangEditor)
    报表生成(POI,jquery.table2excel.js,Echarts)
    java 相关书籍介绍
    poj2456 Aggressive cows
    poj1064 Cable master
    洛谷P1396 营救
  • 原文地址:https://www.cnblogs.com/huang-y-x/p/10299276.html
Copyright © 2011-2022 走看看