如何设置基本的C ++ / C编译,然后链接脚本以进行脚本构建

时间:2018-11-03 16:16:46

标签: emscripten webassembly

此测试是否为编写更全面的构建模块做准备。但是我需要先掌握基础知识。渴望:    -将所有“目标文件”输出构建到输出文件夹    -将它们链接到可分发的“ wasm”二进制文件中,并格式化html页面      并将它们输出到“ dist”文件夹。

我查看了以下手册:

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

它不像人们想要的那样直观或明确地拼写出来。 下面是我当前的简单测试版本。

#!/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"`

运行上述命令会给我带来错误,但如果只有一个源文件,则可以使用。

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$

下面的方法有效,但是将所有输出文件放在src /文件夹中,并为它们分配.o后缀而不是.bc后缀:它还在目录中输出.out.js和.out.wasm文件脚本从中运行。我想抑制一下,直到最后的“链接”阶段将几个编译结果在单独的步骤中链接在一起。

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

1 个答案:

答案 0 :(得分:0)

好-可以正常运行,因为makefile可以一次编译一个。

#!/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()'