所以我试图将来自libcURL HTTP请求的响应存储到稍后要解析的C字符串中.响应代码完全用C语言编写,而其他所有内容都是用C++编写的,而且对于任何其他C++编译器,它应该可以正常工作.但是当我尝试编译时,即使我给'-xc'参数后跟文件名,我得到这些特定的响应.
g++ main.cpp -x c cJSON.c -x c respbuffer.c -lcurl -lm
./respbuffer.c:14:9: error: assigning to 'char *' from incompatible type 'void *'
s->ptr = malloc(s->len+1);
^ ~~~~~~~~~~~~~~~~
./respbuffer.c:23:9: error: assigning to 'char *' from incompatible type 'void *'
s->ptr = realloc(s->ptr, new_len+1);
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)
奇怪的是,只有在使用g ++编译时才会弹出此错误.如果我使用gcc,它运行正常,并且运行得足够顺畅.对于好奇,我在Xcode中编写了所有内容,并且我正在使用GCC 4.2.1进行编译.