是否有可能将"-Wwrite-strings"变成错误?



我已经在GCC 4.8.3、4.9.2和trunk 20141210上测试过了。当使用-Werror=write-strings时,它正确地启用了警告,但不会将其转化为错误。我使用的命令行是:

g++ -std=c99 -x c -Werror=write-strings -O2 -Wall -pedantic main.cpp
warning: initialization discards 'const' qualifier from pointer target type
     char *s = "test";

然而,它在C++模式下变成了一个错误:

g++ -Werror=write-strings -O2 -Wall -pedantic -pthread main.cpp
main.cpp:8:15: error: deprecated conversion from string constant to 'char*' 
  [-Werror=write-strings]
     char *s = "test";

是我遗漏了什么,还是这只是一个遗漏的功能?

我看到了被删除的答案,但请耐心等待……它实际上是正确的(至少在我的测试中)。我认为你的发现很有趣,而且几乎是一个bug。

就我的测试而言(GCC 4.9.1构建在Mac OS X 10.9.4 Mavericks上,运行在Mac OS X 10.10.2 Yosemite上),-Werror=write-strings似乎没有将警告变成错误(我认为这可能是一个错误,尽管你可能会发现GCC团队对此有不同的看法)。只有当-Werror生效时,它才会变成一个错误。

以下是我运行的内容:

$ cat x.c
#include <stdio.h>
int main(void)
{
    char *test = "data";
    printf("%sn", test);
    return 0;
}
$ gcc -O3 -g -std=c11 -c x.c
$ gcc -O3 -g -std=c11 -c x.c -Wwrite-strings
x.c: In function ‘main’:
x.c:5:18: warning: initialization discards ‘const’ qualifier from pointer target type
     char *test = "data";
                  ^
$ gcc -O3 -g -std=c11 -c x.c -Werror=write-strings
x.c: In function ‘main’:
x.c:5:18: warning: initialization discards ‘const’ qualifier from pointer target type
     char *test = "data";
                  ^
$ gcc -O3 -g -std=c11 -c x.c -Werror -Wwrite-strings
x.c: In function ‘main’:
x.c:5:18: error: initialization discards ‘const’ qualifier from pointer target type [-Werror]
     char *test = "data";
                  ^
cc1: all warnings being treated as errors
$ gcc -O3 -g -std=c11 -c x.c -Wall -Wextra -Werror=write-strings
x.c: In function ‘main’:
x.c:5:18: warning: initialization discards ‘const’ qualifier from pointer target type
     char *test = "data";
                  ^
$ gcc -O3 -g -std=c11 -c x.c -Wall -Wextra -Werror
$ gcc -O3 -g -std=c11 -c x.c -Wall -Wextra -Werror -Wwrite-strings
x.c: In function ‘main’:
x.c:5:18: error: initialization discards ‘const’ qualifier from pointer target type [-Werror]
     char *test = "data";
                  ^
cc1: all warnings being treated as errors
$ gcc -O3 -g -std=c11 -c x.c -Wall -Wextra -Werror -Werror=write-strings
x.c: In function ‘main’:
x.c:5:18: error: initialization discards ‘const’ qualifier from pointer target type [-Werror]
     char *test = "data";
                  ^
cc1: all warnings being treated as errors
$

为了方便起见,也为了便于查看,以下是我运行的GCC命令:

gcc -O3 -g -std=c11 -c x.c
gcc -O3 -g -std=c11 -c x.c -Wwrite-strings
gcc -O3 -g -std=c11 -c x.c -Werror=write-strings
gcc -O3 -g -std=c11 -c x.c -Werror -Wwrite-strings
gcc -O3 -g -std=c11 -c x.c -Wall -Wextra -Werror=write-strings
gcc -O3 -g -std=c11 -c x.c -Wall -Wextra -Werror
gcc -O3 -g -std=c11 -c x.c -Wall -Wextra -Werror -Wwrite-strings
gcc -O3 -g -std=c11 -c x.c -Wall -Wextra -Werror -Werror=write-strings

如果你不能正常使用-Werror运行,那么你可能不得不使用-Werror -Wwrite-strings进行试构建,因为其他问题会失败,但这也会发现可写字符串的问题,你可以修复并检查这些问题。然后,当你对-Wwrite-strings错误没有问题时,你可以回到编译中不包括-Werror(在命令行中保留-Wwrite-strings甚至-Werror=write-strings,这样,如果你犯了错误和/或修复了假定的编译器错误,那么你就可以让它按照你真正想要的方式工作)。

相关内容

最新更新