【问题标题】:CMake project for EmscriptenEmscripten 的 CMake 项目
【发布时间】:2016-10-11 08:39:13
【问题描述】:

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

  1. 链接过程似乎错过了 Emscripten“二进制文件”,并为所有与 embind 相关的函数生成警告,例如:warning: unresolved symbol: _embind_register_class,这会导致在 rowser 中加载编译的 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})
    

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

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

这是 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 .. &amp;&amp; make 但是,在链接和运行代码期间会产生warning: unresolved symbol: _embind_register_class 之类的警告,并且在编译 CMake 项目时不会创建client.data 文件中的预加载数据。同时没有警告,手动编译时一切正常。

【问题讨论】:

    标签: c++ cmake emscripten


    【解决方案1】:

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

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

    【讨论】:

    • 抱歉,您修改了哪个文件?生成文件?
    • 不,CMakeLists.txt 文件。只需使用set_target_properties 条目来指定链接器参数。
    猜你喜欢
    • 2017-12-28
    • 1970-01-01
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多