如何将 emscripten 与 cmake 一起用于项目依赖项?
How to use emscripten with cmake for project dependencies?
所以我正在尝试将 emscripten 移植到 WebAssembly (wasm) 一个程序,该程序在开头包含以下内容:
#include <Eigen/Geometry>
#include <boost/filesystem.hpp>
#include <dvo/core/intrinsic_matrix.h>
#include <dvo/core/surface_pyramid.h>
#include <dvo/dense_tracking.h>
#include <fstream>
#include <iostream>
#include <opencv2/opencv.hpp>
为了轻松开始,我尝试编译了一个最小的 hello world OpenCV 程序:
#include <opencv2/opencv.hpp>
#include <stdio.h>
using namespace cv;
int main(int argc, char **argv) {
Mat M(2, 2, CV_8UC3, Scalar(0, 0, 255));
std::cout << "M = " << std::endl << " " << M << std::endl;
return 0;
}
我有以下 CMakeLists.txt
:
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED PATHS third-party/opencv-4.1.0/build_wasm NO_DEFAULT_PATH)
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( hello hello.cpp )
target_link_libraries( hello ${OpenCV_LIBS} )
其中 third-party/opencv-4.1.0/build_wasm
是我将 OpenCV 构建到 wasm (cf doc) 的文件夹:
python ./platforms/js/build_js.py build_wasm --build_wasm
现在,当我在我的项目中 运行 emconfigure cmake ..
时,它没有找到 OpenCV ("Could not find a package configuration ...")。我做错了什么,但我不知道是什么。不幸的是,关于将 emscripten 与 cmake 和依赖项一起使用的文档没有详细说明。或者这个问题可能是 OpenCV 特有的,我不知道。
编辑
完全错误:
~/t/w/w/build emconfigure cmake .. ven. 26 avril 2019 16:01:00 CEST
CMake Error at CMakeLists.txt:5 (find_package):
Could not find a package configuration file provided by "OpenCV" with any
of the following names:
OpenCVConfig.cmake
opencv-config.cmake
Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set
"OpenCV_DIR" to a directory containing one of the above files. If "OpenCV"
provides a separate development package or SDK, be sure it has been
installed.
-- Configuring incomplete, errors occurred!
See also "/home/matthieu/temp/wasm/wasm-opencv/build/CMakeFiles/CMakeOutput.log".
shared:ERROR: Configure step failed with non-zero return code: 1. Command line: cmake -DCMAKE_CROSSCOMPILING_EMULATOR="/home/matthieu/programs/emsdk/node/8.9.1_64bit/bin/node" .. -DCMAKE_TOOLCHAIN_FILE=/home/matthieu/programs/emsdk/emscripten/1.38.30/cmake/Modules/Platform/Emscripten.cmake at /home/matthieu/temp/wasm/wasm-opencv/build
CMakeFiles/CMakeOutput.log
的内容:
The target system is: Emscripten - 1 - x86
The host system is: Linux - 4.19.36-1-lts - x86_64
所以我终于设法用 cmake 编译了这个例子。不过这很丑陋,因为我手动添加了一些 GLOB
的包含和库。我希望 OpenCV 在将库构建为 wasm 时,会提供一个 OpenCVConfig.cmake
文件,其中包含所有依赖项设置。它将允许在以 wasm 为目标时使用经典的 ${OpenCV_INCLUDE_DIRS}
和 ${OpenCV_LIBS}
。
cmake_minimum_required( VERSION 3.1 )
set( CMAKE_CXX_STANDARD 11 )
project( HelloCV )
# Does not work
# find_package( OpenCV REQUIRED PATHS third-party/opencv-4.1.0/build_wasm NO_DEFAULT_PATH)
# Needed for opencv2/opencv.hpp
include_directories( third-party/opencv-4.1.0/include )
# Needed by opencv.hpp for opencv2/opencv_modules.hpp
include_directories( third-party/opencv-4.1.0/build_wasm )
# Needed by opencv_modules.hpp for every module
file( GLOB opencv_include_modules "third-party/opencv-4.1.0/modules/*/include" )
include_directories( ${opencv_include_modules} )
# Our hello world executable
add_executable( hello hello.cpp )
# Link to opencv.js precompiled libraries
file( GLOB opencv_js "third-party/opencv-4.1.0/build_wasm/lib/*.a" )
target_link_libraries( hello ${opencv_js} )
这在使用 emscripten 编译为 WebAssembly 时效果很好:
~/t/e/o/build emconfigure cmake -DCMAKE_BUILD_TYPE=Release ..
...
~/t/e/o/build emmake make
...
~/t/e/o/build node hello.js
M =
[ 0, 0, 255, 0, 0, 255;
0, 0, 255, 0, 0, 255]
我当前的 CMakeLists.txt
的一个烦人的问题是它没有在本机中成功编译,只有 wasm。如果您有比这更好的 cmake 配置,那么可以轻松更改目标(x86 或 emscripten),请分享您的答案。
如果您的项目在源文件数量方面不是太大,您最好使用不带 cmake 的 emscripten。这是对我有用的命令示例:
emcc latuile.cpp binpack.cpp compact_frame.cpp compact_rectangles.cpp fit_together.cpp KMeansRexCore.cpp MPD_Arc.cpp MyRect.cpp optimize_rectangle_positions.cpp permutation.cpp stair_steps.cpp swap_rectangles.cpp WidgetContext.cpp FunctionTimer.cpp -o latuile.js -I/usr/include/eigen3 -Wno-c++11-narrowing -s EXPORTED_FUNCTIONS='["_latuile"]' -s EXPORTED_RUNTIME_METHODS='["ccall","cwrap"]' -s ALLOW_MEMORY_GROWTH=1
使用cmake,我遇到了和你一样的错误,所以我选择了这条路,不再浪费太多时间了。
我刚刚从我的 cmake 中复制了 cpp 文件列表。
注意 EXPORTED_FUNCTIONS='["_latuile"]'
EXPORTED_RUNTIME_METHODS='["ccall","cwrap"]'
还有 -s ALLOW_MEMORY_GROWTH=1
选项。
我不知道你如何将所有这些都输入到 cmake 中......
所以我正在尝试将 emscripten 移植到 WebAssembly (wasm) 一个程序,该程序在开头包含以下内容:
#include <Eigen/Geometry>
#include <boost/filesystem.hpp>
#include <dvo/core/intrinsic_matrix.h>
#include <dvo/core/surface_pyramid.h>
#include <dvo/dense_tracking.h>
#include <fstream>
#include <iostream>
#include <opencv2/opencv.hpp>
为了轻松开始,我尝试编译了一个最小的 hello world OpenCV 程序:
#include <opencv2/opencv.hpp>
#include <stdio.h>
using namespace cv;
int main(int argc, char **argv) {
Mat M(2, 2, CV_8UC3, Scalar(0, 0, 255));
std::cout << "M = " << std::endl << " " << M << std::endl;
return 0;
}
我有以下 CMakeLists.txt
:
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED PATHS third-party/opencv-4.1.0/build_wasm NO_DEFAULT_PATH)
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( hello hello.cpp )
target_link_libraries( hello ${OpenCV_LIBS} )
其中 third-party/opencv-4.1.0/build_wasm
是我将 OpenCV 构建到 wasm (cf doc) 的文件夹:
python ./platforms/js/build_js.py build_wasm --build_wasm
现在,当我在我的项目中 运行 emconfigure cmake ..
时,它没有找到 OpenCV ("Could not find a package configuration ...")。我做错了什么,但我不知道是什么。不幸的是,关于将 emscripten 与 cmake 和依赖项一起使用的文档没有详细说明。或者这个问题可能是 OpenCV 特有的,我不知道。
编辑
完全错误:
~/t/w/w/build emconfigure cmake .. ven. 26 avril 2019 16:01:00 CEST
CMake Error at CMakeLists.txt:5 (find_package):
Could not find a package configuration file provided by "OpenCV" with any
of the following names:
OpenCVConfig.cmake
opencv-config.cmake
Add the installation prefix of "OpenCV" to CMAKE_PREFIX_PATH or set
"OpenCV_DIR" to a directory containing one of the above files. If "OpenCV"
provides a separate development package or SDK, be sure it has been
installed.
-- Configuring incomplete, errors occurred!
See also "/home/matthieu/temp/wasm/wasm-opencv/build/CMakeFiles/CMakeOutput.log".
shared:ERROR: Configure step failed with non-zero return code: 1. Command line: cmake -DCMAKE_CROSSCOMPILING_EMULATOR="/home/matthieu/programs/emsdk/node/8.9.1_64bit/bin/node" .. -DCMAKE_TOOLCHAIN_FILE=/home/matthieu/programs/emsdk/emscripten/1.38.30/cmake/Modules/Platform/Emscripten.cmake at /home/matthieu/temp/wasm/wasm-opencv/build
CMakeFiles/CMakeOutput.log
的内容:
The target system is: Emscripten - 1 - x86
The host system is: Linux - 4.19.36-1-lts - x86_64
所以我终于设法用 cmake 编译了这个例子。不过这很丑陋,因为我手动添加了一些 GLOB
的包含和库。我希望 OpenCV 在将库构建为 wasm 时,会提供一个 OpenCVConfig.cmake
文件,其中包含所有依赖项设置。它将允许在以 wasm 为目标时使用经典的 ${OpenCV_INCLUDE_DIRS}
和 ${OpenCV_LIBS}
。
cmake_minimum_required( VERSION 3.1 )
set( CMAKE_CXX_STANDARD 11 )
project( HelloCV )
# Does not work
# find_package( OpenCV REQUIRED PATHS third-party/opencv-4.1.0/build_wasm NO_DEFAULT_PATH)
# Needed for opencv2/opencv.hpp
include_directories( third-party/opencv-4.1.0/include )
# Needed by opencv.hpp for opencv2/opencv_modules.hpp
include_directories( third-party/opencv-4.1.0/build_wasm )
# Needed by opencv_modules.hpp for every module
file( GLOB opencv_include_modules "third-party/opencv-4.1.0/modules/*/include" )
include_directories( ${opencv_include_modules} )
# Our hello world executable
add_executable( hello hello.cpp )
# Link to opencv.js precompiled libraries
file( GLOB opencv_js "third-party/opencv-4.1.0/build_wasm/lib/*.a" )
target_link_libraries( hello ${opencv_js} )
这在使用 emscripten 编译为 WebAssembly 时效果很好:
~/t/e/o/build emconfigure cmake -DCMAKE_BUILD_TYPE=Release ..
...
~/t/e/o/build emmake make
...
~/t/e/o/build node hello.js
M =
[ 0, 0, 255, 0, 0, 255;
0, 0, 255, 0, 0, 255]
我当前的 CMakeLists.txt
的一个烦人的问题是它没有在本机中成功编译,只有 wasm。如果您有比这更好的 cmake 配置,那么可以轻松更改目标(x86 或 emscripten),请分享您的答案。
如果您的项目在源文件数量方面不是太大,您最好使用不带 cmake 的 emscripten。这是对我有用的命令示例:
emcc latuile.cpp binpack.cpp compact_frame.cpp compact_rectangles.cpp fit_together.cpp KMeansRexCore.cpp MPD_Arc.cpp MyRect.cpp optimize_rectangle_positions.cpp permutation.cpp stair_steps.cpp swap_rectangles.cpp WidgetContext.cpp FunctionTimer.cpp -o latuile.js -I/usr/include/eigen3 -Wno-c++11-narrowing -s EXPORTED_FUNCTIONS='["_latuile"]' -s EXPORTED_RUNTIME_METHODS='["ccall","cwrap"]' -s ALLOW_MEMORY_GROWTH=1
使用cmake,我遇到了和你一样的错误,所以我选择了这条路,不再浪费太多时间了。
我刚刚从我的 cmake 中复制了 cpp 文件列表。
注意 EXPORTED_FUNCTIONS='["_latuile"]'
EXPORTED_RUNTIME_METHODS='["ccall","cwrap"]'
还有 -s ALLOW_MEMORY_GROWTH=1
选项。
我不知道你如何将所有这些都输入到 cmake 中......