简体   繁体   English

open_memstream警告“来自没有强制转换的整数的指针”

[英]open_memstream warning “pointer from integer without a cast”

I'm writing some C code for an embedded linux system using an open_memstream and I don't understand why I am getting a compile warning: assignment makes pointer from integer without a cast 我正在使用open_memstream为嵌入式linux系统编写一些C代码, open_memstream我不明白为什么会收到编译警告: 赋值使整数生成指针而没有 open_memstream

To make things simple, rather than pasting all my code I reproduced the problem with the small example from 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;
}

The code works, but the compiler complains about the line stream = open_memstream (&buf, &len); 该代码有效,但是编译器抱怨该行stream = open_memstream (&buf, &len);

What integer is it talking about? 它在说什么整数? We're passing in a pointer to a size_t as required by the function prototype. 我们根据函数原型的要求传递了一个指向size_t的指针。

FILE *open_memstream(char **bufp, size_t *sizep);

Is there a problem with this code, or do I need to take a look at my compiler? 这段代码是否有问题,还是需要看一下我的编译器? I want to get rid of this warning the right way. 我想以正确的方式摆脱这种警告。


UPDATE: 更新:

Using gcc 4.3.2, glibc 2.9 使用gcc 4.3.2,glibc 2.9


UPDATE 2: 更新2:

Tried the following: 尝试了以下内容:

powerpc-860-linux-gnu-gcc -std=c99 -Wall -D_XOPEN_SOURCE=700 -c source.c

Result: 结果:

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

According to this , it seems that _XOPEN_SOURCE=700 is available since glibc 2.10 . 根据这个 ,似乎_XOPEN_SOURCE = 700是因为glibc的2.10可用。

Since I'm using glibc 2.9, what other alternatives do I have (other than upgrading glibc)? 由于我使用的是glibc 2.9,除了升级glibc之外,还有哪些其他选择?


UPDATE 3: 更新3:

Adding the following got rid of the warning: 添加以下内容消除了警告:

extern FILE *open_memstream(char **bufp, size_t *sizep);

Is there anything wrong with this solution? 这个解决方案有什么问题吗?


UPDATE 4: 更新4:

This worked instead of the extern: 这工作,而不是外部:

powerpc-860-linux-gnu-gcc -std=c99 -Wall -D_GNU_SOURCE -c ops_cmds.c

So according to the manpage , need to use _GNU_SOURCE if glibc pre-2.10 (in my case) and _XOPEN_SOURCE=700 if 2.10+ 因此,根据手册 ,如果glibc 2.10之前版本(以我_GNU_SOURCE则需要使用_GNU_SOURCE如果_GNU_SOURCE则需要使用_XOPEN_SOURCE=700

Define: 限定:

#define _POSIX_C_SOURCE 200809L

or 要么

#define _XOPEN_SOURCE 700

in your source code before including stdio.h . 在包含stdio.h之前先在源代码中使用。 Or with gcc you can define and pass the macro value to the source file with -D option: 或者使用gcc您可以使用-D选项定义宏值并将其传递到源文件:

gcc -std=c99 -Wall -D_XOPEN_SOURCE=700 -c source.c 

open_memstream is a POSIX function and its declaration is not visible in your program without this define. open_memstream是POSIX函数,如果没有此定义,则它的声明在您的程序中不可见。

The compiler is complaining about the return value of open_memstream , not about the arguments you pass in. 编译器抱怨的是open_memstream返回值 ,而不是您传入的参数。

Your open_memstream is not declared, ie the compiler does not see the prototype. 您的open_memstream没有声明,即编译器看不到原型。 So the compiler (apparently pre-C99) assumes that it returns an int . 因此,编译器(显然是C99之前的版本)假定它返回一个int You are forcing that int into stream pointer, which is what triggers the warning about "making pointer form integer". 您正在将int强制插入stream指针,这将触发有关“使指针形成整数”的警告。

Make sure open_memstream is declared before you attempt to use it. 在尝试使用open_memstream之前,请确保已open_memstream进行了声明。 The prototype is supposed to reside in stdio.h , but it is only available in POSIX.1-2008. 该原型应位于stdio.h ,但仅在POSIX.1-2008中可用。 You have to enable it explicitly (see other answers). 您必须显式启用它(请参阅其他答案)。

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

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