2
void init(void) 
{
   glClearColor ((float)0.0, (float)0.0, (float)0.0, (float)0.0);
   glShadeModel (GL_FLAT);
}

的参数glClearColor是浮点数。但是 gcc 总是给出警告:

main.c: In function ‘init’:
main.c:12: warning: passing argument 1 of ‘glClearColor’ as ‘float’ rather than ‘double’ due to prototype
main.c:12: warning: passing argument 2 of ‘glClearColor’ as ‘float’ rather than ‘double’ due to prototype
main.c:12: warning: passing argument 3 of ‘glClearColor’ as ‘float’ rather than ‘double’ due to prototype
main.c:12: warning: passing argument 4 of ‘glClearColor’ as ‘float’ rather than ‘double’ due to prototype 

但我只是将一个数字转换为浮点数,而不是双精度数,不知道为什么。

我的操作系统是 mac os 64bit,下面是makefile.

UNAME := $(shell uname)

ifeq ($(UNAME), Darwin)
    CFLAG := -framework GLUT -framework OpenGL -g -Wall -Wconversion
else
    CFLAG := -lm -lglut -lGL -lGLU -g -Wall
endif

PUB_SRC := util.c plane.c dot.c linked_dots.c controller.c time_data.c
SRC := main.c $(PUB_SRC)
TEST_SRC := test.c $(PUB_SRC)

.PHONY : main
main: $(SRC)
    gcc $(CFLAG) $(SRC)

.PHONY : test
test: $(TEST_SRC)
    gcc $(CFLAG) $(TEST_SRC)
4

3 回答 3

3

我认为您使用的是 GCC 3.x?在 GCC 3.x 中,该-Wconversion标志记录如下:

如果原型导致的类型转换与没有原型时相同参数发生的类型转换不同,则发出警告。这包括定点到浮点的转换,反之亦然,以及改变定点参数的宽度或符号的转换,除非与默认提升相同。

此外,如果负整数常量表达式隐式转换为无符号类型,则会发出警告。例如, x = -1如果x未签名,则警告分配。但不要警告像(unsigned) -1.

[链接]

据我了解,此标志的最初目的是帮助检测从传统 C 迁移到 ANSI C 时的潜在问题,以及添加原型可能导致问题的情况。(老实说,在我看来,原型本身触发警告比使用函数更有意义,但我可能缺少一些用途。)

于 2012-11-25T05:09:37.913 回答
1

如果你只是输入一个十进制数字文字,你就声明了一个双精度数。glClearColor 使用浮点数。双精度数可能更长,并且可以包含更多信息,因此通过将其转换为浮点数,可能会丢失信息,这就是您收到警告的原因。您可以通过在数字后添加 af 来指定您想要一个浮点数。

于 2012-11-25T05:12:36.417 回答
1

使用 Apple 提供的 GCC,

i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)

这段代码:

extern void glClearColor(float, float, float, float);
enum { GL_FLAT = 0 };
extern void glShadeModel(int);
extern void init(void);

void init(void) 
{
   glClearColor(0.0F, 0.0F, 0.0F, 0.0F);
   glClearColor(0.0, 0.0, 0.0, 0.0);
   glClearColor((float)0.0, (float)0.0, (float)0.0, (float)0.0);
   glShadeModel(GL_FLAT);
}

编译时出现警告:

/usr/bin/gcc -O3 -g -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -Wconversion -c xx.c
xx.c: In function ‘init’:
xx.c:8: warning: passing argument 1 of ‘glClearColor’ as ‘float’ rather than ‘double’ due to prototype
xx.c:8: warning: passing argument 2 of ‘glClearColor’ as ‘float’ rather than ‘double’ due to prototype
xx.c:8: warning: passing argument 3 of ‘glClearColor’ as ‘float’ rather than ‘double’ due to prototype
xx.c:8: warning: passing argument 4 of ‘glClearColor’ as ‘float’ rather than ‘double’ due to prototype
xx.c:9: warning: passing argument 1 of ‘glClearColor’ as ‘float’ rather than ‘double’ due to prototype
xx.c:9: warning: passing argument 2 of ‘glClearColor’ as ‘float’ rather than ‘double’ due to prototype
xx.c:9: warning: passing argument 3 of ‘glClearColor’ as ‘float’ rather than ‘double’ due to prototype
xx.c:9: warning: passing argument 4 of ‘glClearColor’ as ‘float’ rather than ‘double’ due to prototype
xx.c:10: warning: passing argument 1 of ‘glClearColor’ as ‘float’ rather than ‘double’ due to prototype
xx.c:10: warning: passing argument 2 of ‘glClearColor’ as ‘float’ rather than ‘double’ due to prototype
xx.c:10: warning: passing argument 3 of ‘glClearColor’ as ‘float’ rather than ‘double’ due to prototype
xx.c:10: warning: passing argument 4 of ‘glClearColor’ as ‘float’ rather than ‘double’ due to prototype

使用 GCC 4.7.1 或不使用编译时-Wconversion,它可以干净地编译。

您要么必须编辑makefile以删除-Wconversion警告,要么接受并忽略警告。在两者之间,我会放弃-Wconversion

于 2012-11-25T05:16:38.803 回答