我正在使用OpenCV开发一些计算机视觉代码,并希望通过使用CMake编译OpenCV教程代码来尝试运行OpenCV。当我尝试编译并运行基本的display_image程序时,它已成功编译并运行(我添加了一个CMakeLists.txt文件并运行了cmake。然后在终端中生成。但是,当我应用相同的程序运行其他一些程序时,它不起作用。
我收到以下错误消息
$ cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/censam/OpenCV/Vision Code/Examples/grabcut
$ make
Linking CXX executable grabcut
Undefined symbols for architecture x86_64:
"cv::imread(std::__1::basic_string<char, std::__1::char_traits<char>, >
std::__1::allocator<char> > const&, int)", referenced from:
_main in grabcut.cpp.o
"cv::imshow(std::__1::basic_string<char, std::__1::char_traits<char>, >
std::__1::allocator<char> > const&, cv::_InputArray const&)", referenced from:
GCApplication::showImage() const in grabcut.cpp.o
"cv::Exception::Exception(int, std::__1::basic_string<char, std::__1::char_traits<char>, >
std::__1::allocator<char> > const&, std::__1::basic_string<char, >
std::__1::char_traits<char>, std::__1::allocator<char> > const&, >
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >
const&, int)", referenced from:
getBinMask(cv::Mat const&, cv::Mat&) in grabcut.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [grabcut] Error 1
make[1]: *** [CMakeFiles/grabcut.dir/all] Error 2
make: *** [all] Error 2
我的CMakeLists.txt文件如下:
cmake_minimum_required(VERSION 2.8)
project( grabcut )
find_package( OpenCV REQUIRED )
add_executable( grabcut grabcut.cpp )
target_link_libraries( grabcut ${OpenCV_LIBS} )
我可以知道为什么会这样吗?我怀疑这可能是因为CMake在编译期间没有链接所需的库。当我尝试使用XCode时,我得到了相同的结果,但我更喜欢使用CMake,以便我可以更轻松地将代码移植到Linux系统。
我发现以下链接Linking OpenCV 2.3 program in Mac OS X Lion: symbol(s) not found for architecture x86_64也遇到了同样的问题,但问题似乎没有得到解决。
我将非常感谢有关如何在OSX Mavericks中使用CMake编译和运行程序的任何帮助。