3

Getting a warning message as below which I am trying to get rid off

warning: initialization discards `const' from pointer target type

The code needs to stay as it is so in VS used Suppress Specific Warning but I was wondering if there is some way to do the same in gcc as well.

Using quite an older version of GCC compiler 3.4 all the other posts I have come across talk about higher versions. Some helpful posts-

How to supress specific warnings in g++

http://www.dbp-consulting.com/tutorials/SuppressingGCCWarnings.html

https://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Warning-Options.html

Tried using-

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wignored-qualifiers"
//< code that generates warning >
#pragma GCC diagnostic pop

But then the warnings increase-

: warning: ignoring pragma: push
: warning: ignoring pragma: "-Wignored-qualifiers
: warning: initialization discards `const' from pointer target type
: warning: initialization discards `const' from pointer target type
: warning: ignoring pragma: pop
Community
  • 1
  • 1
Maverick
  • 41
  • 4
  • 5
    Is the `daignostic ` a typo? – ouah Oct 30 '15 at 16:16
  • 2
    `gcc 3.4`: sounds like fun – m.s. Oct 30 '15 at 16:17
  • 1
    If gcc 3.4 doesn't support these pragmas you can also compile the problematic source file with `-Wignored-qualifiers`. – ouah Oct 30 '15 at 16:20
  • Its giving error: Invalid option `-Wignored-qualifiers' on compilation. I am not sure if its the correct flag that I am using @ouah – Maverick Oct 30 '15 at 17:03
  • 2
    You can usually avoid that warning with an explicit cast, such as `char *s = (char*)"do not modify me";`. Although there is a reason for the warning. – rici Oct 30 '15 at 17:52
  • I hope that for all of us this is not part of a professional or even commercial project. Using compilers *that* old and painting over such non-trivial warnings shouldn't pass any project review, anywhere. – Jens Gustedt Oct 30 '15 at 18:17
  • Well sorry @Jens Gustedt it is a part of professional and even commercial project and why its still being used you might wanna read the answer to this http://aviation.stackexchange.com/a/21613/3354 – Maverick Oct 30 '15 at 18:43
  • Please post the relevant ocde. At least the variable declaration with the initializer. – user3629249 Oct 30 '15 at 18:44
  • Well the code needs to stay as it is I m more concerned to figure out a way to disable the warnings @user3629249 – Maverick Oct 30 '15 at 18:49
  • 2
    @Maverick, if you are starting to use the same program but on a new platform or even just another compiler such as gcc here, the argument that is given in the link is just not applicable. gcc might just do different things with the same undefined behavior that your are painting over. Depending on the context, this could be irresponsible. – Jens Gustedt Oct 31 '15 at 03:22
  • 1
    Are you sure your terminology is correct? At 'initialization' is where a 'const' variable gets its' value set. The warning message is more likely because 1) a const value is passed to a function and the function tries to change it. 2) your 'initialization' is actually an 'assignment' Please post the relevant couple lines of code ( the variable declaration and where the compiler says the error is occurring.) – user3629249 Oct 31 '15 at 12:51
  • @user3629249 Sir I do understand what you are trying to imply but my intention is not exactly to modify the code to resolve it but to figure out if there is any possible way to just disable and suppress warning like we do have in some other compilers. – Maverick Nov 03 '15 at 18:18
  • "The code needs to stay as it is" - why? If you are following some corporate policy about code changes, be aware that compiler switch changes are also a change and perhaps even more likely to cause disruption than fixing the code in many instances – M.M Nov 03 '15 at 19:59

1 Answers1

2

look here:

<gcc.gnu.org/onlinedocs/gcc/Option-Summary.html>; 

for the list of the gcc options and look at:

<gcc.gnu.org/onlinedocs/gcc/…; 

for more detailed descriptions.

This link:

<gcc.gnu.org/onlinedocs/gcc/Warning-Options.html>

has this to say:

"Each of these specific warning options also has a negative form beginning ' -Wno- ' to turn off warnings; 

for example, -Wno-implicit ."
user3629249
  • 16,402
  • 1
  • 16
  • 17
  • Its giving an error **cc1plus: error: Invalid option `-Wno-ignored-qualifiers'** Seems like the older version doesn't support it. Tried using -Wno-discarded-qualifiers [link](https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#Warning-Options) – Maverick Nov 03 '15 at 20:15
  • How can we get to know which flag actually turns on **warning: initialization discards `const' from pointer target type** this warning – Maverick Nov 03 '15 at 20:31
  • Using the following compiler flags `COMP_FLAGS += \ -Wall \ -Wno-cast-qual \ -Wno-comment \ -D_VMOS \ -DLYNXOS \ -fno-rtti \ -fno-exceptions \ -mminimal-toc \ -fno-builtin \ -D__STDC_LIMIT_MACROS` Would the order impact? – Maverick Nov 03 '15 at 21:41
  • What are all those `\\` in the list of COMP_FLAGS? They are not valid in the list of compiler flags – user3629249 Nov 03 '15 at 23:17
  • the order of the compiler flags does count, later options will overwrite/modify earlier options – user3629249 Nov 03 '15 at 23:20
  • where are the `-Wextra` and `-pedantic` parameters. Why are the -D parameters mixed in with the 'straight' compiler flags? – user3629249 Nov 03 '15 at 23:22
  • regarding the `cc1plus` in the error message: this is 'usually' from trying to use `gcc` to compile a `c++` file. --or-- the source file is using `utf-16` format rather than `utf-8` format. – user3629249 Nov 03 '15 at 23:44