找不到文件main.obj

时间:2013-04-26 06:21:55

标签: c++ visual-studio-2010 qt opencv environment-variables

我的PC上安装了QT,使用的是Visual Studio 2010编译器

enter image description here

我用一个简单的hello world应用程序测试了它,它运行正常。

但是现在我正在尝试将OpenCV与QT一起使用。我已经安装了Visual Studio 2010 Express和专业版,opencv在那里工作得很好。但是,当我尝试在QT中运行OpenCV时,我收到以下错误

main.obj:-1: error: LNK2019: unresolved external symbol "public: void __thiscall cv::Mat::deallocate(void)" (?deallocate@Mat@cv@@QAEXXZ) referenced in function "public: void __thiscall cv::Mat::release(void)" (?release@Mat@cv@@QAEXXZ)

main.obj:-1: error: LNK2019: unresolved external symbol "void __cdecl cv::fastFree(void *)" (?fastFree@cv@@YAXPAX@Z) referenced in function "public: __thiscall cv::Mat::~Mat(void)" (??1Mat@cv@@QAE@XZ)

main.obj:-1: error: LNK2019: unresolved external symbol "int __cdecl cv::waitKey(int)" (?waitKey@cv@@YAHH@Z) referenced in function _main

    main.obj:-1: error: LNK2019: unresolved external symbol "void __cdecl cv::imshow(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class cv::_InputArray const &)" (?imshow@cv@@YAXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@ABV_InputArray@1@@Z) referenced in function _main

    main.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall cv::_InputArray::_InputArray(class cv::Mat const &)" (??0_InputArray@cv@@QAE@ABVMat@1@@Z) referenced in function _main

    main.obj:-1: error: LNK2019: unresolved external symbol "void __cdecl cv::namedWindow(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int)" (?namedWindow@cv@@YAXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@Z) referenced in function _main

    main.obj:-1: error: LNK2019: unresolved external symbol "class cv::Mat __cdecl cv::imread(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,int)" (?imread@cv@@YA?AVMat@1@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@H@Z) referenced in function _main

    release\FirstProject.exe:-1: error: LNK1120: 7 unresolved externals

以下是我的 FirstProject.pro 文件的内容

#-------------------------------------------------
#
# Project created by QtCreator 2013-04-26T11:15:33
#
#-------------------------------------------------

QT       += core

QT       -= gui

TARGET = FirstProject
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp

INCLUDEPATH += C:\opencv\build\include\
LIBS += -LC:\opencv\build\x86\vc10\lib \
-lopencv_calib3d240d.lib\
-lopencv_contrib240d.lib\
-lopencv_core240d.lib\
-lopencv_features2d240d.lib\
-lopencv_flann240d.lib\
-lopencv_gpu240d.lib\
-lopencv_haartraining_engined.lib\
-lopencv_highgui240d.lib\
-lopencv_imgproc240d.lib\
-lopencv_legacy240d.lib\
-lopencv_ml240d.lib\
-lopencv_nonfree240d.lib\
-lopencv_objdetect240d.lib\
-lopencv_photo240d.lib\
-lopencv_stitching240d.lib\
-lopencv_ts240d.lib\
-lopencv_video240d.lib\
-lopencv_videostab240d.lib

我尝试过追随以下FirstProject.pro内容,但一点也不好

#-------------------------------------------------
#
# Project created by QtCreator 2013-04-26T11:15:33
#
#-------------------------------------------------

QT       += core

QT       -= gui

TARGET = FirstProject
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += main.cpp

INCLUDEPATH += C:\opencv\build\include\
LIBS += -LC:\opencv\build\x86\vc10\lib \
-lopencv_core240 \
-lopencv_highgui240 \
-lopencv_imgproc240 \
-lopencv_features2d240 \
-lopencv_calib3d240

以下是我的代码

的main.cpp

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

using namespace std;
using namespace cv;

int main()
{

    Mat image = imread("D:/Tulips.jpg");

    if(!image.data)
    {
        cout << "Error loading image" << endl;
    }
    else
    {
        namedWindow("Image");
        imshow("Image",image);
    }

    waitKey(0);
    return 0;

}

为什么我收到此错误?请帮我摆脱这个

3 个答案:

答案 0 :(得分:2)

QT项目文件尝试统一处理文件/目录分隔符,即转发v的反斜杠。无论使用哪种都无关紧要。但是,也许正在进行一些翻译。

尝试使用正向版本'/'替换所有反斜杠'\'。 Qmake会适当地转换它们。

替换为-LC:/opencv/build/x86/vc10/lib

这也可能是你的INCLUDEPATH的最后反斜杠问题。也许它正在加入下一行?

答案 1 :(得分:2)

我找到了答案。解决问题的唯一方法是逐个添加库,并使用完整路径

LIBS += C:/opencv/build/x86/vc9/lib/opencv_calib3d240.lib
LIBS += C:/opencv/build/x86/vc9/lib/opencv_contrib240.lib

答案 2 :(得分:1)

我遇到了同样的问题,但是却找不到答案。经过8天的尝试修复后,我终于找到了答案,并且想与您分享。

当您使用静态链接opencv时,首先检查您的库是否真的是静态链接,为此,请打开开发人员命令提示符msvc,导航到您的库路径,然后键入以下命令:

lib /LIBS opencv_world.lib

除了opencv_world.lib之外,您还可以放置其他任何库名。

然后检查其中仅包含已刷新的.obj文件,因此这意味着您可以静态使用此库。 然后记住当您在opencv中使用静态库时,您需要在其他一些库中进行动态链接中不需要的项目。这些库位于您的库目录中,其名称不以opencv开头。 您可以在下面看到其中的一些:

IlmImf.lib
ippicvmt.lib
ippiw.lib
ittnotify.lib
libjasper.lib
libtiff.lib
libwebp.lib
zlib.lib

记住,如果出现错误“无法找到库”,请使用确切的路径添加它。例如:

LIBS += E:\opencv\build-x86-static\install\x86\vc15\staticlib\zlib.lib