Emscripten

speciaLLunch·2023년 7월 27일

WebAssembly with React

목록 보기
2/2

emscripten

개념

C,C++ 이나 Rust 등의 LLVM(Low Level Virtual Machine, 컴파일러 기반 구조) 사용 언어들을 브라우저나 node.js 환경에서 사용할 수 있도록 컴파일해주는 툴

설치 방법은 공식 홈페이지 참고
https://emscripten.org/docs/getting_started/downloads.html

변환 방법

// hello_world.c 예제
#include <stdio.h>

int main() {
  printf("hello, world!\n");
  return 0;
}

// side_module.c 예제
int Increment(int value) {
    return (value + 1);
}

js 템플릿 : emcc hello_world.c -o hello_world.js
wasm 모듈만 : emcc side_module.c -s SIDE_MODULE=2 -O1 -s EXPORTED_FUNCTIONS=['_Increment'] -o side_module.wasm

※ 옵션
main_module/side_module : 시스템 라이브러리 유무 차이. 오직 하나의 싱글톤 메인 모듈JS 환경을 포함하고 나머지 사이드 모듈은 순수한 webAssembly 모듈.
-01 : 최적화 플래그
s EXPORTED_FUNCTIONS : export 할 함수 지정. 함수앞에 자동으로 underscore 포함되어 표기 필요

브라우저 확인

emrun으로 html 파일 확인

emrun side_module.html


https://emscripten.org/
https://velog.io/@errorcode4319/WASM-001.-%EC%B2%98%EC%9D%8C%ED%95%B4%EB%B3%B4%EB%8A%94-%EC%9B%B9%EC%96%B4%EC%85%88%EB%B8%94%EB%A6%AC

profile
web front

1개의 댓글

comment-user-thumbnail
2023년 7월 27일

공감하며 읽었습니다. 좋은 글 감사드립니다.

답글 달기