man手册该选项的说明如下:
-fpermissive
Downgrade some diagnostics about nonconformant code from errors to warnings. Thus, using -fpermissive will allow some nonconforming code to compile.
该选项会将不一致代码的诊断从错误降级为警告。
该选项最好不要使用,因为会降低对于代码检查的严格性。
例子:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
const static char *source = "source";
int main(int argc, char *argv[])
{
char buf[512];
snprintf(buf, "%s", source);
return 0;
}
在该代码中,snprintf并没有使用第二个参数sizeof(buf),如果不使用该选项,编译结果如下所示:
syrius@AI-MACHINE:~ $ g++ g.cc
g.cc: In function ‘int main(int, char**)’:
g.cc:10:23: error: invalid conversion from ‘const char*’ to ‘size_t’ {aka ‘long unsigned int’} [-fpermissive]
10 | snprintf(buf, "%s", source);
| ^~~~
| |
| const char*
In file included from g.cc:1:
/usr/include/stdio.h:378:51: note: initializing argument 2 of ‘int snprintf(char*, size_t, const char*, ...)’
378 | extern int snprintf (char *__restrict __s, size_t __maxlen,
| ~~~~~~~^~~~~~~~
g.cc:10:17: warning: format not a string literal and no format arguments [-Wformat-security]
10 | snprintf(buf, "%s", source);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~
syrius@AI-MACHINE:~ $
syrius@AI-MACHINE:~ $ g++ g.cc -fpermissive
g.cc: In function ‘int main(int, char**)’:
g.cc:10:23: warning: invalid conversion from ‘const char*’ to ‘size_t’ {aka ‘long unsigned int’} [-fpermissive]
10 | snprintf(buf, "%s", source);
| ^~~~
| |
| const char*
In file included from g.cc:1:
/usr/include/stdio.h:378:51: note: initializing argument 2 of ‘int snprintf(char*, size_t, const char*, ...)’
378 | extern int snprintf (char *__restrict __s, size_t __maxlen,
| ~~~~~~~^~~~~~~~
g.cc:10:17: warning: format not a string literal and no format arguments [-Wformat-security]
10 | snprintf(buf, "%s", source);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~