Skip to content

Fix -Wmissing-prototypes warning on Clang#1863

Merged
wjakob merged 1 commit into
pybind:masterfrom
mosra:fix-missing-prototypes
Aug 16, 2019
Merged

Fix -Wmissing-prototypes warning on Clang#1863
wjakob merged 1 commit into
pybind:masterfrom
mosra:fix-missing-prototypes

Conversation

@mosra

@mosra mosra commented Jul 30, 2019

Copy link
Copy Markdown
Contributor

Similarly to #1852 this is just a minor change with minimal impact :)

The -Wmissing-prototypes warning on Clang (or -Wmissing-declarations on GCC) is very useful to avoid accidents where a function definition in a source file doesn't match the corresponding declaration in a header file, as it would warn already during compilation and not fail much later during link time.

Unfortunately this means that exported functions defined only in the source file (usually the ones annotated with extern "C") will cause this warning to be emitted too (on Clang, GCC has a slightly different behavior with -Wmissing-declarations and doesn't warn here). For example:

magnum.cpp:211:1: warning: no previous prototype for function 'PyInit__magnum' [-Wmissing-prototypes]
PYBIND11_MODULE(_magnum, m) {
^
/usr/include/pybind11/detail/common.h:285:5: note: expanded from macro 'PYBIND11_MODULE'
    PYBIND11_PLUGIN_IMPL(name) {                                               \
    ^
/usr/include/pybind11/detail/common.h:172:42: note: expanded from macro 'PYBIND11_PLUGIN_IMPL'
    extern "C" PYBIND11_EXPORT PyObject *PyInit_##name()
                                         ^

This fixes the warning by providing a matching declaration right before the definition, both for the Python 3 and Python 2 variant.

@wjakob

wjakob commented Aug 15, 2019

Copy link
Copy Markdown
Member

MSVC doesn't like it: error C2375: 'initpybind11_tests': redefinition; different linkage . I think you will also need to copy-paste the PYBIND11_EXPORT.

The -Wmissing-prototypes Clang warning (or -Wmissing-declarations on
GCC) is very useful to avoid accidents where a function definition in a
source file doesn't match the corresponding declaration in a header
file, as it would warn already during compilation and not much later
during link time.

Unfortunately this means that exported functions defined only in the
source file (usually the ones annotated with `extern "C"`) will cause
this warning to be emitted too (on Clang, GCC has a slightly different
behavior with -Wmissing-declarations and doesn't warn here). This fixes
the warning by providing a declaration right before the definition.
@mosra mosra force-pushed the fix-missing-prototypes branch from eb0ac8d to fd5eda3 Compare August 15, 2019 22:13
@wjakob wjakob merged commit 5b0ea77 into pybind:master Aug 16, 2019
@wjakob

wjakob commented Aug 16, 2019

Copy link
Copy Markdown
Member

Merged, thanks!

@mosra mosra deleted the fix-missing-prototypes branch August 16, 2019 10:00
@mosra

mosra commented Aug 16, 2019

Copy link
Copy Markdown
Contributor Author

Thank you as well, and apologies for forgetting to ensure AppVeyor passes as well :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants