zoukankan      html  css  js  c++  java
  • gcc 相关总结 动态链接库

    #include < >与#include " "

    #include < >:直接到系统指定的目录中去找头文件。
    #include " " :先在源文件所在文件夹寻找,再到系统指定的目录中去找头文件。

    gcc 常用命令

    GCC是GNU C Compiler 的简写,GCC现在已经支持多种语言的编译,是开放源代码领域应用最广泛的编译器,具有功能强大,编译代码支持性能优化等特点。

    //test.c
    #include <stdio.h>
    int main(void)
    {
        printf("Hello World!
    ");
        return 0;
    }
    

    最基本的gcc命令如下:

    gcc test.c -o test
    

    根据test.c文件生产可执行文件test。其实从.c文件到可执行文件会经历四步:预编译(Preprocessing),编译(Compilation),汇编(Assembly),链接(Linking)。

    预编译

    gcc -E test.c -o test.i 或 gcc -E test.c
    

    得到汇编代码

    gcc -S test.i -o test.s
    

    得到目标文件

    gcc -c test.s -o test.o
    

    链接

    gcc test.o -o test
    

    多个文件的编译

    gcc test1.c test2.c -o test
    

    gcc -c test1.c -o test1.o
    gcc -c test2.c -o test2.o
    gcc test1.o test2.o -o test
    

    第一中方法编译时需要所有文件重新编译,而第二种方法可以只重新编译修改的文件,未修改的文件不用重新编译。

    优化选项 -O

    gcc -O1 test.c -o test
    

    使用编译优化级别1编译程序。级别为1~3,级别越大优化效果越好,但编译时间越长。

    检错

    输出所有警告信息:

    gcc -Wall test.c -o test    
    

    在所有产生警告的地方停止编译:

    gcc -Werror test.c -o test
    

    查看二进制文件相关命令

    查看文件类型:

    file
    

    查看二进制文件链接到哪些库

    ldd
    

    查看二进制文件里面所包含的symbol,T表示加载,U表示undefined symbol

    nm(选项)(参数)
    

    选项:

    -A:每个符号前显示文件名; 
    -D:显示动态符号; 
    -g:仅显示外部符号; 
    -r:反序显示符号表。
    
    

    参数:
    目标文件:二进制目标文件,通常是库文件和可执行文件。

    读二进制文件里面的信息

    readelf -a smu.o
    

    将二进制文件转换为汇编

    objdump -d sum.o
    

    制作动态链接库

    编写想要制作库的.h和.c文件

    /*mylib.h*/
    void lib_test();
    
    /*mylib.c*/
    #include "mylib.h"
    #include <stdio.h>
    void lib_test()
    {
            printf("This is a dynamic link library!
    ");
    }
    

    输入命令:

    gcc -shared -fPIC mylib.c -o libmylib.so
    

    生产动态链接库libmylib.so文件。
    接下来要就要使用动态链接库了。
    编写:

    #include "mylib.h"
    int main()
    {
    	lib_test();
    }
    

    输入命令:

    gcc main.c -o main -L. -lmylib -Wl,-rpath,***         #***是动态链接库的路径
    
    

    执行

    ./main
    

    输出为:

    This is a dynamic link library!   
    

    至此成功使用动态链接库。

    参考:
    Linux GCC常用命令
    gcc指定头文件路径及动态链接库路径
    Linux中动态链接库总结
    linux 下动态链接实现原理

  • 相关阅读:
    使用 Spring data redis 结合 Spring cache 缓存数据配置
    Spring Web Flow 笔记
    Linux 定时实行一次任务命令
    css js 优化工具
    arch Failed to load module "intel"
    go 冒泡排序
    go (break goto continue)
    VirtualBox,Kernel driver not installed (rc=-1908)
    go运算符
    go iota
  • 原文地址:https://www.cnblogs.com/sandy-t/p/7261617.html
Copyright © 2011-2022 走看看