title: Emscripten: how to build a C++ project with headers
body: <p>I want to convert this C++ project (Facebook <a href="https://github.com/facebookresearch/fastText" rel="nofollow noreferrer">FastText</a>)</p>
  
  <pre><code>├── args.cc
  ├── args.h
  ├── dictionary.cc
  ├── dictionary.h
  ├── fasttext.cc
  ├── fasttext.h
  ├── main.cc
  ├── matrix.cc
  ├── matrix.h
  ├── model.cc
  ├── model.h
  ├── productquantizer.cc
  ├── productquantizer.h
  ├── qmatrix.cc
  ├── qmatrix.h
  ├── real.h
  ├── utils.cc
  ├── utils.h
  ├── vector.cc
  └── vector.h
  </code></pre>
  
  <p>I want to run in <code>node.js</code>. I have first built it as a <a href="https://kripken.github.io/emscripten-site/docs/compiling/Building-Projects.html" rel="nofollow noreferrer">project</a> with <code>emmake make</code> and I have got the linked LLVM object files:</p>
  
  <pre><code>-rw-r--r--   1 loretoparisi  staff   27536 30 Lug 10:20 args.o
  -rw-r--r--   1 loretoparisi  staff   78632 30 Lug 10:20 dictionary.o
  -rw-r--r--   1 loretoparisi  staff   23864 30 Lug 10:20 productquantizer.o
  -rw-r--r--   1 loretoparisi  staff   12120 30 Lug 10:20 matrix.o
  -rw-r--r--   1 loretoparisi  staff    9132 30 Lug 10:20 qmatrix.o
  -rw-r--r--   1 loretoparisi  staff   10532 30 Lug 10:20 vector.o
  -rw-r--r--   1 loretoparisi  staff   30036 30 Lug 10:20 model.o
  -rw-r--r--   1 loretoparisi  staff    1616 30 Lug 10:20 utils.o
  -rw-r--r--   1 loretoparisi  staff  118404 30 Lug 10:20 fasttext.o
  -rwxr-xr-x   1 loretoparisi  staff  270940 30 Lug 10:20 fasttext
  </code></pre>
  
  <p>but clearly this was not bit code! I have then compiled with <code>emmake make VERBOSE=1</code> and it turns out it was not using <code>em++</code> compiler, so I have tried <code>cmake</code>:</p>
  
  <pre><code>emconfigure cmake
  emmake make VERBOSE=1
  </code></pre>
  
  <p>At this point I can see <code>em++</code> running</p>
  
  <pre><code>/usr/local/Cellar/cmake/3.11.1/bin/cmake -E cmake_link_script CMakeFiles/fasttext-shared.dir/link.txt --verbose=1
  /Users/loretoparisi/Documents/MyProjects/emsdk/emscripten/1.38.10/em++ -fPIC  -pthread -std=c++11 -funroll-loops -O3 -march=native  -shared -Wl,-soname,libfasttext.so -o libfasttext.so @CMakeFiles/fasttext-shared.dir/objects1.rsp 
  </code></pre>
  
  <p>so I get then the following output:</p>
  
  <pre><code>-rw-r--r--   1 loretoparisi  staff  870544 30 Lug 10:40 libfasttext.a
  -rw-r--r--   1 loretoparisi  staff  306646 30 Lug 10:40 fasttext.wasm
  -rw-r--r--   1 loretoparisi  wheel  114099 30 Lug 10:40 fasttext.js
  -rw-r--r--   1 loretoparisi  staff  870504 30 Lug 10:40 libfasttext_pic.a
  -rw-r--r--   1 loretoparisi  staff  663728 30 Lug 10:40 libfasttext.so
  </code></pre>
  
  <p>This turns out to be a valid javascript file, but it causes an exception when running it</p>
  
  <pre><code>$ node fasttext.js predict-prob /root/lang_id.bin - 2
  exception thrown: 5280232 - Exception catching is disabled, this exception cannot be caught. Compile with -s DISABLE_EXCEPTION_CATCHING=0 or DISABLE_EXCEPTION_CATCHING=2 to catch.
  </code></pre>
  
  <p>while it should get from <code>stdin</code> like</p>
  
  <pre><code>./fasttext predict-prob /root/lang_id.bin - 2
  ja das is seher gut heute
  __label__deu 0.999483 __label__bar 4.1711e-05
  </code></pre>
  
  <p>May this error due to <code>stdin</code> in node.js? The command like seems to be fine, since it accepts and parses the parameters in the right way:</p>
  
  <pre><code>$ node fasttext.js predict-prob
  usage: fasttext predict[-prob] &lt;model&gt; &lt;test-data&gt; [&lt;k&gt;] [&lt;th&gt;]
  
    &lt;model&gt;      model filename
    &lt;test-data&gt;  test data filename (if -, read from stdin)
    &lt;k&gt;          (optional; 1 by default) predict top k labels
    &lt;th&gt;         (optional; 0.0 by default) probability threshold
  </code></pre>
  
  <p>I have then modularized it like <code>emcc -s MODULARIZE=1 -s LEGACY_VM_SUPPORT=1 -s WASM=0 -O1 libfasttext.so -o libfasttext.js</code>, and called within a node module like:</p>
  
  <pre><code>let FastTextModule = require('./fasttext-module.js');
  let Module = null;
  if (Module) {
      return;
  }
  Module = {
      noExitRuntime: true,
      noInitialRun: false,
      preInit: [],
      preRun: [],
      postRun: [function () {
          console.log(`Loaded Javascript Module OK`);
      }],
      locateFile: "./",
      arguments: ['predict-prob', '/root/lang_id.bin', '-', 2]
  };
  Module['locateFile'] = function (path, prefix) {
      return prefix + path;
  }
  FastTextModule(Module);
  </code></pre>
  
  <p>But I'm getting the same error: <code>exception thrown: 5278216 - Exception catching is disabled, this exception cannot be caught. Compile with -s DISABLE_EXCEPTION_CATCHING=0 or DISABLE_EXCEPTION_CATCHING=2 to catch.</code></p>
  
  <p>In the same way I can run it setting <code>noInitialRun:true</code> and then doing </p>
  
  <pre><code>Module.callMain(['predict-prob', '/root/ft_lang_model.bin', 'test.csv', '2']);
  </code></pre>
  
  <p>but having that error anyways.</p>