为什么 -pedantic 开关不给出 g++ 中 gnu 扩展代码的错误?
Why isn't -pedantic switch giving error for gnu-extension code in g++?
这是我的图书馆代码。我在这里使用 asprintf
,它是 sprintf
.
的 gnu 扩展
所以我希望编译器在启用 -pedantic
开关时抛出错误。
但是我在创建目标文件时没有得到 error/warning/message。
#include <iostream>
#include <stdio.h>
using namespace std;
int testPrint()
{
char *arr;
asprintf(&arr,"%d",10);
cout<<"testPrint called"<<endl;
return 1;
}
命令:
g++ -pedantic -Werror test.cpp -c
我应该对非标准 C++ 用法的打印错误进行哪些更改。
-pedantic
用法的相关 SO 问题:
GCC 文档的 description of the -pedantic
option 声明:
-Wpedantic
-pedantic
Issue all the warnings demanded by strict ISO C and ISO C++; reject all programs that use forbidden extensions, and some other programs that do not follow ISO C and ISO C++. For ISO C, follows the version of the ISO C standard specified by any -std
option used.
[…]
Some users try to use -Wpedantic
to check programs for strict ISO C conformance. They soon find that it does not do quite what they want: it finds some non-ISO practices, but not all—only those for which ISO C requires a diagnostic, and some others for which diagnostics have been added.
A feature to report any failure to conform to ISO C might be useful in some instances, but would require considerable additional work and would be quite different from -Wpedantic
. We don’t have plans to support such a feature in the near future.
-pedantic
不是严格的 ISO 标准一致性检查。它所做的只是启用标准要求的警告。使用 -std=c++XX
也会禁用语言扩展。
asprintf
是 C 库(很可能是 glibc)的特性,而不是编译器的特性。编译器开关通常不会将库公开的功能转换为语言标准指定的功能。 (毕竟,按照这个逻辑得出的结论意味着编译器应该阻止你使用标准库以外的任何库,因为它们没有被语言标准指定。)
如果要禁用C库的非标准特性,glibc提供feature test macros for that purpose. asprintf
is exposed, among others, when the _GNU_SOURCE
macro is defined. Ensuring the macro is undefined before including standard library headers would work for that purpose… if it weren’t for the fact that libstdc++ requires _GNU_SOURCE
to be enabled.
考虑到这一点,我不认为你的问题有解决方案,只要你坚持使用 G++ 和 libstdc++。
这是我的图书馆代码。我在这里使用 asprintf
,它是 sprintf
.
所以我希望编译器在启用 -pedantic
开关时抛出错误。
但是我在创建目标文件时没有得到 error/warning/message。
#include <iostream>
#include <stdio.h>
using namespace std;
int testPrint()
{
char *arr;
asprintf(&arr,"%d",10);
cout<<"testPrint called"<<endl;
return 1;
}
命令:
g++ -pedantic -Werror test.cpp -c
我应该对非标准 C++ 用法的打印错误进行哪些更改。
-pedantic
用法的相关 SO 问题:
GCC 文档的 description of the -pedantic
option 声明:
-Wpedantic
-pedantic
Issue all the warnings demanded by strict ISO C and ISO C++; reject all programs that use forbidden extensions, and some other programs that do not follow ISO C and ISO C++. For ISO C, follows the version of the ISO C standard specified by any
-std
option used.[…]
Some users try to use
-Wpedantic
to check programs for strict ISO C conformance. They soon find that it does not do quite what they want: it finds some non-ISO practices, but not all—only those for which ISO C requires a diagnostic, and some others for which diagnostics have been added.A feature to report any failure to conform to ISO C might be useful in some instances, but would require considerable additional work and would be quite different from
-Wpedantic
. We don’t have plans to support such a feature in the near future.
-pedantic
不是严格的 ISO 标准一致性检查。它所做的只是启用标准要求的警告。使用 -std=c++XX
也会禁用语言扩展。
asprintf
是 C 库(很可能是 glibc)的特性,而不是编译器的特性。编译器开关通常不会将库公开的功能转换为语言标准指定的功能。 (毕竟,按照这个逻辑得出的结论意味着编译器应该阻止你使用标准库以外的任何库,因为它们没有被语言标准指定。)
如果要禁用C库的非标准特性,glibc提供feature test macros for that purpose. asprintf
is exposed, among others, when the _GNU_SOURCE
macro is defined. Ensuring the macro is undefined before including standard library headers would work for that purpose… if it weren’t for the fact that libstdc++ requires _GNU_SOURCE
to be enabled.
考虑到这一点,我不认为你的问题有解决方案,只要你坚持使用 G++ 和 libstdc++。