Skip to content

Emscripten First Try On FreeBSD

Separate user for emscripten

To prevent nodejs based apps and libs spaming bunch of files into my system,
create a separately jailed user for nodejs based apps, so I can destroy all of the
spam files by a simple command:
rmuser

 

Install emscripten

# pkg install emscripten

 

Compile C++

$ em++ hello.cpp

 

system_libs:WARNING: retrieving port: binaryen from https://github.com/WebAssembly/binaryen/archive/version_84.zip
 

 

This is really embarrassed.. I do not like it.

$ ^C
$ su -
# pkg install binaryen
# exit
$ (( edit .emscripten and set BINARYEN PATH to /usr/local ))
$ em++ hello.cpp

 

Unknown option '--detect-features'
shared:ERROR: '/usr/local/bin/wasm-emscripten-finalize /tmp/emscripten_temp_v9itn5my/a.out.wasm -o /tmp/emscripten_temp_v9itn5my/a.out.wasm.o.wasm --detect-features --global-base=1024 --initial-stack-pointer=5243904' failed (1)
 

 

This is really embarrassed. I think emscripten and binaryen version mismatch.

 

Build and install emscripten and binaryen from FreeBSD ports.

$ su -
# pkg delete emscripten binaryen
# portsnap fetch update
# cd /usr/ports/devel/emscripten
# make install clean
# cd /usr/ports/devel/binaryen
# make install clean
$ exit

 

Compile C++

$ em++ hello.cpp
...
...
...
(( Compiled Success! ))

 

Configure nginx to use HTML DOC Root

 

Check c++17

// hello.cpp

#include <vector>
#include <array>
#include <iostream>

using namespace std::literals;

int main() {
        std::vector v1 {1, 3, 5, 7, 9};
        using cir = const int &;
        for (cir x: v1) std::cout << x << ' ';
        std::cout << '\n';

        auto [a, b, c] = std::array<float, 3>{1.1, 2.2, 3.3};
        auto [d, e, f] = std::make_tuple(2.3f, 3, "Hello emscripten!");

        for (float x: {a,b,c,d}) std::cout << x << " ";
        std::cout << e << " " << f << "\n";
}

 

$ em++ hello.cpp -s WASM=1 -stdlib=libc++ -std=c++17 -o index.html
$ node index.js
1 3 5 7 9
1.1 2.2 3.3 2.3 3 Hello emscripten!

use browser (firefox) to open index.html

 

Check c++ thread.

 

#include <future>
#include <iostream>

int main() {
        auto f = std::async(
                [] {
                        for (int i=0; i<9; i++) {
                                std::cout << i << '\n';
                        }
                }
        );
}
$ em++ hello.cpp -s WASM=1 -std=c++17 -lthr -pthread -o index.html
shared:ERROR: emcc: cannot find library "thr" (`-s ERROR_ON_MISSING_LIBRARIES=0` to disable this error)

 

$ em++ hello.cpp -s WASM=1 -std=c++17 -pthread -o index.html
cache:INFO: generating system library: libc-mt.a...
...
...
...
$ em++ hello.cpp -s WASM=1 -std=c++17 -pthread -o index.html
$ node index.js
Assertion failed: Pthreads do not work in non-browser environments yet
$ node index.worker.js

this.addEventListener('error', function(e) {
     ^

TypeError: this.addEventListener is not a function

 

*** So, c++ thread is compiled but not working on the web now ***

 

Check file io: fstream

 

#include <iostream>
#include <fstream>

int main() {
        const std::string fn = __FILE__;
        std::ifstream file{fn};
        if (file.is_open()) {
                std::string line;
                while (std::getline(file, line)) {
                        std::cout << line << '\n';
                }
                std::cout << std::endl;
                file.close();
        } else {
                std::cout << "Open File Error: " << fn << std::endl;
        }
}
$ em++ hello.cpp -std=c++17 -o index.html
$ (( browser or node index.js ))
Open File Error: hello.cpp
$ em++ hello.cpp -std=c++17 -o index.html --embed-file hello.cpp
$ (( browser or node index.js ))
(( Read File OK ))

 

 

Check chrono

 

#include <iostream>
#include <chrono>
#include <thread>

int main() {
   std::cout << "started.\n";
   std::this_thread::sleep_for(std::chrono::seconds(1));
   std::cout << "end\n";
}
$ em++ hello.cpp -s WASM=1 -std=c++17 -pthread -o index.html
$ node index.js
(( ERROR ))
$ node index.worker.js
(( ERROR ))

 

*** Only use chrono, not use thread ***
$ em++ hello.cpp -s WASM=1 -std=c++17 -o index.html
$ (( browser or node index.js ))
started.
end

(( chrono works ))

 

Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

No comments

Add Comment

Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.
Standard emoticons like :-) and ;-) are converted to images.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

Form options

Submitted comments will be subject to moderation before being displayed.

ijku     SpartanCoin