C++ 如何摆脱 GCC 中从字符串常量到“char*”的“已弃用转换”警告?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/59670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get rid of `deprecated conversion from string constant to ‘char*’` warnings in GCC?
提问by Josh Matthews
So I'm working on an exceedingly large codebase, and recently upgraded to gcc 4.3, which now triggers this warning:
所以我正在处理一个非常大的代码库,最近升级到 gcc 4.3,现在触发了这个警告:
warning: deprecated conversion from string constant to ‘char*'
警告:不推荐使用从字符串常量到 'char*' 的转换
Obviously, the correct way to fix this is to find every declaration like
显然,解决这个问题的正确方法是找到每个声明,如
char *s = "constant string";
or function call like:
或函数调用,如:
void foo(char *s);
foo("constant string");
and make them const char
pointers. However, that would mean touching 564 files, minimum, which is not a task I wish to perform at this point in time. The problem right now is that I'm running with -werror
, so I need some way to stifle these warnings. How can I do that?
并使它们成为const char
指针。但是,这意味着最少要接触 564 个文件,这不是我目前希望执行的任务。现在的问题是我正在运行-werror
,所以我需要一些方法来抑制这些警告。我怎样才能做到这一点?
采纳答案by DGentry
I believe passing -Wno-write-strings
to gcc will suppress this warning.
我相信传递-Wno-write-strings
给 gcc 会抑制这个警告。
回答by John
Any functions into which you pass string literals "I am a string literal"
should use char const *
as the type instead of char*
.
任何向其传递字符串文字的函数"I am a string literal"
都应用char const *
作类型而不是char*
.
If you're going to fix something, fix it right.
如果您要修复某些东西,请正确修复。
Explanation:
解释:
You can not use string literals to initialise strings that will be modified, because they are of type const char*
. Casting away the constness to later modify them is undefined behaviour, so you have to copy your const char*
strings char
by char
into dynamically allocated char*
strings in order to modify them.
您不能使用字符串文字来初始化将被修改的字符串,因为它们的类型是const char*
。虚掷常量性以后修改它们是不确定的行为,所以你要复制你的const char*
字符串char
通过char
到动态分配的char*
字符串,以对其进行修改。
Example:
例子:
#include <iostream>
void print(char* ch);
void print(const char* ch) {
std::cout<<ch;
}
int main() {
print("Hello");
return 0;
}
回答by Rob Walker
Check out gcc's Diagnostic Pragmasupport, and the list of -W warning options(changed: new link to warning options).
查看 gcc 的Diagnostic Pragma支持,以及-W 警告选项列表(已更改:新链接到警告选项)。
For gcc, you can use #pragma warning
directives like explained here.
对于 gcc,您可以使用这里#pragma warning
解释的指令。
回答by BlackShift
I had a similar problem, I solved it like this:
我有一个类似的问题,我是这样解决的:
#include <string.h>
extern void foo(char* m);
int main() {
// warning: deprecated conversion from string constant to ‘char*'
//foo("Hello");
// no more warning
char msg[] = "Hello";
foo(msg);
}
Is this an appropriate way of solving this? I do not have access to foo
to adapt it to accept const char*
, although that would be a better solution (because foo
does not change m
).
这是解决这个问题的合适方法吗?我无法foo
适应它以接受const char*
,尽管这将是一个更好的解决方案(因为foo
不会改变m
)。
回答by Konrad Rudolph
If it's an active code base, you might still want to upgrade the code base. Of course, performing the changes manually isn't feasible but I believe that this problem could be solved once and for all by one single sed
command. I haven't tried it, though, so take the following with a grain of salt.
如果它是一个活动的代码库,您可能仍然希望升级代码库。当然,手动执行更改是不可行的,但我相信这个问题可以通过一个sed
命令一劳永逸地解决。不过,我还没有尝试过,所以请保留以下内容。
find . -exec sed -E -i .backup -n \
-e 's/char\s*\*\s*(\w+)\s*= "/char const* = "/g' {} \;
This might not find all places (even not considering function calls) but it would alleviate the problem and make it possible to perform the few remaining changes manually.
这可能无法找到所有地方(甚至不考虑函数调用),但它会缓解问题并使手动执行少数剩余更改成为可能。
回答by vy32
I can't use the compiler switch. So I have turned this:
我不能使用编译器开关。所以我已经把这个:
char *setf = tigetstr("setf");
to this:
对此:
char *setf = tigetstr((char *)"setf");
回答by EdH
Here is how to do it inline in a file, so you don't have to modify your Makefile.
以下是如何在文件中内联执行此操作,因此您不必修改 Makefile。
// gets rid of annoying "deprecated conversion from string constant blah blah" warning
#pragma GCC diagnostic ignored "-Wwrite-strings"
You can then later...
然后你可以稍后...
#pragma GCC diagnostic pop
回答by takataka
Replace
代替
char *str = "hello";
with
和
char *str = (char*)"hello";
or if you are calling in function:
或者如果您正在调用函数:
foo("hello");
replace this with
将其替换为
foo((char*) "hello");
回答by John
Instead of:
代替:
void foo(char *s);
foo("constant string");
This works:
这有效:
void foo(const char s[]);
foo("constant string");
回答by appapurapu
In C++, use the const_cast
as like below
在 C++ 中,使用const_cast
如下所示
char* str = const_cast<char*>("Test string");