我正在尝试在我的C代码中使用open_memstream
函数。然而,我似乎无法编译它。最小工作示例如下:
#include <stdio.h>
int main(void) {
char *buf;
size_t sz;
FILE *stream = open_memstream(&buf, &sz);
putc('A', stream);
fclose(stream);
}
我还使用以下命令来编译它:
gcc -std=c99 -o test test.c
经过一些研究,我发现在包含stdio.h
之前需要定义一个宏。然而,下面的示例代码没有用。
#define __USE_POSIX
#define __USE_XOPEN
#include <stdio.h>
以下编译器警告被抛出;我认为第二个警告是由于第一个警告引起的。
test.c:7:17: warning: implicit declaration of function ‘open_memstream’ [-Wimplicit-function-declaration]
FILE *stream = open_memstream(&buf, &sz);
^
test.c:7:17: warning: initialization makes pointer from integer without a cast [-Wint-conversion]
man open_memstream
。 - n. m.