How to include non Vcpkg on CMakeLists.txt?

2 min read 04-09-2024
How to include non Vcpkg on CMakeLists.txt?


Beyond Vcpkg: Using External OpenCV Libraries with CMake

This article delves into the process of integrating an external OpenCV library into your CMake project, bypassing the commonly used Vcpkg package manager. We'll explore the steps involved and provide practical examples to guide you through the process.

Understanding the Issue

The provided scenario highlights a common challenge when working with pre-built external libraries. CMake's find_package command relies on conventions to locate and configure libraries. Vcpkg, with its pre-configured package definitions, simplifies the process. However, when using an external OpenCV installation, you need to provide CMake with the necessary information for proper detection and configuration.

Solution: Manual Configuration

The key lies in manually instructing CMake about the location of your external OpenCV installation. We can accomplish this by leveraging the following approaches:

  1. Directly Specifying Paths

    • Set the OpenCV_DIR variable: This approach involves explicitly pointing CMake to the directory containing the OpenCVConfig.cmake file.
    cmake_minimum_required(VERSION 3.19.1)
    project(mylib)
    
    set (CMAKE_CXX_STANDARD 14)
    
    # Specify the OpenCV installation path
    set(OpenCV_DIR "C:/opencv/build")
    
    find_package(OpenCV REQUIRED) 
    
    include_directories(${OpenCV_INCLUDE_DIRS})
    link_libraries(${OpenCV_LIBS})
    
    set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE)
    
    add_library(mylib SHARED mylib.cpp another_lib.cpp)
    
    • This method offers direct control, ensuring CMake finds your desired OpenCV installation.
  2. Environment Variables

    • Utilize the CMAKE_PREFIX_PATH environment variable: This variable acts as a global search path for CMake modules.
    # Set the environment variable before running CMake
    set CMAKE_PREFIX_PATH="C:/opencv/build"
    
    • This approach allows you to modify the search path without directly modifying the CMakeLists.txt file.

Best Practices:

  • Consistency: Maintain consistent paths throughout your project to avoid potential issues.
  • Configuration Files: Familiarize yourself with the contents of OpenCVConfig.cmake to understand the variables it defines and how they influence the build process.
  • Version Control: Employ version control systems to track changes and facilitate collaborative development.

Example:

Let's assume you have downloaded OpenCV and extracted it to C:\opencv. Now, you need to build OpenCV using CMake. Follow these steps:

  1. Create a build directory:

    mkdir C:\opencv\build
    cd C:\opencv\build
    
  2. Configure OpenCV using CMake:

    cmake -G "Visual Studio 16 2019" ..
    
  3. Build OpenCV:

    cmake --build . --config Release
    
  4. Set OpenCV_DIR in your CMakeLists.txt:

    set(OpenCV_DIR "C:/opencv/build")
    

Now, when you run CMake for your project, it will locate the OpenCV installation in C:\opencv\build and use it instead of the Vcpkg-provided OpenCV.

Conclusion:

By manually configuring CMake with the path to your external OpenCV installation, you gain greater control over the build process. This approach provides flexibility and allows you to utilize specific OpenCV versions or custom configurations. Remember to use best practices and ensure consistency in your environment variables and path settings.