Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix -Wmissing-prototypes warning on Clang #1863

Merged
merged 1 commit into from
Aug 16, 2019

Conversation

mosra
Copy link
Contributor

@mosra mosra commented Jul 30, 2019

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
Copy link
Member

wjakob commented Aug 15, 2019

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.
@wjakob wjakob merged commit 5b0ea77 into pybind:master Aug 16, 2019
@wjakob
Copy link
Member

wjakob commented Aug 16, 2019

Merged, thanks!

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

mosra commented Aug 16, 2019

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