How do I set up a basic C++/C compile, and then link script for emscripten builds

邮差的信 提交于 2021-02-08 07:32:35

问题


This test if in preparation for writing a more fully fledged build module. But I need to sus out the basics first. The desire to: - build all the "object files" output to an output folder - link them into a distributable "wasm" binary and format an html page and output them to a "dist" folder.

I looked at the manual in:

https://kripken.github.io/emscripten-site/docs/tools_reference/emcc.html

It's not as intuitive or explicitly spelled out as one would want. Below is my current simple test build.

#!/bin/bash

# set emscripten toolchain paths in this shell
source "${HOME}/emsdk/emsdk_env.sh" --build=Release   

BuildDir="./build"          # root of output dir for built files
SrcDir="./src"              # a source code directory
ObjDir="${BuildDir}/obj"    # where intermediate "object" files are output to.
IncludeDir="./include"      # an include directory

# start clean for this test
rm -fr "${BuildDir}"
mkdir -p "${ObjDir}"  # also re-creates BuildDir


# compile source code out to ObjDir

emcc --default-obj-ext .bc -o "${ObjDir}/" -I "${IncludeDir}" \
    "${SrcDir}/hello.cpp" "${SrcDir}/TestClass.cpp"`

Running the above gives me errors but works if there is only one source file.

Is a directory
Traceback (most recent call last):
      File "/home/peterk/emsdk/emscripten/incoming/emcc.py", line 3107, in <module>
...
status 1
    peterk@5a4a702ca3b5:~/didi-wasmtest/test$

The below works but puts all the output files in the src/ folder and also assigns them the .o suffix and not the .bc suffix: It also outputs a .out.js and a .out.wasm file in the directory the script is run from. I would like to suppress that until a final "link" phase where the results of several compiles would be linked together in a separate step.

emcc --default-obj-ext .bc -I "${IncludeDir}" \
    "${SrcDir}/hello.cpp" "${SrcDir}/TestClass.cpp"`

回答1:


Ok - this works which is good as a makefile will compile one at a time anyway.

#!/bin/bash

source "${HOME}/emsdk/emsdk_env.sh" --build=Release   # set emscripten toolchain paths in this shell

BuildDir="./build"          # root of output dir for built files
SrcDir="./src"              # a source code directory
ObjDir="${BuildDir}/obj"    # where intermediate "object" files are output to.
IncludeDir="./include"      # an include directory
DistDir="./build/dist"      # distribution "binary" output dir

# start clean for this test
rm -fr "${BuildDir}"

# compile source code out to ObjDir
mkdir -p "${ObjDir}"

objFiles=""

for srcFile in "${SrcDir}"/*.cpp; do
    objFile="${ObjDir}/$(basename "$srcFile").bc"
    emcc -o "${objFile}" -I "${IncludeDir}" "${srcFile}"
    objFiles+="${objFile} " # save list of objFiles for link.
done

# link object files into binary runtime in DistDir
mkdir -p "${DistDir}"

emcc -s WASM=1 ${objFiles} -o "${DistDir}/hello.html"

# expose to browser to test
cd "${DistDir}"
python -c 'import SimpleHTTPServer; SimpleHTTPServer.test()'


来源:https://stackoverflow.com/questions/53133172/how-do-i-set-up-a-basic-c-c-compile-and-then-link-script-for-emscripten-build

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!