zoukankan      html  css  js  c++  java
  • gcc链接参数--whole-archive的作用

    // a.h

    extern void foo();

    // a.cpp

    #include <stdio.h>

    void foo()

    {

        printf("foo ");

    }

    // x.cpp

    #include "a.h"

    int main()

    {

            foo();

            return 0;

    }

    // Makefile

    all: x

    x: x.cpp liba.so

    g++ -g -o $@ $^

    liba.so: liba.a

    g++ -g -fPIC -shared -o $@ $^

    #g++ -g -fPIC -shared -o $@ -Wl,--whole-archive $^ -Wl,-no-whole-archive

    liba.a: a.o

    ar cru $@ $^

    a.o: a.cpp

    g++ -g -c $^

    clean:

    rm -f x a.o liba.a liba.so

    $ make

    g++ -g -c a.cpp

    ar cru liba.a a.o

    g++ -g -fPIC -shared -o liba.so liba.a

    #g++ -g -fPIC -shared -o liba.so -Wl,--whole-archive liba.a -Wl,-no-whole-archive

    g++ -g -o x x.cpp liba.so

    /tmp/cc6UYIAF.o: In function `main':

    /data/jayyi/ld/x.cpp:5: undefined reference to `foo()'

    collect2: ld returned 1 exit status

    make: *** [x] Error 1

    默认情况下,对于未使用到的符号(函数是一种符号),链接器不会将它们链接进共享库和可执行程序。

    这个时候,可以启用链接参数“--whole-archive”来告诉链接器,将后面库中所有符号都链接进来,参数“-no-whole-archive”则是重置,以避免后面库的所有符号被链接进来。

    // Makefile

    all: x

    x: x.cpp liba.so

    g++ -g -o $@ $^

    liba.so: liba.a

    g++ -g -fPIC -shared -o $@ -Wl,--whole-archive $^ -Wl,-no-whole-archive

    liba.a: a.o

    ar cru $@ $^

    a.o: a.cpp

    g++ -g -c $^

    clean:

    rm -f x a.o liba.a liba.so

  • 相关阅读:
    C#8.0新特性
    C#7.0新特性
    C#6.0新特性
    C#6.0到C#8.0的新特性
    纪念博客开通的6月29日
    什么是开发中经常说的'POCO'
    什么时候用Model,什么时候用Entity?
    C#数组的 Length 和 Count()
    C#中foreach的实现原理
    Windows Server 2012 R2远程桌面默认端口修改
  • 原文地址:https://www.cnblogs.com/lidabo/p/6206429.html
Copyright © 2011-2022 走看看