Search code examples
c++linuxopencvclion

Can't compile OpenCV header files in CLion


I recently installed OpenCV on my Ubuntu system. I used instructions from here and here to install OpenCV c++.

When I include the opencv header in my file:

#include <opencv2/opencv.hpp>

It gives me the following error while building:

note: ‘cv::Range’ declared here
  589 | class CV_EXPORTS Range
      |                  ^~~~~
In file included from /usr/local/include/opencv4/opencv2/core.hpp:59,
                 from /usr/local/include/opencv4/opencv2/core/core.hpp:48,
                 from /home/mo/CLionProjects/DisjointForest/main.cpp:4:
/usr/local/include/opencv4/opencv2/core/mat.hpp:1640:43: error: template argument 1 is invalid
 1640 |     Mat operator()(const std::vector<Range>& ranges) const;
      |                                           ^
/usr/local/include/opencv4/opencv2/core/mat.hpp:1640:43: error: template argument 2 is invalid
/usr/local/include/opencv4/opencv2/core/mat.hpp:1640:9: error: ‘Mat Mat::operator()(const int&) const’ cannot be overloaded with ‘Mat Mat::operator()(const int&) const’
 1640 |     Mat operator()(const std::vector<Range>& ranges) const;
      |         ^~~~~~~~
/usr/local/include/opencv4/opencv2/core/mat.hpp:1630:9: note: previous declaration ‘Mat Mat::operator()(const int&) const’
 1630 |     Mat operator()( const Rect& roi ) const;
      |         ^~~~~~~~
/usr/local/include/opencv4/opencv2/core/mat.hpp:1643:44: error: expected type-specifier
 1643 |     template<typename _Tp, int n> operator Vec<_Tp, n>() const;
      |                                            ^~~~~~~~~~~
/usr/local/include/opencv4/opencv2/core/mat.hpp:1644:51: error: expected type-specifier
 1644 |     template<typename _Tp, int m, int n> operator Matx<_Tp, m, n>() const;
      |                                                   ^~~~~~~~~~~~~~~
/usr/local/include/opencv4/opencv2/core/mat.hpp:1850:38: error: ‘Vec’ does not name a type
 1850 |     template<int n> uchar* ptr(const Vec<int, n>& idx);
      |                                      ^~~
/usr/local/include/opencv4/opencv2/core/mat.hpp:1850:41: error: expected ‘,’ or ‘...’ before ‘<’ token
 1850 |     template<int n> uchar* ptr(const Vec<int, n>& idx);
      |                                         ^
/usr/local/include/opencv4/opencv2/core/mat.hpp:1852:44: error: ‘Vec’ does not name a type
 1852 |     template<int n> const uchar* ptr(const Vec<int, n>& idx) const;
      |                                            ^~~
/usr/local/include/opencv4/opencv2/core/mat.hpp:1852:47: error: expected ‘,’ or ‘...’ before ‘<’ token
 1852 |     template<int n> const uchar* ptr(const Vec<int, n>& idx) const;
      |                                               ^
/usr/local/include/opencv4/opencv2/core/mat.hpp:1877:50: error: ‘Vec’ does not name a type
 1877 |     template<typename _Tp, int n> _Tp* ptr(const Vec<int, n>& idx);
      |                                                  ^~~
/usr/local/include/opencv4/opencv2/core/mat.hpp:1877:53: error: expected ‘,’ or ‘...’ before ‘<’ token
 1877 |     template<typename _Tp, int n> _Tp* ptr(const Vec<int, n>& idx);
      |                                                     ^
/usr/local/include/opencv4/opencv2/core/mat.hpp:1879:56: error: ‘Vec’ does not name a type
 1879 |     template<typename _Tp, int n> const _Tp* ptr(const Vec<int, n>& idx) const;
      |                                                        ^~~
/usr/local/include/opencv4/opencv2/core/mat.hpp:1879:59: error: expected ‘,’ or ‘...’ before ‘<’ token
 1879 |     template<typename _Tp, int n> const _Tp* ptr(const Vec<int, n>& idx) const;
      |                                                           ^
/usr/local/include/opencv4/opencv2/core/mat.hpp:1950:49: error: ‘Vec’ does not name a type
 1950 |     template<typename _Tp, int n> _Tp& at(const Vec<int, n>& idx);
      |                                                 ^~~
/usr/local/include/opencv4/opencv2/core/mat.hpp:1950:52: error: expected ‘,’ or ‘...’ before ‘<’ token
 1950 |     template<typename _Tp, int n> _Tp& at(const Vec<int, n>& idx);
      |                                                    ^
/usr/local/include/opencv4/opencv2/core/mat.hpp:1952:55: error: ‘Vec’ does not name a type
 1952 |     template<typename _Tp, int n> const _Tp& at(const Vec<int, n>& idx) const;
      |                                                       ^~~
/usr/local/include/opencv4/opencv2/core/mat.hpp:1952:58: error: expected ‘,’ or ‘...’ before ‘<’ token
 1952 |     template<typename _Tp, int n> const _Tp& at(const Vec<int, n>& idx) const;
      |                                                          ^
/usr/local/include/opencv4/opencv2/core/mat.hpp:1958:36: error: ‘Point’ was not declared in this scope; did you mean ‘cv::Point’?
 1958 |     template<typename _Tp> _Tp& at(Point pt);
      |                                    ^~~~~
      |                                    cv::Point
In file included from /usr/local/include/opencv4/opencv2/core.hpp:58,
                 from /usr/local/include/opencv4/opencv2/core/core.hpp:48,
                 from /home/mo/CLionProjects/DisjointForest/main.cpp:4:
/usr/local/include/opencv4/opencv2/core/types.hpp:194:17: note: ‘cv::Point’ declared here
  194 | typedef Point2i Point;
      |                 ^~~~~
In file included from /usr/local/include/opencv4/opencv2/core.hpp:59,
                 from /usr/local/include/opencv4/opencv2/core/core.hpp:48,
                 from /home/mo/CLionProjects/DisjointForest/main.cpp:4:
/usr/local/include/opencv4/opencv2/core/mat.hpp:1958:45: error: data member ‘at’ cannot be a member template
 1958 |     template<typename _Tp> _Tp& at(Point pt);
      |                                             ^
/usr/local/include/opencv4/opencv2/core/mat.hpp:1963:42: error: ‘Point’ was not declared in this scope; did you mean ‘cv::Point’?
 1963 |     template<typename _Tp> const _Tp& at(Point pt) const;
      |                                          ^~~~~
      |                                          cv::Point
In file included from /usr/local/include/opencv4/opencv2/core.hpp:58,
                 from /usr/local/include/opencv4/opencv2/core/core.hpp:48,
                 from /home/mo/CLionProjects/DisjointForest/main.cpp:4:
/usr/local/include/opencv4/opencv2/core/types.hpp:194:17: note: ‘cv::Point’ declared here
  194 | typedef Point2i Point;
      |                 ^~~~~
In file included from /usr/local/include/opencv4/opencv2/core.hpp:59,
                 from /usr/local/include/opencv4/opencv2/core/core.hpp:48,
                 from /home/mo/CLionProjects/DisjointForest/main.cpp:4:
/usr/local/include/opencv4/opencv2/core/mat.hpp:1963:51: error: expected ‘;’ before ‘const’
 1963 |     template<typename _Tp> const _Tp& at(Point pt) const;
      |                                                   ^~~~~~
      |                                                   ;
/usr/local/include/opencv4/opencv2/core/mat.hpp:1963:57: error: data member ‘at’ cannot be a member template
 1963 |     template<typename _Tp> const _Tp& at(Point pt) const;
      |                                                         ^
/usr/local/include/opencv4/opencv2/core/mat.hpp:2001:28: error: ‘MatIterator_’ does not name a type
 2001 |     template<typename _Tp> MatIterator_<_Tp> begin();
      |                            ^~~~~~~~~~~~
/usr/local/include/opencv4/opencv2/core/mat.hpp:2002:28: error: ‘MatConstIterator_’ does not name a type
 2002 |     template<typename _Tp> MatConstIterator_<_Tp> begin() const;
      |                            ^~~~~~~~~~~~~~~~~
/usr/local/include/opencv4/opencv2/core/mat.hpp:2009:28: error: ‘MatIterator_’ does not name a type
 2009 |     template<typename _Tp> MatIterator_<_Tp> end();
      |                            ^~~~~~~~~~~~
/usr/local/include/opencv4/opencv2/core/mat.hpp:2010:28: error: ‘MatConstIterator_’ does not name a type
 2010 |     template<typename _Tp> MatConstIterator_<_Tp> end() const;
      |                            ^~~~~~~~~~~~~~~~~
/usr/local/include/opencv4/opencv2/core/mat.hpp:957:68: error: ‘Range’ has not been declared
  957 |     Mat(const Mat& m, const Range& rowRange, const Range& colRange=Range::all());
      |                                                                    ^~~~~
/usr/local/include/opencv4/opencv2/core/mat.hpp:2181:22: error: expected nested-name-specifier before ‘DataType’
 2181 |     typedef typename DataType<_Tp>::channel_type channel_type;
      |                      ^~~~~~~~
/usr/local/include/opencv4/opencv2/core/mat.hpp:2181:22: error: expected ‘;’ at end of member declaration
 2181 |     typedef typename DataType<_Tp>::channel_type channel_type;
      |                      ^~~~~~~~
      |                              ;
/usr/local/include/opencv4/opencv2/core/mat.hpp:2181:30: error: expected unqualified-id before ‘<’ token
 2181 |     typedef typename DataType<_Tp>::channel_type channel_type;
      |                              ^
/usr/local/include/opencv4/opencv2/core/mat.hpp:2182:13: error: ‘MatIterator_’ does not name a type
 2182 |     typedef MatIterator_<_Tp> iterator;
      |             ^~~~~~~~~~~~
/usr/local/include/opencv4/opencv2/core/mat.hpp:2183:13: error: ‘MatConstIterator_’ does not name a type
 2183 |     typedef MatConstIterator_<_Tp> const_iterator;
      |             ^~~~~~~~~~~~~~~~~
/usr/local/include/opencv4/opencv2/core/mat.hpp:2192:23: error: expected ‘)’ before ‘_size’
 2192 |     explicit Mat_(Size _size);
      |                  ~    ^~~~~~
      |                       )
/usr/local/include/opencv4/opencv2/core/mat.hpp:2194:14: error: expected ‘)’ before ‘_size’
 2194 |     Mat_(Size _size, const _Tp& value);
      |         ~    ^~~~~~
      |              )
/usr/local/include/opencv4/opencv2/core/mat.hpp:2208:31: error: ‘Range’ does not name a type
 2208 |     Mat_(const Mat_& m, const Range& rowRange, const Range& colRange=Range::all());
      |                               ^~~~~
/usr/local/include/opencv4/opencv2/core/mat.hpp:2208:54: error: ‘Range’ does not name a type
 2208 |     Mat_(const Mat_& m, const Range& rowRange, const Range& colRange=Range::all());
      |                                                      ^~~~~
 .....................
 .....................
In file included from /usr/local/include/opencv4/opencv2/core.hpp:3294,
                 from /usr/local/include/opencv4/opencv2/core/core.hpp:48,
                 from /home/mo/CLionProjects/DisjointForest/main.cpp:4:
/usr/local/include/opencv4/opencv2/core/optim.hpp:296:26: error: ‘InputArray’ was not declared in this scope; did you mean ‘OutputArray’?
  296 | CV_EXPORTS_W int solveLP(InputArray Func, InputArray Constr, OutputArray z);
      |                          ^~~~~~~~~~
      |                          OutputArray
/usr/local/include/opencv4/opencv2/core/optim.hpp:296:43: error: ‘InputArray’ was not declared in this scope; did you mean ‘OutputArray’?
  296 | CV_EXPORTS_W int solveLP(InputArray Func, InputArray Constr, OutputArray z);
      |                                           ^~~~~~~~~~
      |                                           OutputArray
/usr/local/include/opencv4/opencv2/core/optim.hpp:296:74: error: expected primary-expression before ‘z’
  296 | CV_EXPORTS_W int solveLP(InputArray Func, InputArray Constr, OutputArray z);
      |                                                                          ^
/usr/local/include/opencv4/opencv2/core/optim.hpp:296:75: error: expression list treated as compound expression in initializer [-fpermissive]
  296 | CV_EXPORTS_W int solveLP(InputArray Func, InputArray Constr, OutputArray z);
      |                                                                           ^
make[3]: *** [CMakeFiles/DisjointForest.dir/build.make:63: CMakeFiles/DisjointForest.dir/main.cpp.o] Error 1
make[2]: *** [CMakeFiles/Makefile2:76: CMakeFiles/DisjointForest.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/DisjointForest.dir/rule] Error 2
make: *** [Makefile:118: DisjointForest] Error 2

I have no idea what this error means. Could someone please guide me how I can fix this error and starting working with openCV?

If it helps, here's my cmake file:

cmake_minimum_required(VERSION 3.16)
project(DisjointForest)
set(CMAKE_CXX_STANDARD 20)
find_package(OpenCV 4.3.0 REQUIRED)

include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(DisjointForest main.cpp LinkedListDisjointSet.cpp DisjointForest.cpp DisjointForest.h)
target_link_directories(DisjointForest PUBLIC ${OpenCV_LIBS})


Solution

  • Okay so after a lot of research about linkers and linking libraries, I've realized the two problems:

    1) Changing c++ 20 to c++ 17 compiled the header files. Perhaps the library is not compatible with c++20 yet.
    2) Changing the above won't suffice as we have still not correctly linked the libraries. It will still give unresolved error. In my cmake file I need to replace the target_link_directories() with target_link_libraries(). This was a silly mistake I made while following the instructions from the links in the question.