当使用 pFUnit (3.2.9) 测试我的 Fortran 代码时,我收到许多“非法预处理器指令”警告,例如
Warning: Illegal preprocessor directive
/path/to/my/file/test.f90:37:2:
#line 26 "/path/to/my/file/test.f90"
1
Run Code Online (Sandbox Code Playgroud)
代码编译并运行良好,因此我想关闭这些警告,同时仍然看到其他编译器警告。哪个 gfortran 编译器标志会关闭此特定警告?我正在使用 gfortran 7.3.1。
我正在运行一个 CTF,目前正在编写一个利用 Cgets函数的问题。我知道该函数已被弃用且危险,我绝不会在任何其他情况下使用它。不幸的是,gcc编译我的代码,当我在函数被调用时运行二进制文件时gets,我收到一条友好的错误消息:
warning: this program uses gets(), which is unsafe.
Run Code Online (Sandbox Code Playgroud)
这通常会很好,因为它会警告您 gets 是不安全的,但不幸的是,在我的 CTF 中,我认为这个错误消息使问题变得有点太简单了。你知道我将如何禁用此警告吗?谢谢!
warning: this program uses gets(), which is unsafe.
Run Code Online (Sandbox Code Playgroud) 我有以下变量。
char **arr
然后我想对数组进行一些修改,这意味着它不能被声明为常量。
现在我有一个接受 type 参数的函数const char ** arr。但是我无法控制这个函数的签名。
现在,当我转换arr为const char ** arrg++ 时会生成一个警告,即[-Werror=cast-qual].
有关更多说明,请考虑以下 MCVE:
#include<cstdio>
void print(const char** x){
printf("%s", x[0]);
}
int main(int argc, char **argv){
if(argc>1){
print((const char **)argv);
}
return 0;
}
//Then compile it as follow:
$ g++ -Wcast-qual test.cpp
//gives the following output:
MCVE.cpp: In function ‘int main(int, char**)’:
MCVE.cpp:5:36: warning: cast from type ‘char**’ to type ‘const char**’ casts away qualifiers [-Wcast-qual]
const …Run Code Online (Sandbox Code Playgroud) 当我尝试编译特定程序时-Wall,GCC显示警告:
expcal.c:66:5: warning: statement with no effect [-Wunused-value]
Run Code Online (Sandbox Code Playgroud)
这个警告是指线:
ed.operator[j] == str[i];
Run Code Online (Sandbox Code Playgroud)
在以下循环中找到:
for(i=0;i<strlen(str);i++)
{
j=0;
if(str[i] == '+' || str[i] == '-' || str[i] == '*')
{
if(str[i+1] == '+' || str[i+1] == '-' || str[i+1] == '*')
return 0;
else
{
//j=0;
ed.operator[j] == str[i];
count++;
j++;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我知道当一个赋值语句出现问题时会出现这个警告.上面的代码有什么问题会导致GCC产生这样的警告?
如果我编译我的程序cc然后它的工作原理.但我必须使用编译gcc -pedantic -Wall -ansi -O4.然后我收到了声明的警告wait(NULL)
miniShell.c: In function ‘main’:
miniShell.c:84:13: warning: implicit declaration of function ‘wait’ [-Wimplicit-function-declaration]
wait(NULL);
^
Run Code Online (Sandbox Code Playgroud)
我可以重写它以取悦编译器吗?
我在codegolf.stackexchange上找到以下代码来打印ASCII字符的代码表:
#include <stdio.h>
int main(){
char i;
for(i = 0; i < 256; i++){
printf("%3d 0x%2x: %c\n", i, i, i);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
由于chars中存储了单个字节,因此它们始终存在< 256且循环永远不会终止.我想在编译时发现这一点.
很好,clang给出以下警告:
a.c:5:18: warning: comparison of constant 256 with expression of type 'char' is always true [-Wtautological-constant-out-of-range-compare]
for(i = 0; i < 256; i++){
~ ^ ~~~
Run Code Online (Sandbox Code Playgroud)
但是,既没有gcc也没有gcc -Wall任何形式的警告.是否有任何一组命令行选项可以打开这种警告?或者在gcc中不可能吗?
我看到了关于代码所使用的C的另一个问题gets(),我评论了关于从不使用的常见警告,gets()除非您想要演示如何破坏安全性.
这一次,我决定检查我的编译器是否发出了关于使用的警告gets().当然我希望它会.对,对吧?即使您没有指定任何警告?
想象一下,我的惊讶,当我发现,不仅编译器不会 默认警告,但我甚至不能弄清楚如何使之警告!
有问题的编译器是关于Debian的gcc 4.7.2,这是我使用的代码:
#include <stdio.h>
int main(void)
{
char s[10];
gets(s);
puts(s);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
尝试gcc g.c.编译时没有警告.运行.如果输入太多文本,则获取段错误.
试过我通常放在makefile中的所有标准警告:
gcc -W -Wall -Wno-long-long -Wshadow -Wlarger-than-1000 \
-Wpointer-arith -Wbad-function-cast -Wcast-qual -Wcast-align \
-Wconversion -Waggregate-return -Wmissing-prototypes \
-Wmissing-declarations -Wpadded -Wredundant-decls -Wnested-externs g.c
Run Code Online (Sandbox Code Playgroud)
结果相同.
试过-std=c11.考虑gets()到C11中甚至不存在,即使这样也没有产生警告,这非常奇怪.也试过
c99.没有警告.
那么这里发生了什么?当我在整个C语言中使用最弃用的函数时,为什么这个使用非常广泛的编译器不会警告我?
编辑:根据Keith Thompson的建议,我检查了一个弃用属性stdio.h.它不存在.然后我复制了头文件并进行了实验.将这些字符串(我在其他标题中找到)添加到声明的末尾会产生警告:
__attribute_deprecated__
__attribute__ ((__deprecated__))
Run Code Online (Sandbox Code Playgroud)
警告:
‘gets’ is deprecated (declared at /usr/include/stdiotz.h:632) [-Wdeprecated-declarations]
Run Code Online (Sandbox Code Playgroud)
总结一下我到目前为止看到的响应,似乎我系统上的libc版本不包含警告,这在以后的版本中确实存在.这很奇怪,因为这个警告至少从1996年开始以某种形式存在.我模糊地回忆起libc已经至少分叉了一次,所以也许警告被遗漏在一个分支之外的时间明显晚于其他分支.
我想我会在Debian邮件列表上询问这个问题,并根据我学到的内容将其报告为bug. …
在我的项目的一个头文件中,以下行包含在inline方法中
typedef boost::archive::iterators::transform_width<boost::archive::iterators::binary_from_base64< boost::archive::iterators::remove_whitespace<std::string::const_iterator>>, 8, 6> Base64ToBin;
Run Code Online (Sandbox Code Playgroud)
当我使用gcc 4.8.2编译它时,我收到以下错误:
错误:'boost :: archive :: iterators :: remove_whitespace <__ gnu_cxx :: __ normal_iterator >>'有一个字段'boost :: archive :: iterators :: remove_whitespace <__ gnu_cxx :: __ normal_iterator >>> ::'其类型使用匿名命名空间[-Werror]
我真的很努力,但无法解决这个问题,同样来自link1和link2,看起来这是一个较低版本的gcc的问题.有人可以建议如何使这个警告保持沉默或者克服这个警告.我正在使用-Werror标志编译.
(长话故事......你可以直接跳到最后的问题......)
我需要使用realpath(3)所以我写了一个简单的例子来尝试它:
$> cat realpath.c
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
int main(int argc, char * argv[])
{
char pathname[PATH_MAX];
if (realpath(argv[1], pathname) < 0) {
perror("realpath");
return 1;
}
printf("%s\n", pathname);
return 0;
}
$> gcc -Wall -o realpath realpath.c
$> ls /xxx
ls: cannot access '/xxx': No such file or directory
$> ./realpath /xxx/foo/bar
/xxx
Run Code Online (Sandbox Code Playgroud)
结果./realpath /xxx/foo/bar让我感到惊讶.根据手册,失败更有意义ENOENT.我甚至提到了POSIX并没有找到答案.经过一段时间后,我重读了手册并找到了realpath(3)退货char *而不是int.我真的很恼火gcc.
那么为什么 gcc(甚至没有 …
当我忘记用分号结束变量初始化而取一个逗号时,我曾在代码中出错。但是,令我惊讶的是,它从未返回错误,并且代码正常工作。
因此我想知道这是如何工作的?我通过编写以下代码简化了代码;
uint32_t randomfunction_wret()
{
printf("(%d:%s) - \n", __LINE__, __FILE__);
return 6;
}
uint32_t randomfunction()
{
printf("(%d:%s) - \n", __LINE__, __FILE__);
}
int main()
{
uint32_t val32 = 3, randomfunction_wret(), valx = 6, randomfunction();
printf("(%d:%s) - %u %u\n", __LINE__, __FILE__, val32, valx);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
执行时返回;
(43:test.c) - 3 6
Run Code Online (Sandbox Code Playgroud)
我对初始化中分离的函数没有错误感到非常震惊。但是,这些功能甚至都没有被调用。
=============更新
从我所看到的来看,现在代码是否如下所示,现在每个函数都被调用了。
int main()
{
uint32_t val32;
val32 = 3, randomfunction_wret(), randomfunction();
printf("(%d:%s) - %u \n", __LINE__, __FILE__, val32);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出将是
(23:test.c) -
(29:test.c) -
(38:test.c) - …Run Code Online (Sandbox Code Playgroud)