用第三方库编译C++项目到WebAssembly 我有一个简单的C++项目,包括我可以在自己的机器上编译这个项目,但是用emscripten编译到webassembly时遇到了麻烦

用第三方库编译C++项目到WebAssembly 我有一个简单的C++项目,包括我可以在自己的机器上编译这个项目,但是用emscripten编译到webassembly时遇到了麻烦,c++,webassembly,emscripten,C++,Webassembly,Emscripten,项目结构: . ├── CMakeLists.txt ├── include │   └── HelloWasm │   └── my_lib.h └── src ├── main.cpp └── my_lib.cpp 文件内容: CMakeLists.txt include/HelloWasm/my_lib.h 尝试编译到webassemply时出错 我试着按照 输出: configure: cmake .. -DCMAKE_TOOLCHAIN_FILE=/Users

项目结构:

.
├── CMakeLists.txt
├── include
│   └── HelloWasm
│       └── my_lib.h
└── src
    ├── main.cpp
    └── my_lib.cpp
文件内容:

CMakeLists.txt

include/HelloWasm/my_lib.h

尝试编译到webassemply时出错 我试着按照

输出:

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
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')
现在运行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
输出:

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
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
致:


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

我有一个建议

首先,您可以将lib文件my_lib.h和my_lib.cpp放在同一个目录中

其次,您可以为应用程序HelloWasm创建一个文件夹,并将可执行代码main.cpp放在此文件夹中

最后,您可以为每个文件夹创建CMakeLists.txt文件

这是您的新目录树: CMakeLists.txt第一个: HelloWasm的CMakeLists.txt: 用于库的CMakeLists.txt: 通过这些编辑,您可以创建更大的项目、库。我认为这样更有用。

没有这样的文件:CMakeFiles/HelloWasm.dir/src/my_lib.cpp只有my_lib.o。如果我运行:em++src/main.cpp src/my_lib.cpp-o helloWasm.js,它将找不到my_lib.h:src/main.cpp:2:10:致命错误:“未找到helloWasm/my_lib.h”文件包括helloWasm/my_lib.h^~~~~~~~~~~~~~生成的错误。em++:错误:'/Users/me/programming/emsdk/upstream/bin/clang++-target wasm32-。。。拥有my_lib.cpp和my_lib.h让我很困惑。无论如何,您必须告诉emscripten编译一个.cpp文件。如果需要在该文件中包含my_lib.h,请使用include。编辑:将my_lib.h的完整路径放入include中。
➜  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
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
.
├── CMakeLists.txt
├── Applications
│   └── HelloWasm
│       ├── CMakeLists.txt
│       └── main.cpp
└── Libraries
    ├── CMakeLists.txt
    ├── my_lib.h
    └── my_lib.cpp
cmake_minimum_required(VERSION 3.0)
project(LIBRARYANDAPPS)

#This function is starting build and looking all folders.

function( start_build )
  file( GLOB_RECURSE components "${CMAKE_SOURCE_DIR}/*/CMakeLists.txt" )
  foreach( component ${components} )
    get_filename_component( path ${component} PATH )
    add_subdirectory( ${path} )
  endforeach( )
endfunction( )

start_build()
#You can add Emscripten flags like this.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} \
    -std=c++11 --bind \
    -s USE_WEBGL2=1 -s FULL_ES3=1 --memory-init-file 0")

find_package(Eigen3 REQUIRED NO_MODULE)
include_directories(${EIGEN3_INCLUDE_DIR})

include_directories("${CMAKE_SOURCE_DIR}/Libraries")

ADD_EXECUTABLE(HelloWasm main.cpp)

TARGET_LINK_LIBRARIES(HelloWasm MyLib Eigen)
find_package(Eigen3 REQUIRED NO_MODULE)
include_directories(${EIGEN3_INCLUDE_DIR})

ADD_LIBRARY(MyLib STATIC my_lib.cpp my_lib.h)

TARGET_LINK_LIBRARIES(MyLib Eigen)