cmake学习笔记(七)编写自己的xxxConfig.cmake

本文介绍了如何为ONNXRuntime库编写自定义的cmake配置文件onnxruntimeConfig.cmake,包括基本版和进阶版的实现,以便在项目中方便地找到并使用ONNXRuntime库。进阶版详细定义了库的路径、头文件目录、编译选项等,并通过find_package处理标准参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

cmake学习笔记(七)编写自己的xxxConfig.cmake

1. onnxruntimeConfig.cmake

因为项目是统一用cmake管理的,因此对于没有xxxConfig.cmake文件的库需要自己编写。
比如:ONNXRUNTIMEConfig.cmake

find_path(ONNXRUNTIME_INCLUDE_DIR NAMES onnxruntime_cxx_api.h PATHS "D:/ProgramFiles/onnxruntime-win-x64-1.8.1/include")
find_library(ONNXRUNTIME_LIBRARY NAMES onnxruntime.lib PATHS "D:/ProgramFiles/onnxruntime-win-x64-1.8.1/lib")
set(ONNXRUNTIME_FOUND TRUE)
set(ONNXRUNTIME_INCLUDE_DIRS ${ONNXRUNTIME_INCLUDE_DIR})
set(ONNXRUNTIME_LIBRARY ${ONNXRUNTIME_LIBRARY})

写成这样就可以使用了。当然路径那里应该使用相对路径才 具有通用性,后面会改正。

参考:编写自定义cmake配置文件FindXXX.cmake或者xxx-config.cmake | cmake with user defined entry

2. 进阶版onnxruntimeConfig.cmake

onnxruntimeConfig.cmake 该文件放置到下载好的onnxruntime库的根目录下,然后指定onnxruntime_DIR到该根目录。

# Custom cmake config file by jcarius to enable find_package(onnxruntime) without modifying LIBRARY_PATH and LD_LIBRARY_PATH
#
# This will define the following variables:
#   onnxruntime_FOUND        -- True if the system has the onnxruntime library
#   onnxruntime_INCLUDE_DIRS -- The include directories for onnxruntime
#   onnxruntime_LIBRARIES    -- Libraries to link against
#   onnxruntime_CXX_FLAGS    -- Additional (required) compiler flags

include(FindPackageHandleStandardArgs)
set(ONNXRUNTIME_FOUND TRUE) # auto 
message("ONNXRUNTIME_FOUND " ${ONNXRUNTIME_FOUND})
# Assume we are in <install-prefix>/share/cmake/onnxruntime/onnxruntimeConfig.cmake
get_filename_component(CMAKE_CURRENT_LIST_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
message("CMAKE_CURRENT_LIST_DIR " ${CMAKE_CURRENT_LIST_DIR})
get_filename_component(onnxruntime_INSTALL_PREFIX "${CMAKE_CURRENT_LIST_DIR}/" ABSOLUTE)

set(onnxruntime_INCLUDE_DIRS ${onnxruntime_INSTALL_PREFIX}/include)
set(onnxruntime_LIBRARIES onnxruntime)
set(onnxruntime_CXX_FLAGS "") # no flags needed


find_library(onnxruntime_LIBRARY onnxruntime
    PATHS "${onnxruntime_INSTALL_PREFIX}/lib"
)

message("onnxruntime_INCLUDE_DIRS " ${onnxruntime_INCLUDE_DIRS})
message("onnxruntime_LIBRARY " ${onnxruntime_LIBRARY})


add_library(onnxruntime SHARED IMPORTED)
set_property(TARGET onnxruntime PROPERTY IMPORTED_LOCATION "${onnxruntime_LIBRARY}")
set_property(TARGET onnxruntime PROPERTY INTERFACE_INCLUDE_DIRECTORIES "${onnxruntime_INCLUDE_DIRS}")
set_property(TARGET onnxruntime PROPERTY INTERFACE_COMPILE_OPTIONS "${onnxruntime_CXX_FLAGS}")

find_package_handle_standard_args(onnxruntime DEFAULT_MSG onnxruntime_LIBRARY onnxruntime_INCLUDE_DIRS)


CMakeLists.txt中的调用:

FIND_PACKAGE(onnxruntime REQUIRED)

message("onnxruntime_INCLUDE_DIRS " ${onnxruntime_INCLUDE_DIRS})
message("onnxruntime_LIBRARY " ${onnxruntime_LIBRARY})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落花逐流水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值