Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Emscripten获取始终返回0_C++_Xmlhttprequest_Emscripten - Fatal编程技术网

C++ Emscripten获取始终返回0

C++ Emscripten获取始终返回0,c++,xmlhttprequest,emscripten,C++,Xmlhttprequest,Emscripten,我遵循Emscripten附带的同步获取示例,如下所示 void main() { emscripten_fetch_attr_t attr; emscripten_fetch_attr_init(&attr); strcpy(attr.requestMethod, "GET"); attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_SYNCHRONOUS

我遵循Emscripten附带的同步获取示例,如下所示

void main()
{
      emscripten_fetch_attr_t attr;
      emscripten_fetch_attr_init(&attr);
      strcpy(attr.requestMethod, "GET");
      attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_SYNCHRONOUS;
      emscripten_fetch_t *fetch = emscripten_fetch(&attr, "https://ichef.bbci.co.uk/news/660/cpsprodpb/E9DF/production/_96317895_gettyimages-164067218.jpg");
      printf("Fetch finished with status %d\n", fetch->status);
}
它总是从获取状态返回0

我用

FLAGS            += -std=c++17 -stdlib=libc++ -O3
FLAGS            += -s WASM=1  -s USE_WEBGL2=1 -s FULL_ES3=1 
FLAGS            += -s ALLOW_MEMORY_GROWTH=1 
FLAGS            += -o hello.html 
FLAGS            += -s "EXTRA_EXPORTED_RUNTIME_METHODS=['ccall', 'cwrap']"    
FLAGS            += --no-heap-copy
FLAGS            +=  -s FETCH=1
但是,当我使用async进行测试并读取OnSuccess函数中的数据时。数据打印正确

static
void ondownload_success(emscripten_fetch_t *fetch)
{
    printf("[ download ][ OK ]    %llu bytes  [ URL ]: %s\n", fetch->numBytes, fetch->url);

    printf("%c %c %c", fetch->data[0], fetch->data[3], fetch->data[2] );
    emscripten_fetch_close(fetch); // Free data associated with the fetch.
}
我的获取同步代码有什么问题?所有内容都与“example\u synchronous\u fetch.cpp”示例完全相同


我在Windows10上运行。Emscripten 1.38.29。使用Microsoft Edge在没有服务器的情况下直接浏览文件(双击hello.html)

同步
fetch
有一些额外的限制,并且您的生成标志似乎没有启用同步
fetch

同步Emscripten获取操作受到许多限制 限制,取决于哪个Emscripten生成模式(链接器标志) 用于:

无标志:只有异步获取操作可用

–代理到工作者:对于只执行XHR但不与IndexedDB交互的回迁,允许同步回迁操作

-s usepthreads=1:同步获取操作在PTHREADS上可用,但在主线程上不可用

–proxy to worker+-s USE_PTHREADS=1:同步获取操作在主线程和pthread上都可用


这个答案似乎不适用于wasm编译,至少不适用于emscripten v2.0.20。由于同步原因,WASM=1时不会运行相对代码