Skip to content

Instantly share code, notes, and snippets.

@UnaNancyOwen
Last active October 26, 2024 16:16
Show Gist options
  • Save UnaNancyOwen/9d25d9ef66b163e0667b4b3bf3962f8a to your computer and use it in GitHub Desktop.
Save UnaNancyOwen/9d25d9ef66b163e0667b4b3bf3962f8a to your computer and use it in GitHub Desktop.
CMakeLists for OpenCV
cmake_minimum_required( VERSION 3.6 )
# Create Project
project( solution )
add_executable( project main.cpp )
# Set StartUp Project
set_property( DIRECTORY PROPERTY VS_STARTUP_PROJECT "project" )
# Find Package
set( OpenCV_DIR "C:/Program Files/opencv/build" )
option( OpenCV_STATIC OFF )
find_package( OpenCV REQUIRED )
# Set Static Link Runtime Library
if( OpenCV_STATIC )
foreach( flag_var
CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO )
if( ${flag_var} MATCHES "/MD" )
string( REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}" )
endif()
endforeach()
endif()
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()
@cloudlakecho
Copy link

@YCAyca I think if you have OpenCV installed in your computer, the "OpenCV_LIBS" will be automatically assigned. It means that you don't need set ( OpenCV_LIBS <your local folder> ).

Here are similar question in Stackoverflow and suggestion in "OpenCVConfig.cmake.in"

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