简体   繁体   English

G++ 无法编译 __attribute__ 关键字

[英]G++ failed to compile __attribute__ keyword

I tried compiling attribute with g++, but failed, gcc will compile successfully.我尝试用 g++ 编译属性,但失败了,gcc 将编译成功。 g++ test.c -o test g++ test.c -o 测试

Here is the function:这是功能:

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

struct student{
    int num;
};

static __inline int student_information (struct student *) __attribute__((__unused__));
static __inline int student_information(stu) struct student *stu;
{
    return 0;
}

int main(void)
{
    struct student *stu = (struct student *)malloc(sizeof(struct student));
    student_information(stu);

    return 0;
}

Here is the failture message:这是失败消息:

test.c:9:44: error: ‘student_information’ declared as an ‘inline’ variable
 static __inline int student_information(stu) struct student *stu;
                                            ^
test.c:9:44: error: ‘int student_information’ redeclared as different kind of symbol
test.c:8:21: error: previous declaration of ‘int student_information(student*)’
 static __inline int student_information (struct student *) __attribute__((__unused__));
                     ^
test.c:9:41: error: ‘stu’ was not declared in this scope
 static __inline int student_information(stu) struct student *stu;
                                         ^
test.c:10:1: error: expected unqualified-id before ‘{’ token
 {
 ^
test.c:8:21: warning: inline function ‘int student_information(student*)’ used but never defined [enabled by default]
 static __inline int student_information (struct student *) __attribute__((__unused__));

I don't know why this is wrong, but how do I compile attribute with g++我不知道为什么这是错误的,但是我如何用 g++ 编译属性

It's because g++ is a C++ compiler and you're giving it invalid C++: it has to reject this code, so it just tells you why and quits.这是因为 g++ 是一个 C++ 编译器,而你给它的 C++ 是无效的:它必须拒绝这段代码,所以它只会告诉你原因并退出。 Also, the __attribute__ keyword is a compiler-specific extension, which in this case could be replaced by [[maybe_unused]] .此外, __attribute__关键字是特定于编译器的扩展,在这种情况下可以替换为[[maybe_unused]] In fact nothing in standard C++ starts with any underscores: such names are reserved for use as custom extensions or implementation details by compiler and standard library implementations.事实上,标准 C++ 中的任何内容均不以任何下划线开头:此类名称保留用于编译器和标准库实现的自定义扩展或实现细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM