简体   繁体   English

无法使用cmake在OSX上使用OpenCV

[英]Can't get OpenCV to work on OSX using cmake

I am trying to compile the following sample code: 我正在尝试编译以下示例代码:

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;

int main(int argc, char* argv[]) {
    cout << "Using OpenCV version " << CV_VERSION << endl;
    Point2f *p = new Point2f(1, 2);
    cout << p << endl;
    cout << *p << endl;
    string filename = "test";
    Mat A;
    imshow("asd", A);
    return 0;
}

I am using the following CMakeLists.txt: 我正在使用以下CMakeLists.txt:

cmake_minimum_required (VERSION 2.8)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} /FindOpenCV.cmake)
find_package(OpenCV)
message(STATUS "OpenCV_FOUND: " ${OpenCV_FOUND})

include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(main main.cpp)

target_link_libraries(main ${OpenCV_LIBS})

And this is what I get: 这就是我得到的:

-- OpenCV_FOUND: 1
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/daviderler/Desktop/new/src/test
Scanning dependencies of target main
[100%] Building CXX object CMakeFiles/main.dir/main.cpp.o
Linking CXX executable main
Undefined symbols for architecture x86_64:
  "cv::imshow(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, cv::_InputArray const&)", referenced from:
_main in main.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]: *** [main] Error 1
make[1]: *** [CMakeFiles/main.dir/all] Error 2
make: *** [all] Error 2

I do not understand why imread cannot be linked, the Point2f stuff only compiles just fine... For example the line: 我不明白为什么无法链接imread,Point2f的东西只能编译就好了。例如,以下行:

Mat A;
findFundamentalMat(A, A, 1 ,2.0);

gets compiled. 得到编译。

I basically followed this: http://docs.opencv.org/trunk/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html 我基本上遵循了这一点: http : //docs.opencv.org/trunk/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html

So I tried to compile the code by hand, and this line seems to work: 因此,我尝试手动编译代码,这一行似乎行得通:

clang++ -mmacosx-version-min=10.7 -I/usr/local/include/opencv2 -I/opt/local/include main.cpp -lopencv_core -lopencv_highgui

So the only difference is the -mmacosx-version-min=10.7 flag... but I dont't know how to get it all the way to the compiler-call... :/ 所以唯一的区别是-mmacosx-version-min = 10.7标志...但是我不知道如何一直将其传递给编译器调用...:/

So this little line did the trick... 所以这条小线起到了作用...

set(CMAKE_CXX_FLAGS "-mmacosx-version-min=10.7")

Though I would appreciate an explanation. 虽然我希望得到一个解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM