Sign in to follow this  
Acharis

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

Recommended Posts

Acharis    5979
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 this post


Link to post
Share on other sites
Acharis    5979
[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?
[/quote]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 this post


Link to post
Share on other sites
Bacterius    13165
[quote name='Acharis' timestamp='1353889288' post='5004054']
[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?
[/quote]Please, no religion [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] 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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
[/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 this post


Link to post
Share on other sites
wack    1358
[quote name='Acharis' timestamp='1353889288' post='5004054']
[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?
[/quote]Please, no religion [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] 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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
[/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 this post


Link to post
Share on other sites
Acharis    5979
Sigh... OK, the thing is that all functions like this:

[CODE]
void message(char * s) {}

message("hello world");

[/CODE]
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 this post


Link to post
Share on other sites
Bacterius    13165
[quote name='Acharis' timestamp='1353891126' post='5004060']
Sigh... OK, the thing is that all functions like this:

[CODE]
void message(char * s) {}

message("hello world");

[/CODE]
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 [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] 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.
[/quote]
I don't understand what is wrong with adding const to the function signature?! Clearly the string [b]must[/b] be read-only since you're passing a literal, so marking it with const [i]is the right thing to do[/i] 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 this post


Link to post
Share on other sites
JTippetts    12970
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 this post


Link to post
Share on other sites
Acharis    5979
[quote name='JTippetts' timestamp='1353892260' post='5004064']
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.
[/quote]Thanks, that solved it.

Share this post


Link to post
Share on other sites
Trienco    2555
[quote name='JTippetts' timestamp='1353892260' post='5004064']
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.
[/quote]

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this