use OpenCV with Clion IDE on Windows
Asked Answered
R

3

19

I'm actually looking for a way to create apps with OpenCV with Clion from JetBrains.

I've installed OpenCV with Choco, so I have all the stuff in C:\opencv

this is my projet with Clion

CMakeLists.txt:

cmake_minimum_required(VERSION 3.3)
project(test)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
include_directories("C:\\opencv\\build\\include\\")

FIND_PACKAGE( OpenCV REQUIRED core highgui imgproc)
set(OpenCV_FOUND TRUE)

set(SOURCE_FILES main.cpp)
add_executable(prog ${SOURCE_FILES})

and the main.cpp:

#include <opencv2/opencv.hpp>

int main() {

    cv::Mat img = cv::imread("./test.jpg", -1);
    cv::imshow("Mon image", img);
    cv::waitKey(0);
    return 0;
}

and the response to build is :

undefined reference to `cv::imread(cv::String const&, int)'

and undefined errors for all OpenCV functions

Do you know why it doesn't works?

Rident answered 14/3, 2016 at 10:12 Comment(2)
have you set your environmental variables? What compiler are you using? You're not linking your executeable against the OpenCV_LIBSSyphon
any love for macos environments?Dannadannel
S
47

First of all, you need CMake.
Then you need a compiler of your choise (MinGW, Visual Studio, ...).

  1. Download the OpenCV source files. Link
  2. Unpack to C:\opencv (or a folder of your choice)
  3. Open CMake and select source (directory of 2.) and build for example C:\opencv\mingw-build or C:\opencv\vs-x64-build. Choose one accoring your configuration.
  4. Click Configure and select the generator according to you compiler. MinGW Makefiles in case of MingGW or Visual Studio ... if you are using Visual Studio and so on ...
    (If you experience problems with MinGW, ensure that the minGW/bin directory is added to the evironment path labelled, 'PATH')
  5. Wait for the configuration to be done, edit your properties of your needs (in my case I don't need tests, docs and python).
    enter image description here Click Configure again. When everything is white click Generate else edit the red fields.
  6. Open cmd and dir to build directory of 3.
    If you chose Visual Studio open the generated solution.
  7. Compile the library. Run mingw32-make (or mingw64-make) or build the BUILD_ALL project from the generated solution in Visual Studio if your choosen compiler is MSVC.
    This takes a while. enter image description here
  8. Once it is done, run mingw32-make install (or mingw64-make install). If you've choosen Visual Studio you need to build the INSTALL project.
    This creates an install folder, where everything you need for building your own OpenCV apps is included.
  9. To system PATH add C:\opencv\mingw-build\install\x86\mingw\bin
    Restart your PC.
  10. Set up CLion:

CMakeLists.txt:

project(test)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

# Where to find CMake modules and OpenCV
set(OpenCV_DIR "C:\\opencv\\mingw-build\\install")
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/")

find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})

add_executable(test_cv main.cpp)

# add libs you need
set(OpenCV_LIBS opencv_core opencv_imgproc opencv_highgui opencv_imgcodecs) 

# linking
target_link_libraries(test_cv ${OpenCV_LIBS})

main.cpp:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

int main(int argc, char** argv)
{
    if(argc != 2)
    {
        std::cout << "Usage: display_image ImageToLoadAndDisplay" << std::endl;
        return -1;
    }

    cv::Mat frame;
    frame = cv::imread(argv[1], IMREAD_COLOR); // Read the file

    if(!frame) // Check for invalid input
    {
        std::cout << "Could not open or find the frame" << std::endl;
        return -1;
    }

    cv::namedWindow("Window", WINDOW_AUTOSIZE); // Create a window for display.
    cv::imshow("Window", frame); // Show our image inside it.

    cv::waitKey(0); // Wait for a keystroke in the window
    return 0;
}

Build and run main.cpp.

All Paths depend on the setup you make in 2. and 3. You can change them if you like.

UPDATE 1: I updated the post for using a compiler of you choice.

SUGGESTION: Using a package manager like Conan makes life much easier.

Syphon answered 5/4, 2016 at 9:43 Comment(9)
Above you state, "You need to download FindOpenCV.cmake and add it to project-root/cmake/." In my case, I needed to add FindOpenCV.cmake to CLion's CMake installation directory, which in my case was at C:\Program Files (x86)\JetBrains\CLion 2016.2.1\bin\cmake\share\cmake-3.6\Modules. In this location I find many other Find*.cmake files.Thruway
Can I use this with Cygwin in CLion instead of MinGW while building opencv with MinGW, as in CMake-gui there isn't CygwinJuvenescent
@MichałZiobro Yes you can.Syphon
Why i am getting continuosly errors about undefined references to cvRound() founction? I tried prebuild libraries in opencv extracted packege and also other cygiwn precompiled packege from some website. I have also tried to compile it with CMake gui but it continuously fails to compile with some errors like invocations of some thread realated functions. It seems very hard to get it working on windows ;/ i am porting app from macOS and don't think it will be so hard to make it working correctly.Juvenescent
I tried to run it with Cygwin, but constantly saw multiple errors, then installed MiniGW and all worked like a charm.Titan
Did anyone have a problem when executing cingw32-make? I did everything exactly by the guide, but it throws error like this: opencv\sources\3rdparty\libjpeg-turbo\src\jmemmgr.c: In function 'realize_virt_arrays': \opencv\sources\3rdparty\libjpeg-turbo\src\jmemmgr.c:662:11: error: 'SIZE_MAX' undeclared (first use in this function) if (SIZE_MAX - maximum_space < new_space) ^~~~~~~~ Any idea, please? I am not really c++ developer.Wallraff
Im getting C:\opencv\3rdparty\protobuf\src\google\protobuf\stubs\io_win32.cc:330:10: error: '::_wfopen' has not been declared return ::_wfopen(wpath.c_str(), wmode.c_str()); error ON step 7, anyone ideas what to do here?Divide
I made all of this and I add this FindOpenCV.cmake (your link is dead) in the project and in the folder C:\Program Files\JetBrains\CLion 2020.2.1\bin\cmake\win\share\cmake-3.17\Modules But I have : > Found OpenCV Windows Pack but it has no binaries compatible with your configuration. You should manually point CMake variable OpenCV_DIR to your build of OpenCV library. My PATH is : C:\tools\opencv\build\install\x64\mingw\bin ; C:\Program Files\CMake\binTransmogrify
link to FindOpencv.cmake is brokenDepositary
C
2

I just want to add one more thing in the answer of daB0bby. Few Min-GW Compilers do not support C++ Version 11 or later. This version is required for thread in OpenCV. So I will suggest using TDM-GCC Compiler instead of MinGW Compiler. Install this compiler and set path C:\TDM-GCC-64\bin to the system's environmental variable.

Chilli answered 24/6, 2020 at 16:38 Comment(0)
C
0

While you can build it like the answer by daB0bby, as he suggests at the end of his post a package manager is way more convenient.

In fact, Vcpkg is actually built into Clion.

In New UI you have to click View->Tool Windows->Vcpkg then add an instance with the + sign and find OpenCV.

Consideration answered 1/3 at 9:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.