emscripten-core / emscripten

Emscripten: An LLVM-to-WebAssembly Compiler
Other
25.83k stars 3.31k forks source link

why web assembly can invoke c library function?? #15253

Open 59436830 opened 3 years ago

59436830 commented 3 years ago

i have read the tutorial in official documentation, but cannot figure it out why ws supports c library like glibc stdlibc etc. From the doc I know that

  1. C code can be converted to a wasm file which runs in a browser.
  2. the wasm 's content is some cross-platform intermediate code. now I have a main.c file like below

    include

    int main () { ... printf("helloworld");
    .. } my question is why printf works?? as I understand the c library binary code which including printf.o is not in my wasm file.

curiousdannii commented 3 years ago

as I understand the c library binary code which including printf.o is not in my wasm file

No, all the C/C++ libraries you use are linked statically and compiled into the wasm file.

59436830 commented 3 years ago

as I understand the c library binary code which including printf.o is not in my wasm file

No, all the C/C++ libraries you use are linked statically and compiled into the wasm file.

sorry i cannot understand what you mean completely. maybe I got some wrong information from internet. It's easy to understand how to convert a c source code to WASM, but then c/c++ library are binary code not source code on my machine. you mean the emcc tool chain decompile the printf.o into a intermediate code and link it with my main.c ???

curiousdannii commented 3 years ago

No. Emscripten includes the full source code for libc, libc++ etc. These are compiled for wasm and your computer's normal versions of these libraries are never considered. If you want to use a library that's not included in Emscripten then you have to compile it with emcc yourself.

59436830 commented 3 years ago

No. Emscripten includes the full source code for libc, libc++ etc. These are compiled for wasm and your computer's normal versions of these libraries are never considered. If you want to use a library that's not included in Emscripten then you have to compile it with emcc yourself.

thank you very much, buddy. Now I understand what happens in the compilation. A week ago, I planned to convert a native project to a web assembly project, because i want that project become a web browser plugin that works for our web application. in that native project( coded by c) , I use a lot of system call like fork localtime etc. I was wondering that if I can convert them to WASM from source code. Based on your response, seems that I cannot use WA to meet my requirement. because i used too many system APIs and third-party dynamic libraries (.so and .a) in that project. How sad I have been working for this research for a whole week.

jeffRTC commented 3 years ago

@59436830

You can use pthreads (which will use webworkers behind the scene for you) and of course Date Time in WASM.. Forking is not possible. Most of code will work except OS specific like Windows.