静态库的编译、使用和调试

静态库的生成

  1. 代码
  • add.h
#ifndef _ADD_H_
#define _ADD_H_

int add(int a,int b);

#endif
  • add.cpp
#include<iostream>
#include"add.h"

int add(int a,int b){
    return a+b;
}
  1. 生成汇编文件
    • g++ -c [file_name]
    • g++ -c add.c
    • 生成 add.o ([file_name].o) 文件
  2. 生成静态库
    • ar -crv lib[static_name].a [file_name].o
    • ar -crv libadd.a add.o

静态库的使用

  1. 测试代码
#include<iostream>
#include"add.h"

int main()
{
    int number1 = 10;
    int number2 = 20;
    std::cout << "result: " << add(number1,number2) << std::endl;
    return 0;
}
  1. 编译命令
  • g++ -o [program_name] [file_name].cpp -L [path_static_lib] -l[static_name]
  • g++ -o test test.cpp -L . -l -ladd

静态库的编译

  1. 静态库的链接方式是静态链接,也就是说,在链接阶段,源代码生成的各种目标模块,以及静态库一起装入到一个模块中,最终生成一个可执行程序。换句话说,静态库的代码是包含在可执行程序中,这是和动态库的最重要的区别。
  2. 如果想要 gdb 调试含有静态库的程序,就需要在生成静态库的时候,加上调试信息的参数,所以可进入静态库调试的可执行程序生成的命令如下:
# 汇编命令加上参数 -g
1. g++ -c add.c -g
# 打包静态库命令加上参数 s
2. ar -crvs libadd.a add.o
# 链接命令加上参数 -g
3. g++ -o test test.cpp -L . -ladd -g

调试过程

wangqinghe:~/Desktop/code/ProgramDebug/staticlib/test$ gdb test
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.5) 7.11.1
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
---Type <return> to continue, or q <return> to quit---
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from test...done.
(gdb) l
1       #include<iostream>
2       #include"add.h"
3
4       int main()
5       {
6           int number1 = 10;
7           int number2 = 20;
8           number1 = add(number1,number2);
9           std::cout << "result: " << number1 << std::endl;
10          return 0;
(gdb) b 8
Breakpoint 1 at 0x4008ac: file test.cpp, line 8.
(gdb) r
Starting program: /home/user/Desktop/code/ProgramDebug/staticlib/test/test 

Breakpoint 1, main () at test.cpp:8
8           number1 = add(number1,number2);
(gdb) s
add (a=10, b=20) at add.c:5
5           return a+b;
(gdb) 
6       }(gdb) 
main () at test.cpp:9
9           std::cout << "result: " << number1 << std::endl;
(gdb) 
result: 30
10          return 0;
(gdb) 
11      }(gdb) 
posted @   王清河  阅读(857)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2019-11-30 11-散列4 Hashing - Hard Version (30 分)
2019-11-30 11-散列3 QQ帐户的申请与登陆 (25 分)
2019-11-30 11-散列2 Hashing (25 分)
2018-11-30 KMP 串的模式匹配 (25 分)
2018-11-30 11-散列4 Hashing - Hard Version (30 分)
点击右上角即可分享
微信分享提示