Emscripten 安装和使用

时间:2023-03-08 17:57:57
Emscripten 安装和使用

OS: Windows 10 x64

I. install

0. pre install

Python2.7
Node js
Java

1. down

git clone https://github.com/emscripten-core/emsdk.git

注意:最新版本信息请查看“emscripten-tags.txt”

(1) lkgr.json
https://storage.googleapis.com/wasm-llvm/builds/linux/lkgr.json
to "emsdk\upstream"

(2) llvm
https://s3.amazonaws.com/mozilla-games/emscripten/packages/llvm/tag/win_64bit/emscripten-llvm-e1.38.30.zip
to "ëmsdk\zips"

2. setup

(1) custom install

emsdk install clang-e1.

(2) activate

emsdk activate --vs2013 clang-e1.

(3) test

emsdk_env

II. usage

1. compile BC file

emcc tests/hello_world.c

2. output

(1) normal

emcc tests/hello_world.c -o hello.html

(2) sdl

emcc tests/hello_world_sdl.cpp -o hello.html

(3) file

emcc tests/hello_world_file.cpp -o hello.html --preload-file tests/hello_world_file.txt

3. run

python -m SimpleHTTPServer 

http://localhost:8080/hello.html

Src:

1. hello_world.c

/*
 * Copyright 2011 The Emscripten Authors.  All rights reserved.
 * Emscripten is available under two separate licenses, the MIT license and the
 * University of Illinois/NCSA Open Source License.  Both these licenses can be
 * found in the LICENSE file.
 */

#include <stdio.h>

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

2. hello_world_sdl.cpp

// Copyright 2011 The Emscripten Authors.  All rights reserved.
// Emscripten is available under two separate licenses, the MIT license and the
// University of Illinois/NCSA Open Source License.  Both these licenses can be
// found in the LICENSE file.

#include <stdio.h>
#include <SDL/SDL.h>

#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif

extern "C" int main(int argc, char** argv) {
  printf("hello, world!\n");

  SDL_Init(SDL_INIT_VIDEO);
  SDL_Surface *screen = SDL_SetVideoMode(, , , SDL_SWSURFACE);

#ifdef TEST_SDL_LOCK_OPTS
  EM_ASM("SDL.defaults.copyOnLock = false; SDL.defaults.discardOnLock = true; SDL.defaults.opaqueFrontBuffer = false;");
#endif

  if (SDL_MUSTLOCK(screen)) SDL_LockSurface(screen);
  ; i < ; i++) {
    ; j < ; j++) {
#ifdef TEST_SDL_LOCK_OPTS
      // Alpha behaves like in the browser, so write proper opaque pixels.
      ;
#else
      // To emulate native behavior with blitting to screen, alpha component is ignored. Test that it is so by outputting
      // data (and testing that it does get discarded)
      ;
#endif
      *((Uint32*)screen->pixels + i *  + j) = SDL_MapRGBA(screen->format, i, j, -i, alpha);
    }
  }
  if (SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
  SDL_Flip(screen); 

  printf("you should see a smoothly-colored square - no sharp lines but the square borders!\n");
  printf("and here is some text that should be HTML-friendly: amp: |&| double-quote: |\"| quote: |'| less-than, greater-than, html-like tags: |<cheez></cheez>|\nanother line.\n");

  SDL_Quit();

  ;
}

3. hello_world_file.cpp

// Copyright 2012 The Emscripten Authors.  All rights reserved.
// Emscripten is available under two separate licenses, the MIT license and the
// University of Illinois/NCSA Open Source License.  Both these licenses can be
// found in the LICENSE file.

#include <stdio.h>
int main() {
  FILE *file = fopen("tests/hello_world_file.txt", "rb");
  if (!file) {
    printf("cannot open file\n");
    ;
  }
  while (!feof(file)) {
    char c = fgetc(file);
    if (c != EOF) {
      putchar(c);
    }
  }
  fclose (file);
  ;
}

hello_world_file.txt

==
This data has been read from a file.
The file is readable as if it were at the same location in the filesystem, including directories, as in the local filesystem where you compiled the source.
==

Reference:

1. Emscripten SDK

2. emscripten tutorial