Emscripten使用文件系统FS

4
我想知道如何在Emscripten中使用FS。我认为我已经做了维基中提到的所有事情,但仍然出现Uncaught ReferenceError: FS is not defined。如果我在生成的*.js文件中搜索文字FS,则没有出现,我认为应该有的。
这是目前我所拥有的代码。

InfoMedia.cpp

#include <math.h>  //testing include
extern "C" {

// testing function
int int_sqrt(int x) {
  return sqrt(x);
}

}// extern c 

使用以下命令编译:

emcc -s EXPORTED_FUNCTIONS="['_int_sqrt']" InfoMedia.cpp -o InfoMedia.js

结果请查看InfoMedia.js@pastebin

init_fs.js

var Module = {
  'print': function(text){ 
    console.log(text) 
  },
  'preRun' : function(){
    console.log('prerun');
    FS.createPath('/', 'home/user1', true, true);
  },
  'noInitialRun': true,
};

excample.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>InfoMediaJS-Example</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">    </script>
    <script type="text/javascript" src="init_fs.js"></script>
    <script type="text/javascript" src="InfoMedia.js"></script>
    <script type="text/javascript">
        run();
    </script>
</head>
<body></body>
</html>

在Chrome中运行后,会调用preRun并出现错误。
prerun                                     init_fs.js:6
Uncaught ReferenceError: FS is not defined init_fs.js:7

此外,如果我尝试在编译时嵌入文件,使用以下命令:emcc -s EXPORTED_FUNCTIONS="['_int_sqrt']" --embed-file gizmo.webm InfoMedia.cpp -o InfoMedia.js,我会收到这个错误:Uncaught TypeError: Object #<Object> has no method 'FS_createDataFile'。它在我的生成的js文件中的第1460行http://pastebin.com/Mu1qfG25Module['FS_createDataFile']('/', 'gizmo.webm', [26, 69, 223,....]), true, true);。FS从未插入到生成的js文件中。所以无论我如何调用FS,都没有用。是否有任何编译器选项需要添加以插入该库功能?
1个回答

2

非常简单。只需要使用使用文件系统的函数,emscripten将自动包含它。正如您在输出文件中看到的那样,尽管emscripten提供了比生成的输出文件中显示的更多的c库,但其中没有不必要的库函数。

为了使事情更清晰,只需修改您的InfoMedia.cpp:

#include <math.h>  //testing include
#include <stdio.h>
extern "C" {

// testing function
int int_sqrt(int x) {
  printf ("Decimals: %d %ld\n", 1977, 650000L);  // use FS lib functionality
  return sqrt(x);
}

}// extern c

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接