标签: gcc-warning

g ++警告:无符号表达式的比较<0始终为false

要编译我的C++代码,我使用-W标志,它会导致警告:

警告:无符号表达式的比较<0始终为false

我认为这被视为一个错误,并在GCC 4.3版本上得到修复,但我使用的是GCC 4.1

代码显然在这里冒犯:

void FieldGroup::generateCreateMessage (const ApiEvent::GroupData &data, omsgstream &result) const {
  dblog << debug;

  // Write out the data fields we care about, in the order they were specified
  for (size_t index = 0; index < fields.size(); ++index) {
    size_t esIndex = clsToES[index];
    if (esIndex < 0 || esIndex >= data.fields.length()) {
      ostringstream buf;
      buf << "Invalid field " << index << " (index in ES data set " << esIndex << ", " …
Run Code Online (Sandbox Code Playgroud)

c++ gcc gcc-warning

3
推荐指数
2
解决办法
1万
查看次数

未定义的行为或误报

我(基本上)在野外遇到以下情况

x = x = 5;
Run Code Online (Sandbox Code Playgroud)

显然在早期版本的gcc下完全编译(在gcc 4.5.1下生成警告).据我所知,警告是由-Wsequence-point生成的.所以我的问题是这是否违反了关于在序列点之间操纵变量的标准中的措辞(即,它是根据规范的未定义行为)或者这是gcc误报(即,它是根据规范定义的行为)?关于序列点的措辞有点难以理解.

我说的基本上是因为我实际遇到的(在更大的表达中)是

x[0][0] = x[0][0] = 5;
Run Code Online (Sandbox Code Playgroud)

但我认为这不是警告的重要因素(请纠正我,如果这是重点,而不是我认为是问题的关键).

c++ gcc g++ gcc-warning

3
推荐指数
1
解决办法
206
查看次数

在编译C90代码时,在gcc编译器中获取警告"ISO C90禁止可变大小数组"

我正在用gcc编译我的C90 c代码.我ISO C90 forbids variable-size array在发表声明时收到了警告

int symbols[nc];

其中nc是整数,其值从输入文件中读取.输入文件的值是变化的,所以我不能保持一个恒定的值.我怎么能摆脱它?确实有必要解决这个警告,或者我们可以忽略它吗?

提前致谢.

c89 gcc-warning

3
推荐指数
1
解决办法
5092
查看次数

如何抑制每个文件的-Wno协议

我实现了Objective-C Protocol,它将所有协议的方法转发给另一个目标.Everething很好,除了编译器警告这个类没有实现协议的方法.我正在尝试使用#pragma diagnostic来抑制此警告:

//Header file
@protocol A
-(void)test;
@end

@interface AImpl : NSObject<A> {
    id<A> myItems;
}
@end

//Implementation file:
#pragma GCC diagnostic push
#pragma clang diagnostic push

#pragma GCC diagnostic ignored "-Wno-protocol"
#pragma clang diagnostic ignored "-Wno-protocol"

@implementation AImpl
- (void)forwardInvocation:(NSInvocation *)invocation {
SEL selector = [invocation selector];

    if ([myItems respondsToSelector:selector]) {
    [invocation invokeWithTarget:myItems];
} else {
    [super forwardInvocation:invocation];
}
}
@end

#pragma clang diagnostic pop
#pragma GCC diagnostic pop
Run Code Online (Sandbox Code Playgroud)

但编译器警告"未知警告组'-Wno-protocol'"

xcode gcc clang gcc-warning ios

3
推荐指数
1
解决办法
1790
查看次数

在#include指令结束时禁止GCC警告"额外令牌"

我正在用C语言编写一个程序,用于编译并在HP NonStop机器上运行.但是,我想在运行Linux的工作站上进行主要开发.HP NonStop C编译器需要非标准的#include指令,如下所示:

#include <stdio.h> nolist
Run Code Online (Sandbox Code Playgroud)

对于每个#include指令,我的工作站的GCC抱怨:

S88USF.c:139:21: warning: extra tokens at end of #include directive
Run Code Online (Sandbox Code Playgroud)

我怎么能抑制这个特别警告?

注意:在SO上,已经提出类似的问题,正确的答案是"不要让gcc首先抱怨任何理由".但是,在这种情况下,我明确地希望完全按原样使用#include指令.

我知道我在做什么,我只是不知道如何通知gcc.

c gcc warnings compiler-warnings gcc-warning

3
推荐指数
1
解决办法
2394
查看次数

当指针指向指针时,我会收到警告:赋值从指针生成整数

我讨厌发布这个,因为有这么多,但它们似乎都没有解决我所看到的.正常问题(未声明的函数,无意的强制转换,对基本指针的误解)似乎不适用于此处.这是我的代码的精简版:

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

extern void* malloc ( size_t size );

typedef struct {
    size_t      size;
    uint8_t*    buffer, curr, next;
} buffer_t;

void init( buffer_t* b, int size ) {
    b->size   = (size_t) size;
    b->buffer = (uint8_t*) malloc( sizeof(uint8_t) * b->size + 1 );
    b->curr   = (uint8_t*) b->buffer; // warning: assignment makes integer from pointer without a cast [enabled by default]
    b->next   = (uint8_t*) b->buffer; // warning: assignment makes integer from pointer without a cast [enabled by …
Run Code Online (Sandbox Code Playgroud)

c pointers gcc-warning

3
推荐指数
1
解决办法
443
查看次数

警告:隐式声明函数'getresuid'(和'seteuid')

我想摆脱警告.当我编译源代码时

gcc -Wall -ansi -o test test.c  
Run Code Online (Sandbox Code Playgroud)

我回来了

test.c: In function ‘main’:
test.c:12: warning: implicit declaration of function ‘getresuid’
test.c:14: warning: implicit declaration of function ‘seteuid’
Run Code Online (Sandbox Code Playgroud)

当我编译它没有-ansi开关

gcc -Wall -o test test.c 
Run Code Online (Sandbox Code Playgroud)

我在终端上看到了

test.c: In function ‘main’:
test.c:12: warning: implicit declaration of function ‘getresuid’
Run Code Online (Sandbox Code Playgroud)

我想用-ansi开关并摆脱警告.我怎样才能实现目标?

/*  this is the test.c */
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

#define __USE_GNU 1
#define __USE_BSD 1

int main()
{
   static uid_t euid, ruid, suid;

   getresuid(&ruid, &euid, &suid);  

   seteuid(getuid()); 

   return …
Run Code Online (Sandbox Code Playgroud)

c linux gcc compiler-warnings gcc-warning

3
推荐指数
1
解决办法
5039
查看次数

代码生成wreorder警告

我有以下课程

#include <string>

class A {
 protected:
   std::string m1;
   int port;

 public:
   std::string m2;
   A(std::string,std::string,int);


};

A::A(std::string _m1,std::string _m2,int _port) : m1(_m1),m2(_m2),port(_port){
}

int main(int argc, char *argv[]){
  A("x","y",argc);
}
Run Code Online (Sandbox Code Playgroud)

当用gcc ARM 5.40编译并-Wreorder输出时

a.cpp: In constructor ‘A::A(std::__cxx11::string, std::__cxx11::string, int)’:
a.cpp:9:16: warning: ‘A::m2’ will be initialized after [-Wreorder]
    std::string m2;
                ^
a.cpp:6:8: warning:   ‘int A::port’ [-Wreorder]
    int port;
        ^

a.cpp:15:1: warning:   when initialized here [-Wreorder]
 A::A(std::string _m1,std::string _m2,int _port) : m1(_m1),m2(_m2),port(_port){
 ^
Run Code Online (Sandbox Code Playgroud)
  1. 为什么会产生警告?

  2. 威尔m2port具有缺省值或值分配main …

c++ gcc-warning c++11

3
推荐指数
1
解决办法
543
查看次数

G ++不允许我定义名为"major"的成员函数

所以,今天我编写了一些单元测试,突然G ++给了我一个关于GNU C和我的一个成员函数的意外警告major.为什么我不能在major没有触发G ++的情况下命名成员函数?

这是一个最低限度可行的测试片段:

// Any of these includes trigger the warnings
#include <random>
#include <cstdlib>

class my_class {
public:
    void major() const;
};

inline void my_class::major() const {}

int main(void) {
    my_class my_obj;
    my_obj.major();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是编译的输出(使用g++ --std=c++14 -o test-gcc-major test-gcc-major.cpp):

[flisboac@sonic ~]$ uname -a && lsb_release -a && g++ -v && g++ --std=c++14 -o test-gcc-major test-gcc-major.cpp && ./test-gcc-major 
Linux sonic 4.12.10-1-ARCH #1 SMP PREEMPT Wed Aug 30 12:18:42 CEST 2017 …
Run Code Online (Sandbox Code Playgroud)

c++ g++ libc libstdc++ gcc-warning

3
推荐指数
1
解决办法
211
查看次数

为什么gcc警告我这条线是"误导性地缩进,就好像它被"一个if?

警告是:

/home/dronz/OF/apps/myApps/HexMap/src/HexMap.cpp:48:5: warning: this ‘if’ clause 
does not guard... [-Wmisleading-indentation]
     if (toHexSize < 1)
     ^~
/home/dronz/OF/apps/myApps/HexMap/src/HexMap.cpp:51:2: note: ...this statement, 
but the latter is misleadingly indented as if it were guarded by the ‘if’
  MapTileSizeAtZoom = toHexSize;
  ^~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

代码是这样的:

if (toHexSize < 1)
    toHexSize = 1;

MapTileSizeAtZoom = toHexSize;
Run Code Online (Sandbox Code Playgroud)

如果MapTileSizeAtZoom ...线条缩进更多,我可以看到它有误导性,但它与'if'处于相同的缩进水平,所以这对我来说似乎是正确的.

我想也许有额外的空间和/或标签浮动,但我在文本后删除了任何不必要的空白字符,这没有任何区别.

我想也许它被空白线弄糊涂了,但把它取出并没有停止警告.

此外,在相同的.cpp文件之前是这段代码,它没有警告:

if (toHexSize < 1)
    toHexSize = 1;

HexInfo centerOnHex;
if (SelectedHex.type != -1)
Run Code Online (Sandbox Code Playgroud)

所以我想知道为什么它会警告一个(根本),以及它为什么不警告另一个,这是否是一个gcc bug,以及如何避免它?

...

编辑:@NeilButterworth让我发布"所有代码".(似乎没有相关性,但他有2.5万的声誉,所以我在下面添加了它.)

#include "HexMap.h"
#include <algorithm>
#include <cmath>

//-------------------------------------------------------------- …
Run Code Online (Sandbox Code Playgroud)

c++ gcc-warning

3
推荐指数
1
解决办法
4986
查看次数

标签 统计

gcc-warning ×10

c++ ×5

gcc ×5

c ×3

compiler-warnings ×2

g++ ×2

c++11 ×1

c89 ×1

clang ×1

ios ×1

libc ×1

libstdc++ ×1

linux ×1

pointers ×1

warnings ×1

xcode ×1