一开始很着急,以为是Open CV没有配置好,少了什么库(明天晚上是DDL……)
所有和OpenCV相关的函数都调用错误。
编译器输出:
[ 50%] Linking CXX executable untitled9
Undefined symbols for architecture x86_64:
"cv::Mat::deallocate()", referenced from:
cv::Mat::release() in main.cpp.o
"cv::String::deallocate()", referenced from:
cv::String::~String() in main.cpp.o
"cv::String::allocate(unsigned long)", referenced from:
cv::String::String(char const*) in main.cpp.o
"cv::imread(cv::String const&, int)", referenced from:
_main in main.cpp.o
"cv::imshow(cv::String const&, cv::_InputArray const&)", referenced from:
_main in main.cpp.o
"cv::fastFree(void*)", referenced from:
cv::Mat::~Mat() 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[3]: *** [untitled9] Error 1
make[2]: *** [CMakeFiles/untitled9.dir/all] Error 2
make[1]: *** [CMakeFiles/untitled9.dir/rule] Error 2
make: *** [untitled9] Error 2
试了之前可以通过的几个程序,发现都跑不了(这几个程序第一次运行成功都是在同一个工程下面跑的,所以后面要解释)
然后测试了一下第一个程序Untitled1,发现居然正常运行!然后打开Untitled6,发现代码和Untitled1居然一模一样,但是出现了上述一模一样的链接错误信息!说明不是库配置的问题。
想了一下,问题出在CLion的CMakeLists.txt上面:第一次配置CLion的时候是用Untitled1配置的,修改的也只是这个工程的CMakeLists.txt;后续程序测试也是拿第一个工程运行的,所以一直没有发现链接错误;
但是新建工程的时候忘了配置CMakeLists.txt,所以自然就库链接错误了
附上可以配置好的CMakeLists.txt(参考了一些博客,如有相同请联系,我将注明作者)
cmake_minimum_required(VERSION 3.9)
project(untitled1)
set(CMAKE_CXX_STANDARD 11)
#find_library(OpenCV)
find_package(OpenCV)
include_directories(${OpenCV_INCLUDE_DIRS})
set(CMAKE_CXX_STANDARD 11)
add_executable(untitled1 main.cpp)
target_link_libraries(untitled1 ${OpenCV_LIBS})
然后把这个CMakeLists用到其他工程里面时,所有Untitled1改成对应工程的名字就好了