为什么当我转换为float时gcc总是会出错?

时间:2012-11-25 04:56:52

标签: c opengl gcc

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

glClearColor的参数是float。但是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 64位,以下是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)

3 个答案:

答案 0 :(得分:2)

我认为你正在使用GCC 3.x?在GCC 3.x中,-Wconversion标志记录如下:

  

如果原型导致类型转换与其不同,则发出警告   在没有原型的情况下会发生同样的争论。这个   包括固定点到浮动的转换,反之亦然   转换更改固定点参数的宽度或符号   除非与默认促销相同。

     

另外,如果隐式显示负整数常量表达式,则发出警告   转换为无符号类型。例如,警告有关任务   如果x = -1未签名,则x。但是不要警告明确的   像(unsigned) -1这样的演员。

[link]

据我所知,此标志的最初目的是帮助检测从传统C迁移到ANSI C时的潜在问题,以及添加原型可能导致问题的情况。 (老实说,在我看来,原型本身触发警告会更有意义,而不是使用的功能,但可能会有一些用途我和#39;我失踪了。)

答案 1 :(得分:1)

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

答案 2 :(得分: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