这是我的警告.
implicit declaration of function 'exit'
Run Code Online (Sandbox Code Playgroud)
我怎么能删除它.
我正在使用linux和gcc编译器.
我想在代码库中找到未使用的函数 - 包括编译单元.我正在使用gcc作为我的编译器.
这是一个例子:
foo.c(假设合适foo.h):
void foo() {
....
}
void bar() {
....
}
Run Code Online (Sandbox Code Playgroud)
main.c:
#include <stdio.h>
#include "foo.h"
int main(void) {
bar();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我想得到关于foo()不被使用的警告.
有-Wunused-functiongcc选项:
-Wunused-function每当声明静态函数但未定义静态函数或未使用非内联静态函数时发出警告.-Wall启用此警告.
但它只适用于静态函数 - 它不会在上面的例子中产生警告.
我也接受可以为我做这个的工具/脚本/其他编译器的建议 - 尽管gcc如果可能的话我宁愿坚持下去.
在下面的代码中为什么b[9]未初始化而不是越界?
#include <stdio.h>
int main(void)
{
char b[] = {'N', 'i', 'c', 'e', ' ', 'y', 'o', 'u', '!'};
printf("b[9] = %d\n", b[9]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译器调用:
% gcc -O2 -W -Wall -pedantic -c foo.c
foo.c: In function ‘main’:
foo.c:6:5: warning: ‘b[9]’ is used uninitialized in this function [-Wuninitialized]
printf("b[9] = %d\n", b[9]);
% gcc --version
gcc (Ubuntu 5.4.0-6ubuntu1~16.04.6) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. …Run Code Online (Sandbox Code Playgroud) 我有一个头文件假设abc.h,其中我有函数声明为:
static int function1();
Run Code Online (Sandbox Code Playgroud)
我已将此头文件包含在abc.c中并定义了该函数并使用了它.
static int function1()
{
< function definition>
}
Run Code Online (Sandbox Code Playgroud)
编译后我收到警告:
warning: function1 declared static but never defined
Run Code Online (Sandbox Code Playgroud)
如何在不删除静电的情况下删除警告.谢谢.
我有一个基类和派生类的层次结构。基类具有一个虚函数,该虚函数被派生类覆盖。
class Base
{
public:
~Base();
virtual void other_functionality() = 0;
};
class Derived : public Base
{
public:
~Derived ();
void other_functionality() {//some code};
};
Run Code Online (Sandbox Code Playgroud)
现在,如果我这样做:
int main()
{
Base * P = new Derived ();
delete p;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它给出错误:
删除具有非虚拟析构函数的多态类类型的对象。
但是使用unique_ptr,它会通过而不会发出警告。
int main()
{
std::unique_ptr<Base> p;
p.reset(new Derived ());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道我是否使用虚拟析构函数。用裸露的指针警告将得到解决。但是问题仍然存在-为什么没有虚拟析构函数会显示裸指针而不是unique_ptr问题。
-Wlong-longgcc警告的原因是什么?
从gcc手册页:
-Wlong-long
Warn if long long type is used. This is enabled by either -Wpedantic or -Wtraditional in ISO C90 and C++98 modes. To inhibit the warning messages, use -Wno-long-long.
Run Code Online (Sandbox Code Playgroud)
据我了解,long long要求至少为64位(实际上它总是64位,至少与今天的编译器一样).这不是ISO C90或C++ 98的情况,还是有其他原因不使用long long?
我知道<stdint.h>类型int64_t和朋友的类型,但一些不太老的编译器(例如VS2005和Green Hills ARM 3.5)不提供<stdint.h>,我认为long long(至少)64位用于那些和更新的工具链.
实现弃用警告的一种方法是在调用已弃用的函数时生成警告,除非您从已弃用的上下文中调用.这样,遗留代码可以调用遗留代码,而不会产生仅相当于噪声的警告.
这是一个合理的思路,它反映在我在OS X上的GCC 4.2(1)和Clang 4.0(2)以及Ubuntu上的Clang 3.0(3)中看到的实现中.
但是,当我在Ubuntu上使用GCC 4.6(4)进行编译时,我对所有已弃用函数的调用都会被弃用警告,而与上下文无关.这是功能的回归吗?是否有可用于获取其他行为的编译器选项?
示例程序:
int __attribute__((deprecated)) a() {
return 10;
}
int __attribute__((deprecated)) b() {
return a() * 2; //< I want to get rid of warnings from this line
}
int main() {
return b(); //< I expect a warning on this line only
}
Run Code Online (Sandbox Code Playgroud)
GCC 4.2的输出(是的,我确实得到了两次相同的警告.但我并不关心这一点):
main.cpp: In function ‘int main()’:
main.cpp:10: …Run Code Online (Sandbox Code Playgroud) 显然,在gcc/C中,编译器编译时
if ((x=0)){ some code }
Run Code Online (Sandbox Code Playgroud)
当使用时
if (x=0){ some code }
Run Code Online (Sandbox Code Playgroud)
使用,然后编译器拒绝编译.
两者有什么不同?
作为一个说明,我知道x==0和之间有什么区别x=0.我正在探索C遇到一些奇怪的代码时的行为方式.
我在C中有这个结构,我想初始化为零.如何摆脱缺失的括号警告?
typedef struct {
uint32_t incoming[FRAME_TYPE_MAX];
uint32_t outgoing[FRAME_TYPE_MAX];
uint32_t timeouts;
uint32_t crc_errors;
} pkt_t;
static pkt_t stats = {0};
Run Code Online (Sandbox Code Playgroud) 我从GCC收到这个警告:
警告:不能传递非POD类型的对象'class Something'到'...'; call将在运行时中止
它非常致命,特别是因为它叫做中止.为什么这不是错误?我想把它弄错,但是:
-Wno-invalid-offsetof看起来像隐藏它的标志,但它不