Ubuntu18.0下编译opencv c++并配置clion环境

预编译阶段

先安装一些依赖

sudo apt-get install pkg-config
sudo apt-get install libgtk2.0-dev

编译opencv

官方教程

配置clion

由于编译后的opencv2在/usr/local/include/opencv4里,所以我们要在cmakelist里添加包含路径和库路径

include_directories(/usr/local/include/opencv4)
link_directories(/usr/local/lib)
link_libraries(opencv_core)
link_libraries(opencv_imgcodecs)
link_libraries(opencv_highgui)

测试

#include 
#include 
#include 
#include 
int main() {
    cv::Mat img;
    img = cv::imread("/home/laplus/Downloads/1.jpg");
    cv::imshow("img",img);
    cv::waitKey(0);
    return 0;
}

当中可能遇到的问题

  • opencv2/core.hpp: No such file or directory
    解决方案:在cmakelist里添加
 include_directories(/usr/local/include/opencv4)
  • undefined reference to `cv::Mat::Mat()’
    解决方案:在cmakelist里添加
	link_directories(/usr/local/lib)
	link_libraries(opencv_core)
	link_libraries(opencv_imgcodecs)
	link_libraries(opencv_highgui)
  • If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function ‘cvNamedWindow’
    解决方案:安装依赖后,重新编译源码即可

你可能感兴趣的:(环境配置)