1

I have a .cpp file with opencv code . Things compile when I do: g++ my_file.cpp.

My ~/.zshrc: alias g++='g++ $(pkg-config --cflags --libs /usr/local/opt/opencv@2/lib/pkgconfig/opencv.pc)'

I want to harness Clion's autocomplete features; as such I need to tell it where my opencv libs are located through the CMakelist.

Currently, it looks like this:

cmake_minimum_required(VERSION 3.14)
project(my_project)

set(CMAKE_CXX_STANDARD 14)

add_executable(my_project main.cpp)

How do I add opencv as a dependency in my CMakelist in the simplest way possible?

nz_21
  • 6,140
  • 7
  • 34
  • 80
  • There are **tons** of questions on Stack Overflow about using OpenCV with CMake. Almost every of them uses `find_package(OpenCV)`. Have you checked these questions? E.g. this one: https://stackoverflow.com/questions/13970377/configuring-an-c-opencv-project-with-cmake – Tsyvarev Nov 11 '19 at 07:49

1 Answers1

0

https://docs.opencv.org/2.4/doc/tutorials/introduction/linux_gcc_cmake/linux_gcc_cmake.html

Key portions: to find OpenCV and set OpenCV-related build variables

find_package( OpenCV REQUIRED )

and to link a build target to OpenCV

target_link_libraries( DisplayImage ${OpenCV_LIBS} )

This assumes OpenCV's headers and libraries are in the default search path. Easiest way of ensuring that on Linux is with the system package manager (sudo apt install opencv libopencv-dev or dnf install opencv opencv-devel). You can also look at dependency managers like vcpkg or conan for more portable solutions. (You can always build it yourself but that's rarely the easiest way for a popular library).

parktomatomi
  • 3,851
  • 1
  • 14
  • 18