Skip to content

Instantly share code, notes, and snippets.

@PolarNick239
Created February 13, 2021 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PolarNick239/18036d942ae4b9aad9f1da756ce6c845 to your computer and use it in GitHub Desktop.
Save PolarNick239/18036d942ae4b9aad9f1da756ce6c845 to your computer and use it in GitHub Desktop.
buildOCV.sh + CLion workaround for OpenCV
#!/bin/bash -e
# Adopted from https://docs.opencv.org/master/d3/d52/tutorial_windows_install.html
#
# Changes:
# - version fixed to 4.5.1 - https://github.com/opencv/opencv/releases/tag/4.5.1
# - disabled opencv_contrib
# - RelWithDebInfo instead of Release
#
# How to use:
# 1) Run git-bash
# 2) mkdir /c/lib
# 3) Save this script to C:/lib/installOCV.sh
# 4) cd /c/lib
# 5) ./installOCV.sh
# 6) CLion: File->Settings->Build, ..->CMake:
# - Add RelWithDebInfo profile
# - To both profiles add to CMake options: -DOpenCV_DIR=C:\lib\build_opencv
# - Right click on the project root->Copy->Absolute path
# - Edit launch configurations: Working directory: Ctrl+V (i.e. path to project root)
# 7) Try to run test_sift (in both: Debug and RelWithDebInfo) - it will fail
#
# 8) Copy to ...\PhotogrammetryTasks2021\cmake-build-relwithdebinfo these files:
# - ...\PhotogrammetryTasks2021\cmake-build-relwithdebinfo\bin\gtest_main.dll
# - ...\PhotogrammetryTasks2021\cmake-build-relwithdebinfo\bin\gtest.dll
# - C:\lib\install\opencv\x64\vc16\bin\opencv_world451.dll
#
# 9) Copy to ...\PhotogrammetryTasks2021\cmake-build-debug these files:
# - ...\PhotogrammetryTasks2021\cmake-build-debug\bin\gtest_maind.dll
# - ...\PhotogrammetryTasks2021\cmake-build-debug\bin\gtestd.dll
# - C:\lib\install\opencv\x64\vc16\bin\opencv_world451d.dll
#
# 10) Launch test_sift - should work :)
myRepo=$(pwd)
CMAKE_GENERATOR_OPTIONS=-G"Visual Studio 16 2019"
#CMAKE_GENERATOR_OPTIONS=-G"Visual Studio 15 2017 Win64"
#CMAKE_GENERATOR_OPTIONS=(-G"Visual Studio 16 2019" -A x64) # CMake 3.14+ is required
if [ ! -d "$myRepo/opencv" ]; then
echo "cloning opencv"
#git clone https://github.com/opencv/opencv.git
git clone --depth 1 --branch 4.5.1 https://github.com/opencv/opencv.git
else
cd opencv
git pull --rebase
cd ..
fi
#if [ ! -d "$myRepo/opencv_contrib" ]; then
# echo "cloning opencv_contrib"
# git clone https://github.com/opencv/opencv_contrib.git
#else
# cd opencv_contrib
# git pull --rebase
# cd ..
#fi
RepoSource=opencv
mkdir -p build_opencv
pushd build_opencv
CMAKE_OPTIONS=(-DBUILD_PERF_TESTS:BOOL=OFF -DBUILD_TESTS:BOOL=OFF -DBUILD_DOCS:BOOL=OFF -DWITH_CUDA:BOOL=OFF -DBUILD_EXAMPLES:BOOL=OFF -DINSTALL_CREATE_DISTRIB=ON)
set -x
#cmake "${CMAKE_GENERATOR_OPTIONS[@]}" "${CMAKE_OPTIONS[@]}" -DOPENCV_EXTRA_MODULES_PATH="$myRepo"/opencv_contrib/modules -DCMAKE_INSTALL_PREFIX="$myRepo/install/$RepoSource" "$myRepo/$RepoSource"
cmake "${CMAKE_GENERATOR_OPTIONS[@]}" "${CMAKE_OPTIONS[@]}" -DCMAKE_INSTALL_PREFIX="$myRepo/install/$RepoSource" "$myRepo/$RepoSource"
echo "************************* $Source_DIR -->debug"
cmake --build . --config debug
echo "************************* $Source_DIR -->RelWithDebInfo"
cmake --build . --config RelWithDebInfo
cmake --build . --target install --config RelWithDebInfo
cmake --build . --target install --config debug
popd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment