Loading
柚木鉉の空間
0%
INITIALIZING
首页 文章 标签 归档 友链 推广 搜索
文档编号 // 5AB0D9 在线

Emscripten(2)使用CMAKE构建Emscripten

2019-07-09
更新: 2026-04-25
2466 字符
这篇文章写于 2019,已经超过 7 年了。内容可能已经过时。

CMake是一个跨平台的安装(编译)工具,可以用简单的语句来描述所有平台的安装(编译过程)。他能够输出各种各样的makefile或者project文件,能测试编译器所支持的C++特性,类似UNIX下的automake。只是 CMake 的组态档取名为 CMakeLists.txt。Cmake 并不直接建构出最终的软件,而是产生标准的建构档(如 Unix 的 Makefile 或 Windows Visual C++ 的 projects/workspaces),然后再依一般的建构方式使用。这使得熟悉某个集成开发环境(IDE)的开发者可以用标准的方式建构他的软件,这种可以使用各平台的原生建构系统的能力是 CMake 和 SCons 等其他类似系统的区别之处。

下载安装CMAKE

自行百度(((

安装完成后输入cmake

C:\Users\gloom>cmake
Usage

cmake [options] <path-to-source>
cmake [options] <path-to-existing-build>
cmake [options] -S <path-to-source> -B <path-to-build>

Specify a source directory to (re-)generate a build system for it in the
current working directory. Specify an existing build directory to
re-generate its build system.

Run 'cmake --help' for more information.

输出这些即为正确

使用Python3脚本进行构建

写一个普通的CMakeLists.txt,这里不多说了。

下载下面的脚本,并按要求修改

# Work With Python3
import os
import stat
from shutil import rmtree
from subprocess import check_call


def resolve_path(rel_path):
return os.path.abspath(os.path.join(os.path.dirname(__file__), rel_path))


def rmtree_silent(root):
def remove_readonly_handler(fn, root, excinfo):
if fn is os.rmdir:
if os.path.isdir(root): # if exists
os.chmod(root, stat.S_IWRITE) # make writable
os.rmdir(root)
elif fn is os.remove:
if os.path.isfile(root): # if exists
os.chmod(root, stat.S_IWRITE) # make writable
os.remove(root)

rmtree(root, onerror=remove_readonly_handler)


def makedirs_silent(root):
try:
os.makedirs(root)
except OSError: # mute if exists
pass


if __name__ == "__main__":
build_dir = resolve_path("build")
rmtree_silent(build_dir)
makedirs_silent(build_dir)
os.chdir(build_dir)

check_call([
"cmake",
os.path.expandvars(
"-DCMAKE_TOOLCHAIN_FILE=到Emscripten.cmake的路径,例如[D:\\Emscripten\\emscripten\\1.35.0\\cmake\\Modules\\Platform\\Emscripten.cmake]"),
"-DCMAKE_BUILD_TYPE=编译方式:Debug或者Release",
"-DCMAKE_MAKE_PROGRAM=编译的方法,例如mingw32-make",
"-G",
"Unix Makefiles",
".."
])

check_call(["make"])

运行即可完成编译,输出内容如下

-- Configuring done
-- Generating done
Scanning dependencies of target Test
[ 94%] Building CXX object CMakeFiles/Test.dir/src/Test.cpp.o
[100%] Linking CXX executable Test.js
[100%] Built target Test

到build文件夹下,运行

node Test.js

输出Hello World就完成了

WeChat Pay 微信
Alipay 支付宝
导航 // 相关文章
目录