CMake project for Emscripten



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

  1. 链接过程似乎错过了Emscripten的"二进制文件",并对所有与后绑定相关的函数(如以下函数)产生警告:warning: unresolved symbol: _embind_register_class,在将编译的JS文件加载到行集中时会导致相应的错误
  2. 编译期间未生成.data文件

我创建了一个极简主义示例,其中包括两个目标:一个是"正常"(客户端),另一个是手动(手动客户端),它只是按照我期望的方式运行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/.h)
    file(GLOB_RECURSE CORE_SRC src/.cpp)
    add_definitions("-s DEMANGLE_SUPPORT=1 --preload-file ${CMAKE_SOURCE_DIR}/assets --bind")
    add_executable(client ${CORE_SRC} ${CORE_HDR})
    

Result should be equivalent to running the following command manualy:

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

Here is the 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;
}

和应用。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")

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

最新更新