3

I use autotools to manage my CPP project. When compile my code, gcc has these flags: -Wall -Werror

So, when there is an warning in my code, gcc will treat it as error, the compile will be interrupted.

My project also includes antlr3, which generate several files. The generated files contain several warnings, which interrupt compiling.

error: unused variable ‘index21_49’
in CongerCQLLexer.c, line 20589, column 24
20587>                 ANTLR3_UINT32 LA21_49;
20588> 
20589>                 ANTLR3_MARKER index21_49;
20590> 
20591> 

error: unused variable ‘index21_131’
in CongerCQLLexer.c, line 20622, column 24
20620>                 ANTLR3_UINT32 LA21_131;
20621> 
20622>                 ANTLR3_MARKER index21_131;
20623> 
20624> 

I want to know how to disable warnings for the generated files? thank you.

Chathuranga Chandrasekara
  • 20,548
  • 30
  • 97
  • 138
hellojinjie
  • 1,868
  • 3
  • 17
  • 23
  • Unfortunately it doesn't seem to be possible to set flags for individual source files, only for individual executables. See [this](http://stackoverflow.com/a/2228022/440558) old SO answer how to do that. Also see the `automake` manual [here](http://www.gnu.org/software/automake/manual/html_node/Program-and-Library-Variables.html#index-maude_005fCXXFLAGS-481). – Some programmer dude Apr 23 '12 at 06:11
  • 4
    `-Wall -Werror` is a very bad idea to include by default in your project's build system. Even if it compiles fine on gcc version X on your system, it might not on gcc version Y on your end user's system. gcc adds new warnings all the time, and it's possible that characteristics of the user's system (e.g. system header differences) lead to some warnings you did not expect. I would enable -Werror only locally, never in the default build system you ship. – R.. GitHub STOP HELPING ICE Apr 23 '12 at 09:24
  • By the way, GNU binutils has broken this principle lately, and every time I've built it, it's failed due to `-Werror` and I have to go back and re-run configure with `-Werror` removed. It's a huge pain... – R.. GitHub STOP HELPING ICE Apr 23 '12 at 16:05

2 Answers2

1

I think the best way to achieve what you want on a per-file basis is to put GCC pragmas to disable warnings in the affected files. See this question/answer on SO for details:

https://stackoverflow.com/a/3394305/379897

Community
  • 1
  • 1
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
1

The answer depends greatly on how you are getting -Wall -Werror into the build. If you are assigning CFLAGS directly in configure.ac, the solution is to stop doing that. CFLAGS is a user variable and should only be assigned by the user. If you are setting them in AM_CFLAGS, you can instead add them only to specific files via foo_CFLAGS. However, assiging -Wall -Werror to CFLAGS is a bad idea for several reasons, one of which is that not all compilers accept those flags. Do you want the build to die with '-Werror -- unknown option'? Although many (most?) compilers do accept -Wall -Werror, the point is that you do not know what compiler your user is using, and you do not know if -Werror is useful or even accepted, and you do not know if the user wants those flags set. Let the user decide.

Automake does not provide much granularity in terms of defining flags for particular translation units at configure time, but it would be fairly straightforward to add a variable that the user can assign that would be used for all non-built sources, and another for built sources. Instead of assigning CFLAGS, the user could assign BUILT_CFLAGS, and you could add those to foo_CFLAGS for appropriate values of foo. Normally the solution for this is to do nothing and let the user make the necessary adjustments (ie, the user will build with -Werror in CFLAGS, see the build fail, and then rebuild without -Werror.)

William Pursell
  • 204,365
  • 48
  • 270
  • 300