LoginSignup
5
6

More than 5 years have passed since last update.

クロスコンパイルしてwindows用のOpenCVライブラリを作る

Last updated at Posted at 2015-11-10

背景

mruby-cliやcgoでクロスコンパイルしてWindowsのバイナリを簡単に作れるが、今回取り上げたOpenCVなど
サードパーティのライブラリを呼び出すコードを含むようなコードはたぶんビルドが難しいのではと
感じていた。

この問題解決の第一歩として、自前で、Linux上で、Windows向けのOpenCVライブラリのビルドを試みた。
最終的にワンバイナリを目標としているので、DLLではなく、.aの静的ライブラリを作成するようにしている。

前提

以下の、Dockerコンテナーを用意して、mingw-w64が動くLinux環境を用意した。

クロスコンパイル向けの.cmakeファイルの用意

set(CMAKE_SYSTEM_NAME Windows)

# specify the cross compiler
set(CMAKE_C_COMPILER   /usr/bin/i686-w64-mingw32-gcc)
set(CMAKE_CXX_COMPILER /usr/bin/i686-w64-mingw32-g++)

# set PKG_CONFIG_PATH for MinGW Cross Compile Environment
set(ENV{PKG_CONFIG_PATH} /usr/i686-w64-mingw32/lib/pkgconfig)

# where is the target environment 
set(CMAKE_FIND_ROOT_PATH /usr/i686-w64-mingw32/)

コンパイラオプションの変更

-Werror=non-virtual-dtorを無効化する

--- a/cmake/OpenCVCompilerOptions.cmake
+++ b/cmake/OpenCVCompilerOptions.cmake
@@ -64,7 +64,7 @@ if(CMAKE_COMPILER_IS_GNUCXX)
   add_extra_compiler_option(-W)
   add_extra_compiler_option(-Wall)
   add_extra_compiler_option(-Werror=return-type)
-  add_extra_compiler_option(-Werror=non-virtual-dtor)
+  #add_extra_compiler_option(-Werror=non-virtual-dtor)
   add_extra_compiler_option(-Werror=address)
   add_extra_compiler_option(-Werror=sequence-point)
   add_extra_compiler_option(-Wformat)

参考資料

CMakeによるビルド

mkdir build
cd  build
cmake -DCMAKE_TOOLCHAIN_FILE=../mytool.cmake -DWITH_IPP=OFF -DBUILD_SHARED_LIBS=OFF  ..
make
make install
cd ..

使い方

サンプル

WebCamの画像を表示する

#include "opencv2/opencv.hpp"

int main(int, char**)
{
    cv::VideoCapture cap(1); // open the default camera
    if (!cap.isOpened())  // check if we succeeded
        return -1;
    for (;;)
    {
        cv::Mat frame;
        cap >> frame; // get a new frame from camera
        //cvtColor(frame, edges, COLOR_BGR2GRAY);
        //GaussianBlur(edges, edges, Size(7, 7), 1.5, 1.5);
        //Canny(edges, edges, 0, 30, 3);
        cv::imshow("Webcam feed", frame);
        if (cv::waitKey(30) >= 0) break;
    }
    // the camera will be deinitialized automatically in VideoCapture destructor

    return 0;
}

ビルド方法

.aファイルの指定順番が超大事。

i686-w64-mingw32-g++ webcam.cpp -o webcam.exe -I ./install/include/ \
 install/lib/libopencv_highgui300.a \
 install/lib/libopencv_videoio300.a \
 install/lib/libopencv_imgcodecs300.a \
 install/lib/libopencv_imgproc300.a \
 install/lib/libopencv_core300.a \
 install/lib/libopencv_hal300.a \
 3rdparty/lib/libIlmImf.a \
 3rdparty/lib/libzlib.a \
 3rdparty/lib/liblibjasper.a \
 3rdparty/lib/liblibjpeg.a \
 3rdparty/lib/liblibtiff.a \
 3rdparty/lib/liblibwebp.a \
 3rdparty/lib/liblibpng.a \
 -lvfw32 -lole32 -loleaut32 -lgdi32 -lcomdlg32 -luuid \
 -static-libgcc -static-libstdc++

知見

BUILD_opencv_worldでライブラリをまとめようとしたら、エラーとなっていまい、NG。

関連記事

Windowsでのビルド記事

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6