Windows
OpenCV
-
In the official guide we can download the pre-built library, for example, we choose 4.6.0 and download the exe file
By running this
.exe
file we can decompress and extract files into a single folder that follows this structure:As a routine I will save all libraries like
opencv
,libtorch
andboost
, etc, into a folder calledC++Library
. These libraries will be shared among different projects. -
Next in the root level
CMakelists.txt
we includeset(CMAKE_CXX_STANDARD 14) set(CPACK_PROJECT_NAME ${PROJECT_NAME}) set(CPACK_PROJECT_VERSION ${PROJECT_VERSION}) set(OpenCV_DIR "C:\\Users\\user\\Repos\\C++Libraries\\opencv\\build\\x64\\vc16") find_package(OpenCV REQUIRED) message(STATUS "OpenCV_INCLUDE_DIRS = ${OpenCV_INCLUDE_DIRS}") message(STATUS "OpenCV_LIBS = ${OpenCV_LIBS}")
Change the path for
OpenCV_DIR
when needed. -
Then we can use
target_link_libraries(some_target PUBLIC ... ${OpenCV_LIBS} ...)
to link the library when the project needs it.
libtorch
-
We can download the libtorch pre-built library from official pytorch website. The structure is like this:
-
In the root level
CMakeLists.txt
we includeset(CMAKE_PREFIX_PATH "C:\\Users\\user\\Repos\\C++Libraries\\libtorch") find_package(Torch REQUIRED) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}") if (MSVC) message("copying dll files") file(GLOB TORCH_DLLS "${TORCH_INSTALL_PREFIX}/lib/*.dll") add_custom_command(TARGET EyeCatching POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy_if_different ${TORCH_DLLS} $<TARGET_FILE_DIR:EyeCatching>) endif (MSVC)
-
Then we can link the library by
target_link_libraries(some_target PUBLIC ... ${TORCH_LIBRARIES} ...)
Mac
OpenCV_
We install opencv directly by homebrew:
brew install opencv
Then the pre-built library can be found in /opt/homebrew/Cellar/opencv/
. It remains to link it in our cmake project.
The cmake instruction remains the same as windows, in my case my path to the libary is /opt/homebrew/Cellar/opencv/4.7.0_1
.
libtorch_
We can still download pre-built libtorch library in official pytorch website. The cmake instruction remains the same as windows.