Search code examples
c++linuxopencvpkg-config

How do I add OpenCV to LD_LIBRARY path in linux?


I used this link to install OpenCV.

What works:

1.OpenCV works fine with python (running from terminal).

2.I can import opencv libraries in a single C++ program.

What does not work :

When the code is spread across multiple and you need to build it using CMake.

Here's my CmakeLists.txt :

1.cmake_minimum_required(VERSION 3.9)

2.project(Image_processing)

3.set(CMAKE_CXX_STANDARD 14)

4.find_package(OpenCV REQUIRED)

5.include_directories(/home/user/opencv/build)

6.add_executable(main main.cpp)

7.target_link_libraries(project_name ${OpenCV_LIBS})

Errors (can regenerate them by commenting lines 4,5 and 7 in above CMake file):

undefined reference to OpenCV functions.

CMake Error at CMakeLists.txt:7 (target_link_libraries): Cannot specify link libraries for target "Image_processing" which is not built by this project.


Solution

  • Correct it with:

    cmake_minimum_required(VERSION 3.5)
    
    project(Image_processing)
    
    set(CMAKE_CXX_STANDARD 14)
    
    find_package(OpenCV REQUIRED)
    
    include_directories(${OpenCV_INCLUDE_DIRS})
    
    add_executable(main main.cpp)
    
    target_link_libraries(main ${OpenCV_LIBS})