如何防止静态库中的所有符号加载以及为什么在链接静态库时导出相同.o文件中的其他符号进行测试

[英]How to prevent all symbols from static library to load and why other symbols from same .o file get exported to test while linking static library


Suppose there are three c files, say a.c contains functions xx(), yy() and b.c contains nn(), mm() and c.c contains qq(), rr().

假设有三个c文件,例如a.c包含函数xx(),yy()和b.c包含nn(),mm()和c.c包含qq(),rr()。

I made a static library stat.a out of a.o, b.o and c.o. If I link stat.a into a test which calls xx(), then symbol yy() also gets exported: nm test has both symbols xx and yy.

我从a.o,b.o和c.o中创建了一个静态库stat.a.如果我将stat.a链接到一个调用xx()的测试,那么符号yy()也会被导出:nm test同时包含符号xx和yy。

  1. I would like to know why the symbols qq and rr do not get exported ?
  2. 我想知道为什么符号qq和rr不会被导出?
  3. Is there any method to prevent any other symbols than xx being loaded?
  4. 是否有任何方法可以防止除xx被加载之外的任何其他符号?

1 个解决方案

#1


0  

  1. I would like to know why the symbols qq and rr do not get exported ?
  2. 我想知道为什么符号qq和rr不会被导出?

You have to inform the linker of your intention How to force gcc to link an unused static library

您必须通知链接器您的意图如何强制gcc链接未使用的静态库

gcc -L./ -o test test.c -Wl,--whole-archive stat.a -Wl,--no-whole-archive

gcc -L./ -o test test.c -Wl, - whole-archive stat.a -Wl, - no-whole-archive

  1. Is there any method to prevent any other symbols than xx being loaded?
  2. 是否有任何方法可以防止除xx被加载之外的任何其他符号?

From How do I include only used symbols when statically linking with gcc?

从静态链接到gcc时,如何仅包含已使用的符号?

gcc -ffunction-sections -c a.c

gcc -ffunction-sections -c a.c

gcc -L./ -o test test.c -Wl,--gc-sections stat.a

gcc -L./ -o test test.c -Wl, - gc-sections stat.a


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2018/09/05/ac691d7fdcc3f2e7d9d280e66c04485.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号