gpt4 book ai didi

c++ - 允许特定的 undefined reference 与 GCC

转载 作者:行者123 更新时间:2023-12-01 14:47:52 24 4
gpt4 key购买 nike

在带有 GCC 的 C++ 程序中,我使用链接器标志 -Wl,--no-undefined因此,如果存在 undefined reference ,链接器将抛出错误。是否有 pragma 或我可以在特定符号旁边的代码中放置一些我希望允许在链接时未定义的内容?

最佳答案

一种选择是使 symbol weak :

A weak symbol denotes a specially annotated symbol during linking of Executable and Linkable Format (ELF) object files. By default, without any annotation, a symbol in an object file is strong. During linking, a strong symbol can override a weak symbol of the same name. In contrast, two strong symbols that share a name yield a link error during link-time. When linking a binary executable, a weakly declared symbol does not need a definition. In comparison, (by default) a declared strong symbol without a definition triggers an undefined symbol link error.



例如。:
#pragma weak power2
int power2(int x);

关于c++ - 允许特定的 undefined reference 与 GCC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61659627/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com