gpt4 book ai didi

c++ - 使用第3方库将C++项目编译为WebAssembly

转载 作者:行者123 更新时间:2023-12-02 10:33:32 24 4
gpt4 key购买 nike

我有一个包含eigen的简单c++项目。我可以在自己的机器上编译该项目,但无法使用emscripten将其编译为webassembly。

项目结构:

.
├── CMakeLists.txt
├── include
│   └── HelloWasm
│   └── my_lib.h
└── src
├── main.cpp
└── my_lib.cpp

文件内容:

CMakeLists.txt
cmake_minimum_required( VERSION 3.0 )

project( HelloWasm )

# flags

# include files
include_directories( ./include .include/HelloWasm ./src )

# target
add_executable( HelloWasm ./src/main.cpp ./src/my_lib.cpp )

# 3rd party libs
find_package(Eigen3 REQUIRED NO_MODULE)
include_directories(${EIGEN3_INCLUDE_DIR})

include / HelloWasm / my_lib.h
#include <iostream>
#include <Eigen/Dense>

using namespace std;
using Eigen::MatrixXd;

class MyLib
{
private:
protected:
public:
MyLib()
{
}
~MyLib()
{
}
void eigen_test();
};

src / main.cpp
#include <iostream>
#include "HelloWasm/my_lib.h"

using namespace std;

int main()
{
MyLib my_lib;
my_lib.eigen_test();
}

src / my_lib.cpp
#include "HelloWasm/my_lib.h"

void MyLib::eigen_test()
{
MatrixXd m(2, 2);
m(0, 0) = 3;
m(1, 0) = 2.5;
m(0, 1) = -1;
m(1, 1) = m(1, 0) + m(0, 1);
cout << '\n'
<< m << endl;
}

在本地成功编译项目:
mkdir build && cd build
cmake ..
make

➜ ./HelloWasm

3 -1
2.5 1.5

尝试编译为Webassemply时出错

(我尝试按照 emscripten docs中提供的步骤进行操作)
mkdir build && cd build
cmake ..
emcmake cmake ..


输出:
configure: cmake .. -DCMAKE_TOOLCHAIN_FILE=/Users/me/programming/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake -DCMAKE_CROSSCOMPILING_EMULATOR="/Users/me/programming/emsdk/node/12.9.1_64bit/bin/node"
-- Configuring done
-- Generating done
CMake Warning:
Manually-specified variables were not used by the project:

CMAKE_TOOLCHAIN_FILE


-- Build files have been written to: /Users/me/programming/sandbox/cpp_sandbox/so_question_project/build

现在运行make:
➜  emmake make
make: make
Scanning dependencies of target HelloWasm
[ 33%] Building CXX object CMakeFiles/HelloWasm.dir/src/main.cpp.o
[ 66%] Building CXX object CMakeFiles/HelloWasm.dir/src/my_lib.cpp.o
[100%] Linking CXX executable HelloWasm
[100%] Built target HelloWasm

显然,这没有创建.wasm文件...

执行以下操作:
em++ CMakeFiles/HelloWasm.dir/src/main.cpp.o CMakeFiles/HelloWasm.dir/src/my_lib.cpp.o -o helloWasm.js

输出:
em++: warning: CMakeFiles/HelloWasm.dir/src/main.cpp.o is not a valid input file [-Winvalid-input]
em++: warning: CMakeFiles/HelloWasm.dir/src/my_lib.cpp.o is not a valid input file [-Winvalid-input]
em++: error: no input files
note that input files without a known suffix are ignored, make sure your input files end with one of: ('.c', '.i', '.cpp', '.cxx', '.cc', '.c++', '.CPP', '.CXX', '.C', '.CC', '.C++', '.ii', '.m', '.mi', '.mm', '.mii', '/dev/null', '.bc', '.o', '.obj', '.lo', '.dylib', '.so', '.a', '.ll', '.h', '.hxx', '.hpp', '.hh', '.H', '.HXX', '.HPP', '.HH')

我在这里想念什么...?

最佳答案

更改:

em++ CMakeFiles/HelloWasm.dir/src/main.cpp.o CMakeFiles/HelloWasm.dir/src/my_lib.cpp.o -o helloWasm.js

至:
em++ CMakeFiles/HelloWasm.dir/src/main.cpp.o CMakeFiles/HelloWasm.dir/src/my_lib.cpp -o helloWasm.js

Emscripten无法编译 my_lib.cpp.o,因为它已经被编译为机器代码(它是一个目标文件)。您必须使用 .cpp文件,而不是 .cpp.o

关于c++ - 使用第3方库将C++项目编译为WebAssembly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61340521/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com