GCC 可以不抱怨未定义的引用吗? [英] Can GCC not complain about undefined references?

查看:24
本文介绍了GCC 可以不抱怨未定义的引用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在什么情况下 GCC 可能在尝试调用虚构函数时抛出未定义引用"链接错误消息?

Under what situation is it possible for GCC to not throw an "undefined reference" link error message when trying to call made-up functions?

比如这段C代码被GCC编译链接的情况:

For example, a situation in which this C code is compiled and linked by GCC:

void function()
{
    made_up_function_name();
    return;
}

...即使 made_up_function_name 不在代码中任何地方(不是头文件、源文件、声明,也不是任何第三方库).

...even though made_up_function_name is not present anywhere in the code (not headers, source files, declarations, nor any third party library).

那种代码在不接触实际代码的情况下,在一定条件下可以被GCC接受和编译吗?如果是,是哪个?

Can that kind of code be accepted and compiled by GCC under certain conditions, without touching the actual code? If so, which?

谢谢.

在其他任何地方都没有以前对 made_up_function_name 的声明或提及.这意味着整个文件系统的 grep -R显示确切的单行代码.

no previous declarations or mentions to made_up_function_name are present anywhere else. Meaning that a grep -R of the whole filesystem will only show that exact single line of code.

推荐答案

是的,可以避免报告未定义的引用 - 使用 --unresolved-symbols 链接器选项.

Yes, it is possible to avoid reporting undefined references - using --unresolved-symbols linker option.

g++ mm.cpp -Wl,--unresolved-symbols=ignore-in-object-files

来自man ld

--unresolved-symbols=method

确定如何处理未解析的符号.有四个方法的可能值:

Determine how to handle unresolved symbols. There are four possible values for method:

       ignore-all
           Do not report any unresolved symbols.

       report-all
           Report all unresolved symbols.  This is the default.

       ignore-in-object-files
           Report unresolved symbols that are contained in shared
           libraries, but ignore them if they come from regular object
           files.

       ignore-in-shared-libs
           Report unresolved symbols that come from regular object
           files, but ignore them if they come from shared libraries.  This
           can be useful when creating a dynamic binary and it is known
           that all the shared libraries that it should be referencing
           are included on the linker's command line.

共享库本身的行为也可以由 --[no-]allow-shlib-undefined 选项控制.

The behaviour for shared libraries on their own can also be controlled by the --[no-]allow-shlib-undefined option.

通常链接器会为每个报告未解析的符号,但选项 --warn-unresolved-symbols 可以将此更改为警告.

Normally the linker will generate an error message for each reported unresolved symbol but the option --warn-unresolved-symbols can change this to a warning.

这篇关于GCC 可以不抱怨未定义的引用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆