Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

How do you deal with thirdparty includes, particularly with big header-only libraries? You basically have to change compiler flags on the fly, and AFAIK it isn't well supported. My colleagues have dealt with this by defining some ugly macros which enable/disable hardcoded warnings, and there are lots of them.


I've had good success including not-my-code with the appropriate compiler flag. This tells the compiler that I'm not interested in warnings in those files since I'm not going to change them.

https://gcc.gnu.org/onlinedocs/cpp/System-Headers.html


How does this work with templates? If a type you pass into such a library causes a warning in that library, is that your fault or the library's? How could the compiler possibly tell? It seems like this can't even work in principle if there are templates involved.


The warnings I've had issues with were warnings regardless of whether the code was templates: ignored return values, unused parameters, etc.

There may be cases where the potential false negatives are unacceptable. Be judicious, but in my case the benefits far outweighed the risks.

Of course, it's a great idea to provide upstream patches and issues whenever possible.


Thanks, this looks like a better solution!


That's good to know. I usually just add a warning suppression or two.


Make a warnpush.h file where you use compiler pragmas to suppress warnings and then make a warnpop.h file to restore. Then you do

  #include "warnpush.h"
  #include <crappy.h>
  #include "warnpop.h"
I always do this for third party headers in my projects. Except for std headers.

Example:

https://github.com/ensisoft/newsflash-plus/blob/master/app/c...



Heh, an analogous file in my colleagues' project is 250 non-emply LOC. It seems to list every warning for GCC from 4.8 to 6.X. Sorry, not open source (yet?).


With GCC and Clang, you should use -isystem<thirdparty/header/dir> instead of -I<thirdparty/header/dir> for these include directories. This will suppress warnings from these headers.

CMake also supports this through include_directories(SYSTEM …).

With MSVC you will have to fall back on macros.


Can't you do it just with some #pragmas?


If the number of distinct warnings is small, I tend to fix them and submit a PR.




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: