How to detect declared but undefined functions in C++?

梦想的初衷 提交于 2021-02-04 22:10:49

问题


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!";
}

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.

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.

Thanks!


回答1:


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).

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.

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.

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).




回答2:


Perhaps try calling all functions in your implementation map and adding a try catch that spits out some warning if they segfault.




回答3:


You don't. Or rather, this is not the job of the compiler

I guess I'm just repeating what other said in the comments, but:

  • It's actually a feature (see unimplemented private constructor)
  • It wouldn't really really help, as the "bug" here is that no proper code cleanup was done prior to committing the code, and an unimplemented and unused function is really you smallest problem then. What about all the other stuff that hasn't been cleaned up? The likelihood of having an implemented and unused function seems just as high to me, and it's the same mess more or less.

Rather than worry about this specific case, I would check if this was just a one time glitch, or if your development team could improve some procedures that would prevent such things in the future.




回答4:


As far as languages such as C++ are concerned, detecting and reporting undefined functions will defeat one of the important feature provided by it. Virtual/pure-virtual functions are one of the important mechanism by which C++ implements run-time polymorphism.

If you are developing a library to be used by its clients, you might have declared-but-not-defined virtual functions. You may leave the definition for its clients. In such cases, if the compiler were to report such undefined functions, it won't be beneficial.



来源:https://stackoverflow.com/questions/23201873/how-to-detect-declared-but-undefined-functions-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!