Skip to content

Instantly share code, notes, and snippets.

@sonygod
Forked from UnaNancyOwen/CMakeLists.txt
Created October 17, 2021 05:02
Show Gist options
  • Save sonygod/1497c402b12fac8fcdca126c94dbaa26 to your computer and use it in GitHub Desktop.
Save sonygod/1497c402b12fac8fcdca126c94dbaa26 to your computer and use it in GitHub Desktop.
CMakeLists for OpenCV that installed using Vcpkg
cmake_minimum_required( VERSION 3.0 )
set( CMAKE_TOOLCHAIN_FILE "C:/vcpkg/scripts/buildsystems/vcpkg.cmake" )
# Create Project
project( solution )
add_executable( project main.cpp )
# Set OpenCVConfig.cmake Search Directory
set( OpenCV_DIR )
if( NOT CMAKE_CL_64 )
set( OpenCV_DIR "C:/vcpkg/installed/x86-windows/share/opencv" )
else()
set( OpenCV_DIR "C:/vcpkg/installed/x64-windows/share/opencv" )
endif()
# Find Package
find_package( OpenCV REQUIRED )
if( OpenCV_FOUND )
# Additional Include Directories
include_directories( ${OpenCV_INCLUDE_DIRS} )
# Additional Library Directories
link_directories( ${OpenCV_LIB_DIR} )
# Additional Dependencies
target_link_libraries( project ${OpenCV_LIBS} )
endif()

How to Install OpenCV using Vcpkg

Command

  • x86
    vcpkg install opencv:x86-windows
    
  • x64
    vcpkg install opencv:x64-windows
    
@sonygod
Copy link
Author

sonygod commented Oct 17, 2021

cmake_minimum_required(VERSION 3.0.0)
project(testopencv VERSION 0.1.0)

include(CTest)
enable_testing()

add_executable(testopencv main.cpp)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
set(CMAKE_TOOLCHAIN_FILE "C:/toos/vcpkg/scripts/buildsystems/vcpkg.cmake")
set( OpenCV_DIR )
set( OpenCV_DIR "C:/toos/vcpkg/installed/x64-windows/share/opencv" )

# Find Package
find_package( OpenCV REQUIRED )

if( OpenCV_FOUND )
  # Additional Include Directories
  include_directories( ${OpenCV_INCLUDE_DIRS} )

  # Additional Library Directories
  link_directories( ${OpenCV_LIB_DIR} )

  # Additional Dependencies
  target_link_libraries( testopencv ${OpenCV_LIBS} )
endif()

cpp setting

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${default}",
               
                "C:/toos/vcpkg/installed/x64-windows/include"
            ],
            "windowsSdkVersion": "10.0.19041.0",
            "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx64/x64/cl.exe",
            "cStandard": "c17",
            "cppStandard": "c++17",
            "intelliSenseMode": "windows-msvc-x64",
            "configurationProvider": "ms-vscode.cmake-tools"
        }
    ],
    "version": 4
}

you have to select vs2017 amd x64 for compile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment