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!
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With