简体   繁体   中英

Why do I get c++ compile warnings depending on header order

I'm using compile flags -Wall -Wextra and -Werror. I'm getting a flood of "declared 'static' but never defined [-Werror=unused-function] " Warnings (treated as errors) when I compile the following file. No such warnings when I reverse the order of the #include directives. Please help me understand why?

I know I could remove the extra warnings and errors and get my program to compile, clearly that's not my intent or my code would be more interesting. I'm trying to gain a deeper knowledge of C++, and improve my habits through cleaning up warnings in my code.

I understand that argp is really a C library, and iostream is a C++ library, perhaps that's part of the issue. I'd be happy to use a proper C++ library to accomplish what argp does, but I can't find one. If there is one I'd be happy to hear about it.

#include <argp.h>
#include <iostream>

int main(int argc, char **argv)
{
  return 0;
}

To be clear, I am working on a non-trivial program, and have specific reasons for wanting to use C++ instead of C. I've boiled down the code shown here to the least possible code to produce the effect I'm trying to understand. Please don't suggest that I don't need one or the other of the headers.

Compiler: gcc

    :~/scratch/argp_example$ gcc --version
gcc (Ubuntu 5.2.1-23ubuntu1~12.04) 5.2.1 20151031 Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

compiler invokation: g++ -o obj/main.o -c src/main.cpp -Wall -Wextra -Werror -pedantic -MMD --std=c++11 -Iinc

Specific compiler feedback:

    In file included from /usr/include/x86_64-linux-gnu/c++/5/bits/gthr.h:148:0,
                 from /usr/include/c++/5/ext/atomicity.h:35,
                 from /usr/include/c++/5/bits/ios_base.h:39,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from src/main.cpp:2:
/usr/include/x86_64-linux-gnu/c++/5/bits/gthr-default.h:101:1: warning: ‘int __gthrw_pthread_once(pthread_once_t*, void (*)())’ declared ‘static’ but never defined [-Wunused-function] __gthrw(pthread_once) ^

There are many many more similar errors from gthr.h. This specific copy/paste was from a run without -Werror, but that's the only difference.

SOLUTION: This was my choice of solution, but of course, you could simply reverse the order of includes. It is a recognized bug, so there is no "correct" answer, all solutions would be workarounds. This one, I think, is least likely to give me or others fits later.

#include <argp.h>
#undef __attributes__
#include <iostream>
...

This is a known bug . The culprit is this chunk of code in argp.h , which is triggered when you use -std=c++xx :

#ifndef __attribute__
/* This feature is available in gcc versions 2.5 and later.  */
# if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__
#  define __attribute__(Spec) /* empty */
# endif

The declarations at issue are normally marked with __attribute__ ((__weakref__("pthread_meow"))) , but this macro caused that attribute to vaporize.

Until the bug gets fixed, you might want to compile with -std=gnu++xx , or manually #undef __attribute__ after including argp.h .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM