Emscripten 的 CMake 项目

CMake project for Emscripten

我想交 CMake 和 Emscripten 朋友。在 Emscripten 项目网站上没有找到更多或更少的信息文档,但他们提供了 CMake 工具链文件,所以我认为它应该是可能的。 到目前为止,没有高级参数的非常基本的编译工作正常,但我在使用 embind 和预加载文件时遇到问题。

  1. 链接过程似乎错过了 Emscripten "binaries" 并为所有与嵌入相关的函数产生警告,例如:warning: unresolved symbol: _embind_register_class 在 rowser 中加载已编译的 JS 文件时导致相应的错误。
  2. 编译过程中没有生成.data文件。

我创建了一个简约示例,其中包括两个目标:一个 "normal"(客户端)和一个手册(手动客户端),它只是 运行s emcc,正如我所期望的那样 运行: https://github.com/xwaffelx/minimal-cmake-emscripten-project/blob/master/README.md

虽然手动方式可行,但我认为这不是正确的方式...

---更新---

应要求,这里有更简短的示例:

  1. CMakeLists.txt 文件
    cmake_minimum_required(VERSION 2.8)
    cmake_policy(SET CMP0015 NEW)
    project(emtest)
    set(CMAKE_VERBOSE_MAKEFILE on)
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/build.emscripten)
    include_directories("lib/assimp/include")
    link_directories("lib/assimp/lib-js")
    link_libraries("assimp")
    file(GLOB_RECURSE CORE_HDR src/<em>.h)
    file(GLOB_RECURSE CORE_SRC src/</em>.cpp)
    add_definitions("-s DEMANGLE_SUPPORT=1 --preload-file ${CMAKE_SOURCE_DIR}/assets --bind")
    add_executable(client ${CORE_SRC} ${CORE_HDR})
    </pre>

结果应等同于 运行手动执行以下命令:

emcc 
    -Ilib/assimp/include 
    -s DEMANGLE_SUPPORT=1
    --preload-file assets
    --bind
    Application.cpp 
    lib/assimp/lib-js/libassimp.so 
    -o client.js</pre>

这里是 Application.cpp:

#include "Application.h"

#include <iostream>

#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>

void Application::Initialize() {
    std::cout << "Initializing application." << std::endl;
    Assimp::Importer importer; // For test purpose
}

void Application::SayHello() {
    std::cout << "Hello!" << std::endl;
}

和Application.h:

#ifndef APPLICATION_H
#define APPLICATION_H

#include <emscripten/html5.h>
#include <emscripten/bind.h>

namespace e = emscripten;

class Application {
public:
    void Initialize();
    void SayHello();
};

EMSCRIPTEN_BINDINGS(EMTest) {
  e::class_<Application>("Application")
    .constructor()
      .function("Initialize", &Application::Initialize)
      .function("SayHello", &Application::SayHello);
}

#endif

然后我 运行 cmake 如下: cmake -DCMAKE_TOOLCHAIN_PATH=path/to/Emscripten.cmake .. && make 但是,在链接和 运行 代码期间会产生 warning: unresolved symbol: _embind_register_class 之类的警告,并且在编译 CMake 项目时不会在 client.data 文件中创建预加载数据。同时没有警告,并且在手动编译时一切正常 运行。

解决方案是通过提供以下 CMake 指令在链接期间指定所有标志: set_target_properties(client PROPERTIES LINK_FLAGS "-s DEMANGLE_SUPPORT=1 --preload-file assets --bind")

感谢在 github 问题跟踪器中提供帮助的 emscripten 开发人员。