1

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?

sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • Why not fix the offending source file so it doesn't generate any warnings? – Ferruccio Mar 10 '15 at 11:19
  • @Ferruccio Third-party library. – sashoalm Mar 10 '15 at 11:31
  • I see. Your second edit gave me the impression that it was one of your source files. In the long run, you may be better off building any third-party libraries separately and then linking them to your project. – Ferruccio Mar 10 '15 at 11:37
  • IMO best approach is to embed those sources in separate static library. This way you will have additional project where different compiler setting could be used. – Marek R Mar 10 '15 at 13:51

2 Answers2

1

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
Community
  • 1
  • 1
nils
  • 2,424
  • 1
  • 17
  • 31
0

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.

sashoalm
  • 75,001
  • 122
  • 434
  • 781