# Code::Blocks "warning: deprecated conversion from string constant to ‘char*’"

This topic is 2182 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm using Code::Blocks and I'm getting this warning message "warning: deprecated conversion from string constant to ‘char*’". How can I disable it?

I googled and it's present in GCC 4.2.3+ and can be disabled by one of these:
-Wno-deprecated
-Wno-write-string

The thing is, I can't find any such setting in Code::Block's IDE Any clues how to disable it (globally, I would really hate to set it up for each project separately)? Edited by Acharis

##### Share on other sites

That's not one of the warning's I'd consider useless. Pointing a non-const pointer at const memory is bad juju. Why not just use a const char * instead?
Please, no religion Half of the people will say one should rewrite everything to const char * the other half will say it's an insane idea and this topic will turn into a discussion about good coding practices.

I just want to know how to disable it

##### Share on other sites

[quote name='SiCrane' timestamp='1353888548' post='5004049']
That's not one of the warning's I'd consider useless. Pointing a non-const pointer at const memory is bad juju. Why not just use a const char * instead?
Please, no religion Half of the people will say one should rewrite everything to const char * the other half will say it's an insane idea and this topic will turn into a discussion about good coding practices.

I just want to know how to disable it
[/quote]
const exists for a reason, if you end up in a situation where your const char* (which points to a string constant) has to be modified, your design has failed and const helps you catch that with -fpermissive. You are just asking for trouble here.

But if you need to disable it, go into your project options, "Other options" and write your extra compiler flags there. I'm not sure there's a way to set it globally but unless you have hundreds of projects...

##### Share on other sites

[quote name='SiCrane' timestamp='1353888548' post='5004049']
That's not one of the warning's I'd consider useless. Pointing a non-const pointer at const memory is bad juju. Why not just use a const char * instead?
Please, no religion Half of the people will say one should rewrite everything to const char * the other half will say it's an insane idea and this topic will turn into a discussion about good coding practices.

I just want to know how to disable it
[/quote]

It's not really religion... It's just plain wrong. If you attempt to write to your char* that is actually a const char*, you might in fact crash your program.

Anyways... If you're sure what you are doing I will offer this little tip, although I haven't used Code::Blocks much.

There most certainly should be some kind of project option where you can specify custom command line arguments for the compiler. If you can't find any place in Code::Blocks to specify a custom command line, you could try adding it to your CXXFLAGS environment variable.

##### Share on other sites
Sigh... OK, the thing is that all functions like this:

 void message(char * s) {} message("hello world"); 
would give a warning. It was never like that in the past, over the decades. Making this "Obsolete" after, well, so many generation when it was all right just make no sense As far as I know only newest GCC compiler has this flaw, no other compiler have it set that strict. It basicly makes above 99% of examples/code that was written in the past causing that kind of warning.

##### Share on other sites

Sigh... OK, the thing is that all functions like this:

 void message(char * s) {} message("hello world"); 
would give a warning. It was never like that in the past, over the decades. Making this "Obsolete" after, well, so many generation when it was all right just make no sense As far as I know only newest GCC compiler has this flaw, no other compiler have it set that strict. It basicly makes above 99% of examples/code that was written in the past causing that kind of warning.

I don't understand what is wrong with adding const to the function signature?! Clearly the string must be read-only since you're passing a literal, so marking it with const is the right thing to do to ensure it can never be modified. Don't throw C++ type safety away just because you don't like reading warnings, you'll be glad to have that warning when your code explodes in your face.

Your code could work without error if you are rigorous enough to a) not modify the string b) not call any function that may modify the string, but it doesn't make OK to play with fire like that. Using const offers you a guarantee that the char pointer will never be written to by well-behaved functions and methods - what's wrong with that?

##### Share on other sites
I use tolua++ to generate Lua bindings and unfortunately, as it currently stands, the generated binding code is filled to the brim with these warnings. They are harmless in the context of what tolua++ generates (ie, the code doesn't do anything "bad"), and it's even possible that a newer version of tolua++ has eliminated them, but for the time being I just use compiler flag.

You can do it per-project as Bacterius suggested, or you can do it globally. Go to Settings->Global Compiler Settings->Other options and add the -Wno-write-strings there. I actually don't recommend you do it globally, though, since you shouldn't get in the habit of ignoring warnings.

##### Share on other sites

You can do it per-project as Bacterius suggested, or you can do it globally. Go to Settings->Global Compiler Settings->Other options and add the -Wno-write-strings there. I actually don't recommend you do it globally, though, since you shouldn't get in the habit of ignoring warnings.
Thanks, that solved it.

##### Share on other sites

I use tolua++ to generate Lua bindings and unfortunately, as it currently stands, the generated binding code is filled to the brim with these warnings.

Maybe I'm confused by the name, but ++ suggests that it creates bindings for C++. Everything producing "char*" instead of "const char*" (unless the function might actually modify the string) should be considered flat out broken and worthy of a bug report. Mostly because I'm extremely sick of having to add hacks and workarounds when using C++ strings and c_str. Casting away the const feels like slapping a time bomb and copying the string to a vector<char> is just irritating.

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 11
• 15
• 12
• 14
• 9
• ### Forum Statistics

• Total Topics
633354
• Total Posts
3011488
• ### Who's Online (See full list)

There are no registered users currently online

×