This is my library code. Here I am using asprintf
which is a gnu extension of sprintf
.
So I was expecting the compiler to throw error if I enable -pedantic
switch.
But I got no error/warning/message while creation of the object file.
#include <iostream>
#include <stdio.h>
using namespace std;
int testPrint()
{
char *arr;
asprintf(&arr,"%d",10);
cout<<"testPrint called"<<endl;
return 1;
}
Command:
g++ -pedantic -Werror test.cpp -c
What changes should I make to print error for non standard C++ usage.
Related SO question for -pedantic
usage: how-to-disable-gnu-c-extensions
GCC documentation’s description of the -pedantic
option states:
-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
is not a strict ISO standard conformance check. All it does is enable warnings that are required by the standard. Using -std=c++XX
would also disable language extensions.
asprintf
is a feature of the C library (most likely glibc), not of the compiler. Compiler switches do not generally restrict features exposed by libraries to those that are specified by the language standard. (After all, following this to the logical conclusion would mean the compiler should prevent you from using any library other than the standard library, since they aren’t specified by the language standard.)
If you want to disable non-standard features of the C library, glibc provides 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.
With that in mind, I don’t think your problem has a solution, as long as you stick to G++ and libstdc++.