标签: gcc-warning

如何修复 gcc 警告“友元声明声明非模板函数”

所以我这里有一些使用 gcc、clang 和 msvc 编译的代码:

#include <cstdio>
#include <type_traits>

struct c_class;

template <class T> struct holder { friend auto adl_lookup(holder<T>); };

template <class C, class T> struct lookup {
  friend auto adl_lookup(holder<T>) { return holder<C>{}; }
};

struct cpp_class : lookup<cpp_class, c_class *> {
  cpp_class() {}
};

int main() {
  static_assert(std::is_same<holder<cpp_class>,
                             decltype(adl_lookup(holder<c_class *>{}))>{},
                "Failed");
}
Run Code Online (Sandbox Code Playgroud)

之所以在类adl_lookup中定义lookup而不是在类中定义,是为了在继承CRTP类时holder可以从c_class到进行“反向”查找。所以友元函数不能移到类中。cpp_classlookup<cpp_class, c_class *>holder

但是,在 gcc 上,我收到有关非模板友元函数的警告:

<source>:9:37: warning: friend declaration 'auto adl_lookup(holder<T>)' declares a …
Run Code Online (Sandbox Code Playgroud)

c++ templates gcc-warning language-lawyer c++14

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

为什么 GCC make 仅在使用结构名称而不是 typedef 时才会抛出错误和不必要的警告?

我有一个由两个源文件(farm.c,init.c)和两个相应的头文件(farm.h,init.h)组成的程序两个源文件都包含头保护和彼此,因为它们都需要来自的函数/变量彼此。

\n

初始化.h:

\n
#ifndef INIT_H\n#define INIT_H\n\n#include<stdio.h>\n#include<stdlib.h>\n#include"farm.h"\n\n#define PIG_SOUND "oink"\n#define CALF_SOUND "baa"\n\nenum types {PIG, CALF};\n\ntypedef struct resources {\n    size_t pork;\n    size_t veal;\n    size_t lamb;\n    size_t milk;\n    size_t eggs;\n} resources;\n\ntypedef struct animal {\n    size_t legs;\n    char* sound;\n    int efficiency;\n    void (*exclaim)(struct animal*);\n    void (*work)(struct animal*, struct resources*);\n} animal;\n\n/* I have tried various ways of declaring structs in addition to\n   the typedef such as this */\n\n//animal stock;\n//animal farm;\n\nvoid make_pig(struct animal* a, int perf);\nvoid make_calf(struct animal* a, int perf);\n\n#endif\n
Run Code Online (Sandbox Code Playgroud)\n

农场.h:

\n
#ifndef FARM_H\n#define …
Run Code Online (Sandbox Code Playgroud)

c struct pointers gnu-make gcc-warning

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

Xcode警告:"属性'<x>'及其超类'<y>'没有匹配的'原子'属性"

在编译几个现有Cocoa类的子类时,我收到了Xcode警告.例如,以下类

   @interface HMAttitude : CMAttitude
    {
        double pitch;
        double roll;
        double yaw;
    }

    @property (readwrite) double pitch;
    @property (readwrite) double roll;
    @property (readwrite) double yaw;

    @end
Run Code Online (Sandbox Code Playgroud)

-

@implementation HMAttitude

@synthesize pitch, roll, yaw;

- (id) init
{
    return [super init];
}

@end
Run Code Online (Sandbox Code Playgroud)

产生三个警告

警告:属性'yaw'及其超类'CMAttitude'没有匹配的'atomic'属性

警告:属性'pitch'及其超类'CMAttitude'没有匹配的'atomic'属性

警告:属性'roll'及其超类'CMAttitude'没有匹配的'atomic'属性

所有子类都是必需的,以便创建能够像超类一样运行的CMMotionManager和CLLocationManager子类,只加载来自csv文件的数据.我将它们子类化的唯一原因是获得访问(或覆盖)其只读属性.如果没有设置这些属性的能力,我无法返回与真实CMMotionManager和CLLocationManager类相同的对象.

目前一切正常,除了必须使用#pragma忽略稍微困扰我的警告.

有谁知道为什么会产生这个警告?鉴于属性未设置为非原子(原子是默认值),我绝对没有线索.

为了使这些属性成为原子,我需要明确做些什么吗?

xcode objective-c gcc-warning

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

在C中定义函数之前使用函数时的隐式声明,为什么编译器无法解决这个问题呢?

正如标题所说,我知道导致此错误的原因,但我想知道为什么编译器会在这种情况下提供它.

例如:

main.c中

void test(){
    test1();
}

void test1(){
   ...
}
Run Code Online (Sandbox Code Playgroud)

会给出一个隐式声明警告,因为编译器在读取它的声明之前会调用test1(),我可以看到这个明显的问题(不知道返回类型等),但为什么编译器不能做一个简单的传递获取所有函数声明,然后编译删除这些错误的代码?这看起来很简单,我不相信我在其他语言中看到过类似的警告.

在这种情况下,有人知道这个警告是否有特定目的我忽略了吗?

c gcc compiler-warnings gcc-warning

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

除stdlib.h之外的其他地方是否声明了exit()函数?

在尝试编译下面的示例时,我收到了一条警告:

>gcc -o file file.c
file.c: In function ‘main’:
file.c:12: warning: incompatible implicit declaration of built-in function ‘exit’
Run Code Online (Sandbox Code Playgroud)

经过一番搜索,我意识到这个例子缺少陈述#include <stdlib.h>.那么exit()函数在哪里宣布?图书馆stdio.h没有声明它.我的代码也没有.如果编译器支持它,为什么它会发出警告?另外,为什么要重新定义stdlib.h

例:

#include <stdio.h>

int main()
{
    char *fn = "./test.txt";
    FILE *fp;

    if((fp = fopen(fn, "w"))==NULL)
    {
        printf("Cannot open file '%s' for writing.\n", fn);
        exit(1);
    }

    fprintf(fp, "Hello, world!\n");

    if(fclose(fp)==0)
        printf("File '%s' closed successfully.\n", fn);
    else
        printf("Error closing file '%s'.\n", fn);

    return 0;    
}
Run Code Online (Sandbox Code Playgroud)

c gcc gcc-warning

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

赋值使整数警告指针

标题:

#ifndef BIT_H_INCLUDE_GUARD
#define BIT_H_INCLUDE_GUARD

typedef unsigned char byte;

typedef struct{
    size_t* size;
    byte* map;
} bit;

bit* bdcteate(byte* size);

#endif /* BIT_H_INCLUDE_GUARD */
Run Code Online (Sandbox Code Playgroud)

资源:

#include <stdlib.h>
#include "bit.h"

bit* bdcreate(byte* size){
    bit* d;
    byte i;
    size_t s = 0;
    for(i = 1; i < size[0]; i++){
        s += (size_t) size[i];
    }
    if(!(d = malloc(sizeof(bit)))){
        return (bit*) NULL;
    }
    if(!(d->size = malloc(sizeof(size_t)))){
        return (bit*) NULL;
    }
    if(!(d->map = malloc(s * sizeof(byte)))){
        return (bit*) NULL;
    }
    *d->size = s;
    return (bit*) …
Run Code Online (Sandbox Code Playgroud)

c header gcc-warning

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

当一个求值为0的整数枚举被用作指针时,gcc会发出警告吗?

以下程序编译时没有警告(这是不可取的,因为省略第19行和第21行的数组索引会有效地破坏数组).如果使用-D CHECK_NONZERO进行编译,您将看到第23行在没有警告的情况下将无法编译,因为枚举BBB的计算结果为1,其中AAA和aaa计算为0.

似乎如果枚举计算结果为0,gcc将无缝地将其转换为NULL指针.

这应该被视为一个错误吗?

编辑:我不认为我对我所认为的问题一如既往.在我看来,在将枚举解析为常量值之前,为了警告目的而对枚举进行类型检查是没有害处的,但这不是gcc目前的工作方式.但是,我不确定这是否值得对gcc项目的错误报告或功能请求.

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

typedef enum {
    AAA,
    BBB,
} alpha_e;

enum {
    aaa,
    bbb,
};

int main(void) {
    alpha_e *alpha_array = malloc(sizeof(*alpha_array) * 2);
    alpha_array[0] = AAA;
    alpha_array[1] = BBB;
    printf("1: alpha_array[0] == %u, alpha_array[1] == %u\n", alpha_array[0], alpha_array[1]);
    alpha_array = AAA;
    printf("2: alpha_array[0] == %u, alpha_array[1] == %u\n", alpha_array[0], alpha_array[1]);
    alpha_array = aaa;
#ifdef CHECK_NONZERO
    alpha_array = BBB;
#endif
    return 1;
}
Run Code Online (Sandbox Code Playgroud)

gcc -v:

Using built-in specs.
Target: x86_64-linux-gnu
Configured with: …
Run Code Online (Sandbox Code Playgroud)

c gcc gcc-warning

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

通过硬编码地址创建指向结构的指针数组时避免gcc警告

假设我有一个充当寄存器映射的结构.

typedef struct{
    int reg1;
    int reg2;
} regs;
Run Code Online (Sandbox Code Playgroud)

我的寄存器有一些常量地址

# define ADDR1 0x60000000
# define ADDR2 0x70000000
# define ADDR3 0x80000000
# define ADDR4 0x90000000
Run Code Online (Sandbox Code Playgroud)

为了让事情更容易循环,我想把它们放在一个数组中

regs * reg_list[4] = { ADDR1, ADDR2, ADDR3, ADDR4 };
Run Code Online (Sandbox Code Playgroud)

当我用gcc和-wAll编译它时,我得到了数组中每个元素的以下警告.我正试图摆脱这个警告.

warning: initialization makes pointer from integer without a cast
Run Code Online (Sandbox Code Playgroud)

我可以将每个单独的地址输入到reg*但这看起来非常冗长.有没有更好的办法?

c arrays embedded pointers gcc-warning

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

GCC C警告“重复的'const'声明”

我为什么要

重复的'const'声明说明符[-Wduplicate-decl-specifier]

为了这个宣言?

extern uint8_t CalculateChecksum(const communicationBlock_t const *messageBlock);
Run Code Online (Sandbox Code Playgroud)

communicationBlock_t结构在哪里。

我没有功能可以更改参数所指向的结构,也不希望它可以将该参数指向其他位置。

我做错了什么?

c gcc gcc-warning

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

如何解决警告“编译器可以假设'object'的地址永远不会为NULL”

我使用gcc8编译以下代码:

#include <iostream>

class person
{
    public:
        virtual void setage()=0;
};

void test(person &object)
{
    if (&object == NULL) {
        std::cout << "NULL object1" << std::endl;
    }

    if (!(&object)) 
    {
        std::cout << "NULL object1" << std::endl;
    }
}

int main()
{
    person *object=NULL;
    person &object1=*object;

    test(object1);
}
Run Code Online (Sandbox Code Playgroud)

然后,在编译并运行后会出现两个警告:

$ g ++ -std = c ++ 14 -pthread -fgnu-tm -O2 -Wall -Wextra -pedantic -pthread -pedantic-errors main.cpp -lm -latomic -lstdc ++ fs && ./a.out

main.cpp:在函数“ void test(person&)”中:

main.cpp:11:17:警告:编译器可以假定“对象”的地址永远不会为NULL [-Waddress]

 if (&object == …
Run Code Online (Sandbox Code Playgroud)

c++ gcc-warning

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