簡體   English   中英

無法在 CLion 中編譯 OpenCV header 文件

[英]Can't compile OpenCV header files in CLion

我最近在我的 Ubuntu 系統上安裝了 OpenCV。 我使用此處此處的說明安裝 OpenCV c++。

當我在我的文件中包含 opencv header 時:

#include <opencv2/opencv.hpp>

它在構建時給了我以下錯誤:

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

我不知道這個錯誤是什么意思。 有人可以指導我如何解決此錯誤並開始使用 openCV 嗎?

如果有幫助,這是我的 cmake 文件:

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})

好的,在對鏈接器和鏈接庫進行了大量研究之后,我意識到了兩個問題:

1) 將 c++ 20 改為 c++ 17 編譯 header 文件。 也許該庫與 c++20 不兼容。
2) 更改上述內容還不夠,因為我們仍未正確鏈接庫。 它仍然會給出未解決的錯誤。 在我的 cmake 文件中,我需要將 target_link_directories target_link_directories()替換為 target_link_libraries target_link_libraries() 這是我在按照問題中鏈接的說明操作時犯的一個愚蠢的錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM