如何在C ++中检测已声明但未定义的函数? [英] How to detect declared but undefined functions in C++?

查看:63
本文介绍了如何在C ++中检测已声明但未定义的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码可以很好地进行编译,链接和运行(在Xcode 5.1/clang上):

The following compiles, links and runs just fine (on Xcode 5.1 / clang):

#include <iostream>

class C { int foo(); };

int main(int argc, const char * argv[])
{
    C c;
    cout << "Hello world!";
}

但是, C :: foo()并未在任何地方定义,仅在声明时定义.我没有收到任何编译器或链接器警告/错误,这显然是因为 C :: foo()从未在任何地方引用.

However, C::foo() is not defined anywhere, only declared. I don't get any compiler or linker warnings / errors, apparently because C::foo() is never referenced anywhere.

有什么办法可以发出警告,即使在整个程序中,即使声明了C :: foo(),也不存在C :: foo()的定义?错误实际上会更好.

Is there any way I can emit a warning that in the whole program no definition for C::foo() exists even though it is declared? An error would actually be better.

谢谢!

推荐答案

有很多理由使它不容易实现.一组头文件可以声明许多功能,其中一些功能由 additional 库提供.您可能需要 #include 这样的标头,而不使用所有这些功能(例如,如果您只想使用某些 #define -d常量).

There are good reasons why it is not easily feasible. A set of header files could declare many functions, some of which are provided by additional libraries. You may want to #include such headers without using all of these functions (for instance, if you only want to use some #define-d constant).

或者,有一些标头并仅在标头文件定义的API中实现(在您的库中)是合法的.

Alternatively, it is legitimate to have some header and to implement (in your library) only a subset of the API defined by the header files.

对于通常没有插件运行的程序,C ++或C头文件也可以定义潜在插件定义的代码接口.许多接受插件的程序都在其头文件中声明了插件接口.

And a C++ or C header file could also define the interface of code defined by potential plugins, for programs which usually run without plugins. Many programs accepting plugins are declaring the plugin interface in their header file.

如果您真的想进行此检查,则可以考虑使用 MELT 定制GCC;但是,这种检查目前尚不容易实现(并且您还需要优化链接时间).

If you really wanted to have such a check, you might perhaps consider customizing GCC with MELT; however, such a check is non trivial to implement currently (and you'll need link time optimization too).

这篇关于如何在C ++中检测已声明但未定义的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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