【发布时间】:2013-02-13 20:30:22
【问题描述】:
我正在使用 open_memstream 为嵌入式 linux 系统编写一些 C 代码,但我不明白为什么我会收到编译警告:assignment make pointer from integer without a cast
为了简单起见,我没有粘贴我所有的代码,而是通过here 中的小示例重现了这个问题:
#include <stdio.h>
#include <stdlib.h>
int
main (void)
{
FILE *stream;
char *buf;
size_t len;
off_t eob;
stream = open_memstream (&buf, &len);
if (stream == NULL)
/* handle error */ ;
fprintf (stream, "hello my world");
fflush (stream);
printf ("buf=%s, len=%zu\n", buf, len);
eob = ftello(stream);
fseeko (stream, 0, SEEK_SET);
fprintf (stream, "good-bye");
fseeko (stream, eob, SEEK_SET);
fclose (stream);
printf ("buf=%s, len=%zu\n", buf, len);
free (buf);
return 0;
}
代码有效,但编译器抱怨stream = open_memstream (&buf, &len);这一行
它在说什么整数?我们按照函数原型的要求传递了一个指向 size_t 的指针。
FILE *open_memstream(char **bufp, size_t *sizep);
这段代码有问题吗,还是我需要看一下我的编译器?我想以正确的方式摆脱这个警告。
更新:
使用 gcc 4.3.2、glibc 2.9
更新 2:
尝试了以下方法:
powerpc-860-linux-gnu-gcc -std=c99 -Wall -D_XOPEN_SOURCE=700 -c source.c
结果:
source.c: In function 'main':
source.c:12: warning: implicit declaration of function 'open_memstream'
source.c:12: warning: assignment makes pointer from integer without a cast
根据this,似乎_XOPEN_SOURCE=700 可用从glibc 2.10开始。
由于我使用的是 glibc 2.9,我还有哪些其他选择(除了升级 glibc)?
更新 3:
添加以下内容消除了警告:
extern FILE *open_memstream(char **bufp, size_t *sizep);
这个解决方案有什么问题吗?
更新 4:
这代替了外部:
powerpc-860-linux-gnu-gcc -std=c99 -Wall -D_GNU_SOURCE -c ops_cmds.c
所以根据manpage,如果glibc pre-2.10(在我的情况下)需要使用_GNU_SOURCE,如果2.10+需要使用_XOPEN_SOURCE=700
【问题讨论】: