Compiling a trivial python program in javascript using cython and emscripten on mac

I am trying to create javascript from python using cython and emscripten.

hello.py :

 print 'Hello world.' 

Then I compile this with cython

 >>> cython --embed hello.py -v 

This generates a hello.c file, which I compile with

 >>> gcc hello.c -I/usr/include/python2.7/ -lpython2.7 

This works for gcc or clang. When I execute ./a.out , I get the expected output

 >>> ./a.out >>> Hello world 

next I want to compile hello.c in javascript using emscripten

 >>> emcc hello.c -I/usr/include/python2.7/ -lpython2.7 

I get

 >>> WARNING emcc: -I or -L of an absolute path encountered. >>> If this is to a local system header/library, it may cause problems >>> (local system files make sense for compiling natively on your system, >>> but not necessarily to JavaScript) >>> clang: warning: argument unused during compilation: '-nostdinc++' 

It still generates a.out.js file which I am trying to run in node.js

 >>> node a.out.js 

I get a reference error

 >>> ReferenceError: _Py_SetProgramName is not defined 

I tried to modify the generated javscript a bit, but basically I think all _Py_ functions are undefined.

Does anyone have experience with this or any suggested fixes?

+4
source share
2 answers

You will need to compile the python -lpython2.7 built-in library in javacsript so that it is available for your javacsript program.

Fortunately, work on this has already been done in empythoned . This provides inline python compiled in Javascript.

You can use empythoned to provide missing _Py_SetProgramName

+6
source

To do this, I think you need all of Python compiled by emcc for JavaScript in order to have the appropriate libraries compiled into code that node.js can handle. Otherwise, the binary libraries remain intact. You cannot mix it.

In fact, emcc informs you of this with a warning if you read it carefully.

You need to learn how to cross-compile Python in javascript before compiling your own scripts. This has already been done because I saw it on repl.it.

+2
source

Source: https://habr.com/ru/post/1485790/


All Articles