3

本教程中,它显示了以下用于导出 C 函数的示例

./emcc tests/hello_function.cpp -o function.html -s EXPORTED_FUNCTIONS='["_int_sqrt"]' -s EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]'

我想做同样的事情,只是我像这样使用 CMake

cd bin
emcmake cmake ../src
emmake make

-s在 emmake中指定的规范方法是什么?我应该添加它CMakeLists.txt喜欢

set(EXPORTED_FUNCTIONS '["_int_sqrt"]')

或做类似的事情?

4

3 回答 3

2

这是现在最简单/最干净的方法:

target_link_options(target PRIVATE
        -sEXPORTED_FUNCTIONS=['_main','_foo','_bar'])

如果你有更多的 -s 设置(你可能会),你可以在这个函数调用中添加它们,或者你可以多次调用 target_link_options,两者都可以。看起来很随和,我不需要逃避任何事情。

于 2020-08-06T12:22:20.760 回答
2

到目前为止,我发现可以通过以下设置实现 CMake

# Here you can add -s flag during compiling object files
add_definitions("-s EXPORTED_RUNTIME_METHODS='[\"ccall\",\"cwrap\"]'")
add_definitions("-s EXTRA_EXPORTED_RUNTIME_METHODS='[\"ccall\",\"cwrap\"]'")
add_definitions("-s EXPORTED_FUNCTIONS='[\"_testInt\"]'")
# Here you can add -s flag during linking
set_target_properties(web_mealy_compiler PROPERTIES LINK_FLAGS "-s EXTRA_EXPORTED_RUNTIME_METHODS=['ccall','cwrap']")
# Set this if you want to to generate sample html file
set(CMAKE_EXECUTABLE_SUFFIX ".html")

然后你应该能够从 javascript 调用 C 函数,如下所示:

<script type="text/javascript">
     
    Module['onRuntimeInitialized'] = function() {
     
        console.log("wasm loaded ");
        
        console.log(Module.ccall); // make sure it's not undefined
        console.log(Module._testInt); // make sure it's not undefined
        console.log(Module._testInt()); // this should work
        console.log( Module.ccall('testInt', // name of C function
            'number', // return type
             [], // argument types
             []) // argument values
        );
    }
</script>

这是我对 C 函数的定义:

#include <emscripten.h>
EMSCRIPTEN_KEEPALIVE
int testInt(){
    return 69420;
}
于 2020-06-25T13:59:57.743 回答
0

我刚刚遇到了完全相同的问题,甚至在 Emscripten github 页面上启动了一个(请参阅此处)。

对我有用的是将所有 Emscripten 特定标志放入一个长字符串中,然后我添加了 target_compile_options 和 target_link_options。

  set(EMS
      "SHELL:-s EXPORTED_FUNCTIONS=['_main','_malloc','_int_sqrt'] -s EXTRA_EXPORTED_RUNTIME_METHODS=['ccall','cwrap']"
  )
  target_compile_options(EmscriptenExample PRIVATE ${EMS})
  target_link_options(EmscriptenExample PRIVATE ${EMS})

从函数列表中删除双引号和空格很重要,否则它将不起作用。至少 CMake 3.17.3 转义双引号对我不起作用。

/edit 为了完整起见:Emscripten 现在允许删除 -s 前缀和实际标志之间的空格。这使得实际使用 CMake 自己的 target_*_options 函数成为可能,例如:

target_link_options(target PRIVATE -sEXPORTED_FUNCTIONS=['_main','_foo','_bar'])
于 2020-08-06T06:59:57.023 回答