CMakeでビルド時にOpenCVのライブラリが見つからない(undefined reference to)と怒られます。
CMakeLists.txt
ではinclude_directories(/usr/local/include/opencv4)
としているはずですが、
他に追加せねばならないことがあれば教えてください。
環境はUbuntu18.04です。
CMakeLists.txt
cmake
1cmake_minimum_required(VERSION 2.8) 2 3# Set the project name 4project (sample CXX) 5 6include_directories(/usr/local/include/opencv4) 7 8# Add an executable 9add_executable(sample sample.cc)
sample.cc
c++
1#include <opencv2/opencv.hpp> 2#include <iostream> 3 4int main(int argc, const char* argv[]) 5{ 6 cv::Mat img = cv::imread( argv[1], -1); 7 if( img.empty() ) return -1; 8 cv::namedWindow( "Example", cv::WINDOW_AUTOSIZE); 9 cv::imshow( "Example !", img ); 10 cv::waitKey(0); 11 cv::destroyWindow("Example !!!"); 12 13 return 0; 14} 15
ビルド時コマンド
bash
1mkdir build && cd build 2cmake ../ 3make
エラー結果
$ make [ 50%] Linking CXX executable sample CMakeFiles/sample.dir/sample.cc.o: In function `main': sample.cc:(.text+0x75): undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)' sample.cc:(.text+0x9f): undefined reference to `cv::Mat::empty() const' sample.cc:(.text+0xed): undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)' sample.cc:(.text+0x166): undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)' sample.cc:(.text+0x19d): undefined reference to `cv::waitKey(int)' sample.cc:(.text+0x1d8): undefined reference to `cv::destroyWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)' sample.cc:(.text+0x207): undefined reference to `cv::Mat::~Mat()' sample.cc:(.text+0x2e8): undefined reference to `cv::Mat::~Mat()' collect2: error: ld returned 1 exit status CMakeFiles/sample.dir/build.make:94: recipe for target 'sample' failed make[2]: *** [sample] Error 1 CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/sample.dir/all' failed make[1]: *** [CMakeFiles/sample.dir/all] Error 2 Makefile:83: recipe for target 'all' failed make: *** [all] Error 2
回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。