Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

xcode

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!

like image 398
oxfordatnight Avatar asked Apr 21 '14 16:04

oxfordatnight


1 Answers

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

like image 139
Basile Starynkevitch Avatar answered Oct 10 '22 22:10

Basile Starynkevitch