简体   繁体   中英

Set “treat warnings as errors” for all files except one in qmake

I want to specify the "treat warnings as errors" flag for all files except one. For example if I have a.cpp, b.cpp and c.cpp, this would do it for all 3:

SOURCES += a.cpp \
    b.cpp \
    c.cpp
QMAKE_CXXFLAGS += /WX

But how do I do it so that only a.cpp and b.cpp have that flag but c.cpp doesn't? For example I tried this (but it doesn't work):

SOURCES += a.cpp \
    b.cpp \
    c.cpp
QMAKE_CXXFLAGS += /WX
c.cpp: QMAKE_CXXFLAGS -= /WX

Edit: I see there is How to specify separate compilation options for different targets in qmake? and http://permalink.gmane.org/gmane.comp.lib.qt.general/10945 . However I need to unspecify a flag for a certain file (the opposite), so I'm not sure if this works for me. Also the provided solution is for GCC (and I'm not MSVC).

Edit 2: Would it be possible to unset /WX with a #pragma from inside the source file itself?

From the top of my head I think of two ways to deal with your situation:

  1. Split your list of source files into several (disjoint) lists. For each sub-list of source files compile a temporary static lib with all compiler flags that you want. Then link your final application/library to all of the temporary static libraries. Rather cumbersome!

  2. Use #pragma statements to instruct your compiler to ignore certain warnings. These depend on your compiler. For the GCC see

    1. https://stackoverflow.com/a/3394268
    2. https://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html

    and for the Microsoft compiler this could be a good start:

    1. https://msdn.microsoft.com/en-us/library/2c8f766e.aspx

There doesn't seem to be a good solution. Here is what I did in the end:

I put QMAKE_CXXFLAGS += /WX in the .pro file, and then I put #pragma warning(push, 0) at the beginning of each .cpp file I wanted to exclude.

This doesn't disable the "treat warnings as errors" flag, it simply disables warnings, but since usually we want to exclude third-party code anyway, it's a good enough solution.

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