2

If I use the emscripten interface, EM_JS the symbols don't seem to be exported or anything.

If I use the following C code:

#include <emscripten.h>
#include <stdio.h>

//Function in Javascript Land
extern void writeout( int o );

EM_JS(void, console_logger, (const char* str), {
         console.log(UTF8ToString(str));
} );

void testcallback( int z )
{
        char ct[100];
        sprintf( ct, "Calling back: %d\n", z );
        console_logger(ct);
        writeout( z+10000 );
}

If I try compiling with the following line, like this, here are my issues.

/home/cnlohr/git/emsdk/upstream/emscripten/emcc -o add2.wasm add2.c -s EXPORTED_FUNCTIONS='["_add2","_testcallback","_console_logger"]' -s EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]' -s ERROR_ON_UNDEFINED_SYMBOLS=0
warning: undefined symbol: writeout (referenced by top-level compiled C/C++ code)
emcc: error: undefined exported function: "_console_logger" [-Wundefined] [-Werror]

I can try compiling without it, like this:

/home/cnlohr/git/emsdk/upstream/emscripten/emcc -o add2.wasm add2.c -s EXPORTED_FUNCTIONS='["_add2","_testcallback"]' -s EXPORTED_RUNTIME_METHODS='["ccall", "cwrap"]' -s ERROR_ON_UNDEFINED_SYMBOLS=0
warning: undefined symbol: writeout (referenced by top-level compiled C/C++ code)
cat add2.wasm | base64 | ./strencode  > add2.wasm.b64

When I load it, I get

Uncaught (in promise) LinkError: import object field 'console_logger' is not a Function

How do I get emcc to actually make the function I generated using EM_JS?

Just FYI, this is the version of emcc I'm using:

emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 2.0.0
clang version 12.0.0 (/b/s/w/ir/cache/git/chromium.googlesource.com-external-github.com-llvm-llvm--project a3036b386383f1c1e9d32c2c8dba995087959da3)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /home/cnlohr/git/emsdk/upstream/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Candidate multilib: .;@m64
Selected multilib: .;@m64
shared:INFO: (Emscripten: Running sanity checks)
Charles Lohr
  • 695
  • 1
  • 8
  • 23

1 Answers1

2

So, the answer to this is that if you target a .js file, these functions (among others) are put in that separate js file. You can examine that and copy things out willy-nilly if you don't want to use the whole file.

Charles Lohr
  • 695
  • 1
  • 8
  • 23