Emscripten using Filesystem FS

2019-07-19 04:53发布

问题:

I am wondering how to use the FS in Emscripten. I think i have done all the things mentioned in the wiki, but i still get Uncaught ReferenceError: FS is not defined. If i search the resulting *.js file for the literal FS there is no occurrence, i thought there should be.

here is the code i have so far.

InfoMedia.cpp

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

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

}// extern c 

compiled with

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

result at 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>

after running this in chrome preRun is invoked and i get the error.

prerun                                     init_fs.js:6
Uncaught ReferenceError: FS is not defined init_fs.js:7

In addition, if i try to embed a file at compile time with

emcc -s EXPORTED_FUNCTIONS="['_int_sqrt']" --embed-file gizmo.webm InfoMedia.cpp -o InfoMedia.js

i get this error Uncaught TypeError: Object #<Object> has no method 'FS_createDataFile'

it is inside my generated js file at this line http://pastebin.com/Mu1qfG25 @ line 1460 Module['FS_createDataFile']('/', 'gizmo.webm', [26, 69, 223,....]), true, true);

FS is never inserted into the resulting js file. So it doesn't matter how i call that FS stuff. Is there any Compiler option i need to add to insert that Library funktionality?

回答1:

Its dead simple. Just use functions that use the FS and emscripten will automatically include it. As you may see in the output file, there are no unnecessary library functions included in it despite the fact that emscripten offers a lot more c libs than you see in the generated output file.

to make thinks clear, just alter your InfoMedia.cpp to:

#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