"对#34;未定义的引用当使用opencv时

时间:2018-02-23 02:18:02

标签: c++ opencv

我正在使用opencv实现一个简单的项目。但我在编译时得到错误未定义的引用。这是world.cpp :(只是错误发生的部分)

#include "world.h"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <string>
#include <string.h>
#include <sstream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
using namespace std;
using namespace cv;

#define SSTR( x ) static_cast< std::ostringstream & >( \
    ( std::ostringstream() << std::dec << x ) ).str()

World::World()
{
map = new Map;
agent = new Agent(map->GetTheState(0));
ExploringRate = 10000;
}

 void World::Monitor()
{
 Mat myMat(500,500,CV_8UC3,Scalar(0));
 for(int i = 0;i<100;i++)
 {
     string s = SSTR(map->GetTheState(i)->GetValue());
     int k = i/10;
     int j = i%10;
     if(map->GetTheState(i)->GetValue()>0)
         rectangle(myMat,Point(j*50,k*50),Point((j+1)*50,(k+1)*50),Scalar(0,255*map->GetTheState(i)->GetValue(),0),-1,CV_AA);
     else
         rectangle(myMat,Point(j*50,k*50),Point((j+1)*50,(k+1)*50),Scalar(-255*0.5,0,0),-1,CV_AA);
     putText(myMat,s,Point(j*50 +10,k*50 + 25),0,0.40,Scalar(255,255,255),1,0,false);
 }
 int k = agent->GetCurrentStateNumber()/10 , j = agent->GetCurrentStateNumber()%10;
 circle(myMat,Point(j*50 + 20,k*50 + 20),5,Scalar(0,0,255),+20,0);
 imshow("mymat",myMat);
 cvWaitKey(1);
}

这是我的Makefile:

CC = g++
CFLAGS = -g -Wall

OPENCV = `pkg-config opencv --cflags --libs `
LIBS = $(OPENCV)
BOOST= -I./ -L /usr/lib/x86_64-linux-gnu  -pthread
BOOSTFLAG= -lrt


all: main.o state.o agent.o map.o world.o
    $(CC) main.o state.o agent.o map.o world.o -o world

main.o: main.cpp world.h
    $(CC) -c main.cpp $(BOOST) $(LIBS) $(BOOSTFLAG)

state.o: state.cpp state.h 
    $(CC) -c state.cpp $(BOOST) $(LIBS) $(BOOSTFLAG)


agent.o: agent.cpp agent.h
    $(CC) -c agent.cpp $(BOOST) $(LIBS) $(BOOSTFLAG)

map.o: map.cpp map.h state.h
    $(CC) -c map.cpp $(BOOST) $(LIBS) $(BOOSTFLAG)

world.o: world.cpp world.h
    $(CC) -c world.cpp $(BOOST) $(LIBS) $(BOOSTFLAG)

当我想要它,我得到这个错误:

g++ main.o state.o agent.o map.o world.o -o world
world.o: In function `World::Monitor()':
world.cpp:(.text+0x316): undefined reference to `cv::rectangle(cv::_InputOutputArray const&, cv::Point_<int>, cv::Point_<int>, cv::Scalar_<double> const&, int, int, int)'
world.cpp:(.text+0x3fd): undefined reference to `cv::rectangle(cv::_InputOutputArray const&, cv::Point_<int>, cv::Point_<int>, cv::Scalar_<double> const&, int, int, int)'
world.cpp:(.text+0x4fe): undefined reference to `cv::putText(cv::_InputOutputArray const&, cv::String const&, cv::Point_<int>, int, double, cv::Scalar_<double>, int, int, bool)'
world.cpp:(.text+0x654): undefined reference to `cv::circle(cv::_InputOutputArray const&, cv::Point_<int>, int, cv::Scalar_<double> const&, int, int, int)'
world.cpp:(.text+0x6ad): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
world.cpp:(.text+0x6d5): undefined reference to `cvWaitKey'
world.o: In function `cv::String::String(char const*)':
world.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x4d): undefined reference to `cv::String::allocate(unsigned long)'
world.o: In function `cv::String::~String()':
world.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
world.o: In function `cv::Mat::Mat(int, int, int, cv::Scalar_<double> const&)':
world.cpp:(.text._ZN2cv3MatC2EiiiRKNS_7Scalar_IdEE[_ZN2cv3MatC5EiiiRKNS_7Scalar_IdEE]+0xdb): undefined reference to `cv::Mat::operator=(cv::Scalar_<double> const&)'
world.o: In function `cv::Mat::~Mat()':
world.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
world.o: In function `cv::Mat::create(int, int, int)':
world.cpp:(.text._ZN2cv3Mat6createEiii[_ZN2cv3Mat6createEiii]+0x9d): undefined reference to `cv::Mat::create(int, int const*, int)'
world.o: In function `cv::Mat::release()':
world.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x4b): undefined reference to `cv::Mat::deallocate()'
world.o: In function `cv::String::String(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
world.cpp:(.text._ZN2cv6StringC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN2cv6StringC5ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x69): undefined reference to `cv::String::allocate(unsigned long)'
collect2: error: ld returned 1 exit status
Makefile:11: recipe for target 'all' failed
make: *** [all] Error 1

我已经从源文件安装了opencv并且已经尝试了

sudo ldconfig

我真的需要帮助

0 个答案:

没有答案