OpenCV hello world 没有像它应该的那样编译

发布于 2024-12-05 22:25:11 字数 1565 浏览 1 评论 0原文

我正在使用 OpenCV 2.3.1 设置一台新机器。机器是Windows 7盒子,我按照OpenCV网站给出的安装说明进行操作(使用CMake和MinGW来构建)。

这是我的代码:

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int main() {
    char var;
    cv::Mat img;
    img = cv::imread("C:/test/img.jpg");
    cv::namedWindow("Image");
    cv::imshow("Image", img);
    std::cin >> var;
    return 1;
}

这是我的 make 命令:

g++ -o main main.cpp -lopencv_core -lopencv_imgproc -lopencv_calib3d -lopencv_video -lopencv_features2d -lopencv_ml -lopencv_highgui -lopencv_objdetect -lopencv_contrib -lopencv_legacy

这是我的路径:

C:\OpenCV-2.3.1\install\bin;C:\OpenCV-2.3.1\install\include;C:\QtSDK\QtCreator\bin;C:\Program Files (x86)\MiKTeX 2.9\miktex\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\CMake 2.8\bin;C:\QtSDK\mingw\bin;

这是我的错误:

main.cpp:2:33: error: opencv2/core/core.hpp: No such file or directory
main.cpp:3:39: error: opencv2/highgui/highgui.hpp: No such file or directory
main.cpp: In function 'int main()':
main.cpp:8: error: 'cv' has not been declared
main.cpp:8: error: expected ';' before 'img'
main.cpp:9: error: 'img' was not declared in this scope
main.cpp:9: error: 'cv' has not been declared
main.cpp:10: error: 'cv' has not been declared
main.cpp:11: error: 'cv' has not been declared

这是没有意义的。为什么这不能编译?为什么找不到opencv2/core/core.hpp?

I am setting up a new machine with OpenCV 2.3.1. The machine is a Windows 7 box, and I followed the installation instructions given by the OpenCV website (used CMake with MinGW to build).

Here is my code:

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int main() {
    char var;
    cv::Mat img;
    img = cv::imread("C:/test/img.jpg");
    cv::namedWindow("Image");
    cv::imshow("Image", img);
    std::cin >> var;
    return 1;
}

Here is my make command:

g++ -o main main.cpp -lopencv_core -lopencv_imgproc -lopencv_calib3d -lopencv_video -lopencv_features2d -lopencv_ml -lopencv_highgui -lopencv_objdetect -lopencv_contrib -lopencv_legacy

Here is my path:

C:\OpenCV-2.3.1\install\bin;C:\OpenCV-2.3.1\install\include;C:\QtSDK\QtCreator\bin;C:\Program Files (x86)\MiKTeX 2.9\miktex\bin;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\CMake 2.8\bin;C:\QtSDK\mingw\bin;

Here is my error:

main.cpp:2:33: error: opencv2/core/core.hpp: No such file or directory
main.cpp:3:39: error: opencv2/highgui/highgui.hpp: No such file or directory
main.cpp: In function 'int main()':
main.cpp:8: error: 'cv' has not been declared
main.cpp:8: error: expected ';' before 'img'
main.cpp:9: error: 'img' was not declared in this scope
main.cpp:9: error: 'cv' has not been declared
main.cpp:10: error: 'cv' has not been declared
main.cpp:11: error: 'cv' has not been declared

This is not making sense. Why won't this compile? Why can't it find opencv2/core/core.hpp?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

灵芸 2024-12-12 22:25:11

在查找包含文件时,g++ 不考虑 %PATH%(Unix 上的 $PATH)。

在编译命令中添加以下内容:-IC:\OpenCV-2.3.1\install\include

g++ -IC:\OpenCV-2.3.1\install\include -o main main.cpp -lopencv_core ...

g++ doesn't consider %PATH% ($PATH on Unix) when looking for include files.

Add the following to the compilation command: -IC:\OpenCV-2.3.1\install\include:

g++ -IC:\OpenCV-2.3.1\install\include -o main main.cpp -lopencv_core ...
终难遇 2024-12-12 22:25:11

您没有包含正确的 OpenCV 目录。

我在 C:\OpenCV2.3 中安装了 OpenCV 的早期版本,这些是我必须为编译器添加以查找标头的路径:

C:\OpenCV2.3\build\include\opencv

C:\OpenCV2.3\build\include\opencv2

C:\OpenCV2.3\build\include

传统上,g++ 使用带有 -I 标志的标头目录,您似乎根本没有使用该标志。

You are not including the right OpenCV directories.

I made an installation of a previous version of OpenCV in C:\OpenCV2.3, and these are the paths I had to add for my compiler to find the headers:

C:\OpenCV2.3\build\include\opencv

C:\OpenCV2.3\build\include\opencv2

C:\OpenCV2.3\build\include

Traditionally, g++ takes headers directories with the -I flag, which you doesn't seem to be using at all.

盗梦空间 2024-12-12 22:25:11

在类似 UNIX 的操作系统上

     g++ filename.cpp -o exec `pkg-config opencv cvblob --libs --cflags`

(如果您要检查 blob,则 cvblob 是可选的)

on unix like os

     g++ filename.cpp -o exec `pkg-config opencv cvblob --libs --cflags`

(cvblob optional if you are checking for blob)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文