hellojinjie
hellojinjie

Reputation: 2138

How to disable specified warning for specified file while using autotools?

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.

Upvotes: 3

Views: 3415

Answers (2)

William Pursell
William Pursell

Reputation: 212404

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.)

Upvotes: 1

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215397

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

Upvotes: 1

Related Questions