Emscripten 和 jquery 点击回调,问题?

Emscripten and jquery click callback, issue?

我在这里放了完整的例子,我不知道这是否正确!? 完成:

emcc jquery001.cpp -o jquery001.js -s EXPORTED_FUNCTIONS="['_x_click','_webmain']"

似乎一切都很好,程序运行良好但是... printf 总是显示不是最后一个 printf,而是 prev

初始输出:

pre-main prep time: 11 ms
jquery001.js:143 
jquery001.js:143  enter webmain
jquery001.js:143  webmain <>

exit web main is missig : printf ( "\n exit webmain");

然后一个 'click' 在元素示例上出现 :

exit webmain
jquery001.js:143  enter x_click
jquery001.js:143  x_click event <x1>

printf 退出 web main ...但不是 printf("\n exit x_click");

怎么了?

[jquery001.html]

<!DOCTYPE html>
<html>
<head>
<title>emcc & jquery</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<label id="x1" class="x" >emscripten1</label>
<label id="x2" class="x" >emscripten2</label>
</body>
<script src="jquery001.js"></script>
<script>
Module.ccall('webmain', 'number', ['string'],['']);
</script>
</html>

[jquery001.cpp]

#include <stdio.h>
#include <stdlib.h>
#include <emscripten.h>
#include <string.h>

#include <string>

extern "C"
{
    int x_click(  char *s ) 
    {
      printf ( "\n enter x_click");
      printf ( "\n x_click event <%s>",s );
      printf ( "\n exit x_click");
      return 0 ;
    }


    int webmain( char *s ) 
    {
        printf ( "\n enter webmain");       
        printf ( "\n webmain <%s>",s);

        int x = EM_ASM_INT({

         $('.x').click(function(e)
         {
             Module.ccall('x_click', 'number', ['string'],[e.target.id]);

         });


          return 0;
        }, NULL);   

        printf ( "\n exit webmain");    

        return 0 ;
    }

}


int main ( void )
{

    return 0 ;
}

Emscripten 通过缓冲传递给 printf 的参数来处理输出,直到它到达换行符,此时该字符串被传递给 Module.print() 方法,该方法处理显示输出。这样做的结果是,如果传递给 printf 的字符串不以换行符结尾,则不会打印它。

这类似于 stdout 在 C 中的缓冲方式,但与 Emscripten(至少我测试过的版本 1.36)的不同之处在于调用 fflush(NULL) 不会刷新缓冲区, stderr 的缓冲方式与 stdout.

相同

纠正您的问题很简单,您只需在最终字符串的末尾添加一个换行符来刷新缓冲区,即:

printf("\n exit x_click\n");

printf("\n exit webmain\n");