gpt4 book ai didi

C11 _Generic c 程序和 Eclipse c/c++ 上的错误

转载 作者:行者123 更新时间:2023-12-01 15:02:07 30 4
gpt4 key购买 nike

你好,我第一次使用 C primer plus book 学习 C,然后在第 16 章关于 C11 标准的 _Generic 我在 Eclipse c/c++ 中编写了一个程序并构建它产生了 8 个错误和警告,我不知道为什么这里是程序和错误

#include <stdio.h>
#include <math.h>
#define RAD_TO_DEG (180/(4 * atanl(1)))

// generic square root function
#define SQRT(X) _Generic((X),\
long double: sqrtl, \
default: sqrt, \
float: sqrtf)(X)

// generic sine function, angle in degrees
#define SIN(X) _Generic((X),\
long double: sinl((X)/RAD_TO_DEG),\
default: sin((X)/RAD_TO_DEG),\
float: sinf((X)/RAD_TO_DEG)\
)

int main(void)
{
float x = 45.0f;
double xx = 45.0;
long double xxx =45.0L;

long double y = SQRT(x);
long double yy= SQRT(xx);
long double yyy = SQRT(xxx);
printf("%.17Lf\n", y); // matches float
printf("%.17Lf\n", yy); // matches default
printf("%.17Lf\n", yyy); // matches long double
int i = 45;
yy = SQRT(i); // matches default
printf("%.17Lf\n", yy);
yyy= SIN(xxx); // matches long double
printf("%.17Lf\n", yyy);

return 0;
}

错误

make all 
Building file: ../generic.c
Invoking: GCC C Compiler
gcc -std=c11 -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"generic.d" -MT"generic.d" -o "generic.o" "../generic.c"
../generic.c: In function ‘main’:
../generic.c:24:5: warning: implicit declaration of function ‘_Generic’ [-Wimplicit-function-declaration]
long double y = SQRT(x);
^
../generic.c:7:5: error: expected expression before ‘long’
long double: sqrtl, \
^
../generic.c:24:21: note: in expansion of macro ‘SQRT’
long double y = SQRT(x);
^
../generic.c:7:5: error: expected expression before ‘long’
long double: sqrtl, \
^
../generic.c:25:21: note: in expansion of macro ‘SQRT’
long double yy= SQRT(xx);
^
../generic.c:7:5: error: expected expression before ‘long’
long double: sqrtl, \
^
../generic.c:26:23: note: in expansion of macro ‘SQRT’
long double yyy = SQRT(xxx);
^
../generic.c:7:5: error: expected expression before ‘long’
long double: sqrtl, \
^
../generic.c:31:10: note: in expansion of macro ‘SQRT’
yy = SQRT(i); // matches default
^
../generic.c:13:1: error: expected expression before ‘long’
long double: sinl((X)/RAD_TO_DEG),\
^
../generic.c:33:10: note: in expansion of macro ‘SIN’
yyy= SIN(xxx); // matches long double
^
make: *** [generic.o] Error 1

14:47:53 Build Finished (took 66ms)

我为 math.h 使用了 -lm 链接,它产生了这些错误,我不知道为什么?

最佳答案

原因:

_Genericgcc 版本 4.9 之前不受支持,请参阅:https://gcc.gnu.org/wiki/C11Status

解决方案:

尝试更新版本的 gcc

示例:

a.c 中是您提供的代码:

[pengyu@GLaDOS tmp]$ gcc a.c -std=c11 -lm -Wall -pedantic
[pengyu@GLaDOS tmp]$ gcc --version | head -n 1
gcc (GCC) 4.9.1 20140903 (prerelease)

关于C11 _Generic c 程序和 Eclipse c/c++ 上的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26074869/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com