将通过conan构建的外部解决方案添加到CMake项目中可以使用以下两种方法:
方法一:使用Find_Package 使用Find_Package命令在CMakeLists.txt文件中查找已安装的包。例如,假设我们想将OpenCV作为外部解决方案添加到项目中,我们可以将以下代码添加到CMakeLists.txt文件中:
find_package(OpenCV REQUIRED)
if(OpenCV_FOUND)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(your_project ${OpenCV_LIBS})
endif()
方法二:使用conan提供的cmake工具链 Conan提供了一个方便的cmake工具链来添加包。只需在CMakeLists.txt文件中添加以下代码:
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
例如,假设我们想将OpenCV包添加到项目中,我们可以使用以下命令安装OpenCV:
conan install opencv/4.5.2@
然后,在CMakeLists.txt文件中添加以下代码:
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
target_link_libraries(your_project ${OpenCV_LIBS})