c++ - 使用 OpenCV header 编译 C++ 文件时没有此类文件或目录错误

标签 c++ opencv include include-path path-variables

我使用的是 Red Hat Linux。我在 C++ 文件中的包含方面遇到了一些(可能是新手)问题。我创建了以下简单的 OpenCV 脚本,

#include "opencv2/highgui/highgui.hpp"
using namespace cv;

int main(int argc, char ** argv){
    Mat img = imread( argv[1], -1 );
    if ( img.empty() ) return -1;
    namedWindow( "Example1", cv::WINDOW_AUTOSIZE );
    imshow( "Example1", img );
    waitKey( 0 );
    destroyWindow( "Example1" );
}

然后在我输入的终端

g++ my_simple_script.cpp

并出现错误

newfile.cpp:1:39: error: opencv2/highgui/highgui.hpp: No such file or directory
newfile.cpp:3: error: 'cv' is not a namespace-name
newfile.cpp:3: error: expected namespace-name before ';' token
newfile.cpp: In function 'int main(int, char**)':
newfile.cpp:6: error: 'Mat' was not declared in this scope
newfile.cpp:6: error: expected ';' before 'img'
newfile.cpp:7: error: 'img' was not declared in this scope
newfile.cpp:8: error: 'cv' has not been declared
newfile.cpp:8: error: 'namedWindow' was not declared in this scope
newfile.cpp:9: error: 'img' was not declared in this scope
newfile.cpp:9: error: 'imshow' was not declared in this scope
newfile.cpp:10: error: 'waitKey' was not declared in this scope
newfile.cpp:11: error: 'destroyWindow' was not declared in this scope

我添加了

/home/m/maxwell9/2.4.3/include

到我的 PATH,其中 2.4.3 表示我正在使用的 OpenCV 版本。 当我输入时

echo $PATH

我明白了

/opt/apps/jdk1.6.0_22.x64/bin:/apps/smlnj/110.74/bin:/usr/local/cuda/bin:/sbin:/bin:/usr/sbin:/usr/bin:/apps/weka/3.7.12:/home/m/maxwell9/bin:/home/m/maxwell9/2.4.3/include

我确认有一个文件

/home/m/maxwell9/2.4.3/include/opencv2/highgui/highgui.hpp

最佳答案

仅添加包含路径只能解决您的编译问题。您仍然会看到链接器错误..(添加包含路径的正确方法是使用 -I 标志,PATH 不用于此..)

要成功编译和链接程序,您需要指定头文件的包含路径和预编译 OpenCV 库的链接器路径以及要链接的库列表...

  1. 标准方式,使用以下顺序将 openCV 安装到标准安装目录

     sudo make install (from your OpenCV build library)
     echo '/usr/local/lib' | sudo tee -a /etc/ld.so.conf.d/opencv.conf
     sudo ldconfig
     printf '# OpenCV\nPKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig\nexport PKG_CONFIG_PATH\n' >> ~/.bashrc  
     source ~/.bashrc  
    

以下内容将为您成功编译并链接您的程序:

g++ my_simple_script.cpp `pkg-config --libs opencv` `pkg-config --cflags opencv`
  • 但显然您还没有这样做..因为您试图指向非标准包含路径。因此,在您的情况下,您需要使用 -I 显式指定包含路径标志和您的预编译库路径 -L使用 -l<name_of_library> 标记并列出您可能想要使用的所有单个库

    g++ my_simple_script.cpp -I /home/m/maxwell9/2.4.3/include -L /home/m/maxwell9/2.4.3/<your build directory name>/lib/ -lopencv_core
    
  • (您可能需要的其他 openCV 库列表必须使用格式附加到上述命令中: -l<name of the lib you need> )

    关于c++ - 使用 OpenCV header 编译 C++ 文件时没有此类文件或目录错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34821672/

    相关文章:

    c++ - 条件变量中 wait_for 函数的用途 - C++11

    c++ - 为一些抽象的 Windows 服务器创建 C++ 客户端应用程序 - 如何管理与服务器速度的 TCP 连接?

    opencv - 如何在 Open CV LibSVM 中缩放数据

    python - Opencv连接最近的矩形轮廓

    C - 我应该使用引号还是括号将标题包含在单独的目录中

    c++ - 库中的引号是否包含 C++ 中的路径意味着项目的基本目录?

    c++ - 是否可以使用 TensorFlow 训练自己的模块,然后在 OpenCV 中使用它进行检测?

    include - Zk 如何通过 id 访问包含的 .zul 页面组件?

    java - Android: Image Processing Library(替代openCV)

    php - 使用 PHP include 时改变 CSS sprite 导航栏的状态