C++ G++;让我调用未定义的方法
我在Ubuntu下有一个很大的Qt项目。刚刚发现G++允许我在调用已声明但未定义的方法的地方编译和链接代码。它在该调用的运行时崩溃 虽然我强制使用了相同的g++命令行,但我无法在测试项目中重现这种行为 问题是: 为什么它让我这么做? 如何使链接器生成错误 编辑(基于评论):C++ G++;让我调用未定义的方法,c++,gcc,g++,ld,C++,Gcc,G++,Ld,我在Ubuntu下有一个很大的Qt项目。刚刚发现G++允许我在调用已声明但未定义的方法的地方编译和链接代码。它在该调用的运行时崩溃 虽然我强制使用了相同的g++命令行,但我无法在测试项目中重现这种行为 问题是: 为什么它让我这么做? 如何使链接器生成错误 编辑(基于评论): 我知道它没有经过优化,因为当我调用该方法时,它会在运行时崩溃 我用一个伪名称声明并调用了另一个相同的方法-我认为类似GFDSGFDHGASFDHGFFA()的方法也可以:)-同样的事情 调用未定义的方法时,应用程序崩溃。很抱
默认情况下,GCC(不仅仅是G++)假定所有未定义的符号都是外部符号。VisualStudio没有 有关问题:
调用该函数时(在运行时)会发生什么?我的猜测是,在对象文件中的某个地方有一个同名函数。你在使用名称空间吗?我添加了答案作为编辑。未定义的方法是否可能是一个
slot
?
--allow-shlib-undefined
--no-allow-shlib-undefined
Allows (the default) or disallows undefined symbols in shared
libraries (It is meant, in shared libraries _linked_against_, not the
one we're creating!--Pavel Shved). This switch is similar to --no-un-
defined except that it determines the behaviour when the undefined
symbols are in a shared library rather than a regular object file. It
does not affect how undefined symbols in regular object files are
handled.
The reason that --allow-shlib-undefined is the default is that the
shared library being specified at link time may not be the same as
the one that is available at load time, so the symbols might actually
be resolvable at load time. Plus there are some systems, (eg BeOS)
where undefined symbols in shared libraries is normal. (The kernel
patches them at load time to select which function is most appropri-
ate for the current architecture. This is used for example to dynam-
ically select an appropriate memset function). Apparently it is also
normal for HPPA shared libraries to have undefined symbols.