如何静态库链接

时间:2016-06-24 08:26:14

标签: c++ linux static

在lib.a def:

的main.cpp

#include "fun_a.h"

void fun_a() {
    return;
}

void fun_nocall_a(){
    return;
}

g++ main.cpp -L./ -la

根据我的理解,a.out不应该链接fun_nocall_a,而是nm输出:

00000000006008e0 d _DYNAMIC 0000000000600aa8 d _GLOBAL_OFFSET_TABLE_ 0000000000400738 R _IO_stdin_used
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
                 w _Jv_RegisterClasses 000000000040061b T _Z12fun_nocall_av 0000000000400635 T _Z12fun_nocall_bv 00000000004005f8 T _Z15fun_nocall_mainv

它有链接fun_nocall_a;

另一项测试:

#include "fun_a.h"
#include "hiredis.h"

void fun_nocall_main() {
    return;
}

int main() {
    redisConnect(NULL,0);
    fun_a();
    return 0;
}

使用Redis lib,我发现一些函数由a.out链接,但有些不是;

0000000000402e70 T redisAppendCommand
0000000000401b80 T redisAppendCommandArgv
00000000004017e0 T redisAppendFormattedCommand
0000000000401de0 T redisBufferRead
0000000000402240 T redisBufferWrite
0000000000406fa0 T redisCheckSocketError
0000000000404050 T redisCommand
0000000000403fd0 T redisCommandArgv
0000000000402050 T redisConnect
00000000004021b0 T redisConnectBindNonBlock
0000000000401ee0 T redisConnectFd
0000000000401390 T redisConnectNonBlock
0000000000402140 T redisConnectUnix

我的问题是:

  

链接静态库时的原理是什么?

0 个答案:

没有答案