質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

Ubuntu

Ubuntuは、Debian GNU/Linuxを基盤としたフリーのオペレーティングシステムです。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

Q&A

解決済

1回答

19546閲覧

OpenCVのリンクエラー

chie_y

総合スコア20

OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

Ubuntu

Ubuntuは、Debian GNU/Linuxを基盤としたフリーのオペレーティングシステムです。

C++

C++はC言語をもとにしてつくられた最もよく使われるマルチパラダイムプログラミング言語の1つです。オブジェクト指向、ジェネリック、命令型など広く対応しており、多目的に使用されています。

0グッド

0クリップ

投稿2017/01/17 11:35

現在、Ubuntu上でOpenCVを使用したC++プログラムを作成しております。
しかし、下記のエラーが出てしまいます。

face_detection.cpp:(.text+0x50): `cv::CascadeClassifier::CascadeClassifier()' に対する定義されていない参照です face_detection.cpp:(.text+0x7d): `cv::CascadeClassifier::load(cv::String const&)' に対する定義されていない参照です face_detection.cpp:(.text+0xc2): `cv::VideoCapture::VideoCapture(int)' に対する定義されていない参照です face_detection.cpp:(.text+0xd1): `cv::VideoCapture::isOpened() const' に対する定義されていない参照です face_detection.cpp:(.text+0xe7): `cv::VideoCapture::operator>>(cv::Mat&)' に対する定義されていない参照です face_detection.cpp:(.text+0x14c): `cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)' に対する定義されていない参照です face_detection.cpp:(.text+0x1ff): `cv::CascadeClassifier::detectMultiScale(cv::_InputArray const&, std::vector<cv::Rect_<int>, std::allocator<cv::Rect_<int> > >&, double, int, int, cv::Size_<int>, cv::Size_<int>)' に対する定義されていない参照です face_detection.cpp:(.text+0x3a2): `cv::rectangle(cv::_InputOutputArray const&, cv::Point_<int>, cv::Point_<int>, cv::Scalar_<double> const&, int, int, int)' に対する定義されていない参照です face_detection.cpp:(.text+0x40f): `cv::imshow(cv::String const&, cv::_InputArray const&)' に対する定義されていない参照です face_detection.cpp:(.text+0x437): `cv::waitKey(int)' に対する定義されていない参照です face_detection.cpp:(.text+0x458): `cv::VideoCapture::~VideoCapture()' に対する定義されていない参照です face_detection.cpp:(.text+0x476): `cv::CascadeClassifier::~CascadeClassifier()' に対する定義されていない参照です face_detection.cpp:(.text+0x553): `cv::VideoCapture::~VideoCapture()' に対する定義されていない参照です face_detection.cpp:(.text+0x57b): `cv::CascadeClassifier::~CascadeClassifier()' に対する定義されていない参照です face_detection.o: 関数 `__static_initialization_and_destruction_0(int, int)' 内: face_detection.cpp:(.text+0x5e8): `std::ios_base::Init::Init()' に対する定義されていない参照です face_detection.cpp:(.text+0x5f7): `std::ios_base::Init::~Init()' に対する定義されていない参照です face_detection.o: 関数 `cv::String::String(char const*)' 内: face_detection.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x4d): `cv::String::allocate(unsigned long)' に対する定義されていない参照です face_detection.o: 関数 `cv::String::~String()' 内: face_detection.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): `cv::String::deallocate()' に対する定義されていない参照です face_detection.o: 関数 `cv::String::operator=(cv::String const&)' 内: face_detection.cpp:(.text._ZN2cv6StringaSERKS0_[_ZN2cv6StringaSERKS0_]+0x28): `cv::String::deallocate()' に対する定義されていない参照です face_detection.o: 関数 `cv::Mat::~Mat()' 内: face_detection.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): `cv::fastFree(void*)' に対する定義されていない参照です face_detection.o: 関数 `cv::Mat::release()' 内: face_detection.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x4b): `cv::Mat::deallocate()' に対する定義されていない参照です face_detection.o: 関数 `cvflann::anyimpl::operator<<(std::ostream&, cvflann::anyimpl::empty_any const&)' 内: face_detection.cpp:(.text._ZN7cvflann7anyimpllsERSoRKNS0_9empty_anyE[_ZN7cvflann7anyimpllsERSoRKNS0_9empty_anyE]+0x1d): `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' に対する定義されていない参照です face_detection.o: 関数 `cvflann::anyimpl::base_any_policy::~base_any_policy()' 内: face_detection.cpp:(.text._ZN7cvflann7anyimpl15base_any_policyD2Ev[_ZN7cvflann7anyimpl15base_any_policyD5Ev]+0x29): `operator delete(void*)' に対する定義されていない参照です face_detection.o: 関数 `cvflann::anyimpl::base_any_policy::~base_any_policy()' 内: face_detection.cpp:(.text._ZN7cvflann7anyimpl15base_any_policyD0Ev[_ZN7cvflann7anyimpl15base_any_policyD5Ev]+0x20): `operator delete(void*)' に対する定義されていない参照です face_detection.o: 関数 `cvflann::anyimpl::big_any_policy<cvflann::flann_centers_init_t>::print(std::ostream&, void* const*)' 内: face_detection.cpp:(.text._ZN7cvflann7anyimpl14big_any_policyINS_20flann_centers_init_tEE5printERSoPKPv[_ZN7cvflann7anyimpl14big_any_policyINS_20flann_centers_init_tEE5printERSoPKPv]+0x29): `std::ostream::operator<<(int)' に対する定義されていない参照です face_detection.o: 関数 `cvflann::anyimpl::big_any_policy<cvflann::flann_algorithm_t>::print(std::ostream&, void* const*)' 内: face_detection.cpp:(.text._ZN7cvflann7anyimpl14big_any_policyINS_17flann_algorithm_tEE5printERSoPKPv[_ZN7cvflann7anyimpl14big_any_policyINS_17flann_algorithm_tEE5printERSoPKPv]+0x29): `std::ostream::operator<<(int)' に対する定義されていない参照です face_detection.o: 関数 `cvflann::anyimpl::big_any_policy<cv::String>::print(std::ostream&, void* const*)' 内: face_detection.cpp:(.text._ZN7cvflann7anyimpl14big_any_policyIN2cv6StringEE5printERSoPKPv[_ZN7cvflann7anyimpl14big_any_policyIN2cv6StringEE5printERSoPKPv]+0x31): `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' に対する定義されていない参照です (以下、省略) collect2: error: ld returned 1 exit status Makefile:16: ターゲット 'face_detection' のレシピで失敗しました make: *** [face_detection] エラー 1

リンクがうまくいってないようなのですが、Makefileでライブラリは指定しています。

Makefile

1# Makefile 2# face_detection: face_detection.cpp 3# gcc -I /usr/local/include -o face_detection face_detection.cpp 4 5CC = gcc 6CFLAGS = -O4 -Wall -I/usr/local/include 7DEST = /usr/local/bin 8LDFLAGS = -L/usr/local/lib 9LIBS = -lhoge -lm 10OBJS = face_detection.o 11PROGRAM = face_detection 12 13all: $(PROGRAM) 14 15$(PROGRAM): $(OBJS) 16 # $(CC) $(OBJS) $(LDFLAGS) $(LIBS) -o $(PROGRAM) 17 $(CC) $(OBJS) $(LDFLAGS) -o $(PROGRAM) 18 19clean: rm -f *.o *~ $(PROGRAM) 20 21install: $(PROGRAM) 22 install -s $(PROGRAM) $(DEST) 23

console

1/usr/local/lib$ ls -al 2合計 44880 3drwxr-xr-x 5 root root 4096 1月 13 14:49 . 4drwxr-xr-x 10 root root 4096 4月 26 2016 .. 5lrwxrwxrwx 1 root root 22 1月 13 13:05 libopencv_world.so -> libopencv_world.so.3.2 6lrwxrwxrwx 1 root root 24 1月 13 13:05 libopencv_world.so.3.2 -> libopencv_world.so.3.2.0 7-rw-r--r-- 1 root root 45935816 1月 13 13:21 libopencv_world.so.3.2.0 8drwxr-xr-x 2 root root 4096 1月 13 13:09 pkgconfig 9drwxrwsr-x 4 root staff 4096 1月 12 23:18 python2.7 10drwxrwsr-x 3 root staff 4096 4月 26 2016 python3.5 11

ソースコードは以下のとおりです。

face_detection.cpp

1using namespace std; 2 3#include "opencv2/opencv.hpp" 4#include <vector> 5#include <stdio.h> 6 7using namespace cv; 8 9int main(int argc, char *argv[]) 10{ 11 Mat im, gray; // 変数宣言 12 // カスケード分類器の取得 13 CascadeClassifier cascade; 14 if (!cascade.load("haarcascade_frontalface_alt.xml")) return -1; 15 vector<Rect> faces; 16 VideoCapture cap(0); // カメラのキャプチャ 17 if (!cap.isOpened()) return -1; // キャプチャのエラー処理 18 19 while (1) { 20 cap >> im; // カメラ映像の取得 21 cvtColor(im, gray, CV_RGB2GRAY); // グレースケール変換 22 // カスケード分類器で顔の探索 23 cascade.detectMultiScale(gray, faces, 1.2, 2, CV_HAAR_SCALE_IMAGE, Size(50, 50)); 24 // 顔領域を矩形で囲む 25 vector<Rect>::const_iterator r = faces.begin(); 26 for (; r != faces.end(); ++r) { 27 rectangle(im, Point(r->x, r->y), 28 Point(r->x + r->width, r->y + r->height), Scalar(20, 20, 200), 3, CV_AA); 29 } 30 imshow("Camera", im); // 映像の表示 31 if (waitKey(30) >= 0) break; // キー入力があれば終了 32 } 33 return 0; 34} 35

何か根本的に設定がおかしいのでしょうか?
基本的なことで恐縮ですが、教えていただけると幸いです。・。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

osamuya

2017/01/17 11:55

外から見ているとなかなか難しいところなので、インストールの方法とどの時点でエラーが出るようになったのか書いていただけると、少しわかるかもです。
chie_y

2017/01/17 12:14

ご回答、ありがとうございます。インストールは、http://umejan.hatenablog.com/entry/2016/04/20/233642 を参考におこないました(ただ、バージョンは3.2.0です)エラーが出たのははじめからです。ちなみに、Pythonでは普通にOpenCVは使えています。
guest

回答1

0

ベストアンサー

Makefile を以下のように書き換えてください。

makefile

1# Makefile 2# face_detection: face_detection.cpp 3# gcc -I /usr/local/include -o face_detection face_detection.cpp 4 5CC = gcc 6CFLAGS = -O4 -Wall -I/usr/local/include 7DEST = /usr/local/bin 8LDFLAGS = -L/usr/local/lib 9LIBS = -lhoge -lm `pkg-config --libs opencv` 10OBJS = face_detection.o 11PROGRAM = face_detection 12 13all: $(PROGRAM) 14 15$(PROGRAM): $(OBJS) 16 $(CC) $(OBJS) $(LDFLAGS) $(LIBS) -o $(PROGRAM) 17 18clean: rm -f *.o *~ $(PROGRAM) 19 20install: $(PROGRAM) 21 install -s $(PROGRAM) $(DEST)

P.S.
しかし、このMakefileは微妙ですね。

投稿2017/01/17 12:23

MasashiKimura

総合スコア1150

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

chie_y

2017/01/17 12:31

ご回答、ありがとうございます。 Makefileを書き換えて実行したところ、以下のエラーが出るようになってしまいました。 gcc face_detection.o -L/usr/local/lib -lm `pkg-config --libs opencv` -o face_detection /usr/bin/ld: face_detection.o: シンボル '_ZTVN10__cxxabiv117__class_type_infoE@@CXXABI_1.3' への未定義参照です //usr/lib/x86_64-linux-gnu/libstdc++.so.6: error adding symbols: DSO missing from command line collect2: error: ld returned 1 exit status Makefile:12: ターゲット 'face_detection' のレシピで失敗しました make: *** [face_detection] エラー 1 なにか設定でおかしいところがあるのでしょうか? 引き続きよろしくお願いいたします。
MasashiKimura

2017/01/17 12:33

CC = gcc を CC = g++ としてください
chie_y

2017/01/17 14:22

無事、コンパイルして実行ファイルができました。 ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問