Is your feature request related to a problem? Please describe.
I want to be able to do find(onnxruntime) in my CMakeLists.txt and move on with my day. Can you guys provide a clear CMakeLists.txt example on how to include onnxruntime for a C++ project?
System information
Describe the solution you'd like
Can you guys provide a clear CMakeLists.txt example on how to include onnxruntime for a C++ project?
Describe alternatives you've considered
Looked everywhere online for a solution.
onnxruntime doesn't support CMake find_package yet.
I second this request.
This issue has been automatically marked as stale due to inactivity and will be closed in 7 days if no further activity occurs. If further support is needed, please provide an update and/or more details.
This issue has been automatically closed due to inactivity. Please reactivate if further support is needed.
Hi all,
for those facing the same issue, I can provide two exemplary solutions that work for Ubuntu (18.04). The second one might be applicable cross-plattform, but I have not tested this.
A) The hacky way via linux environment variables
Download the latest linux release and extract the archive. Copy over the include and lib directory contents into a folder structure of your choice, for example under /home/<USERNAME>/.local/include and /home/<USERNAME>/.local/lib, respectively . If this folder is not automatically checked by the linker, then export the environment variables
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/<USERNAME>/.local/lib
export LIBRARY_PATH=$LIBRARY_PATH:/home/<USERNAME>/.local/lib
such that the shared library is found during link and runtime. If the headers are not discovered automatically, add a corresonding include_directories directive in your CMakeLists.txt. Finally, your target should target_link_libraries against onnxruntime.
B) Create libraryConfig.cmake to enable find_package(onnxruntime)
This solution is much cleaner and does not require messing up the shared library paths nor a global installation. Again, extract the release into something like /home/<USERNAME>/.local/include and /home/<USERNAME>/.local/lib. Then, add onnxruntimeConfig.cmake under /home/<USERNAME>/.local/share/cmake/onnxruntime. Once /home/<USERNAME>/.local/share/cmake is on your CMAKE_PREFIX_PATH one can simply find_package(onnxruntime) and link the corresponding targets against ${onnxruntime_LIBRARIES}.
If the maintainers are interested, I can create a PR with the Cmake configuration. For the time being, I'm pasting onnxruntimeConfig.cmake below:
# 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)
# Assume we are in <install-prefix>/share/cmake/onnxruntime/onnxruntimeConfig.cmake
get_filename_component(CMAKE_CURRENT_LIST_DIR "${CMAKE_CURRENT_LIST_FILE}" PATH)
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"
)
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)
@snnn A script like onnxruntimeConfig.cmake is today the StateOftheArt practive for any library to cleanly integrate with any other project using cmake. I would be ok to prepare a PR to integrate it in the onnxrt master unless @jcarius would prefer to.
Would you mind reopening at least that ticket ?
@WilliamTambellini thank you for picking this up and please go ahead, I don't have the time at the moment to make a clean PR.
Apart from the onnxruntimeConfig.cmake pasted above, it probably makes sense to also install a onnxruntimeVersion.cmake in the same directory, something like this:
set(PACKAGE_VERSION "1.4.0")
# Check whether the requested PACKAGE_FIND_VERSION is compatible
if("${PACKAGE_VERSION}" VERSION_LESS "${PACKAGE_FIND_VERSION}")
set(PACKAGE_VERSION_COMPATIBLE FALSE)
else()
set(PACKAGE_VERSION_COMPATIBLE TRUE)
if("${PACKAGE_VERSION}" VERSION_EQUAL "${PACKAGE_FIND_VERSION}")
set(PACKAGE_VERSION_EXACT TRUE)
endif()
endif()
Most helpful comment
Hi all,
for those facing the same issue, I can provide two exemplary solutions that work for Ubuntu (18.04). The second one might be applicable cross-plattform, but I have not tested this.
A) The hacky way via linux environment variables
Download the latest linux release and extract the archive. Copy over the include and lib directory contents into a folder structure of your choice, for example under
/home/<USERNAME>/.local/includeand/home/<USERNAME>/.local/lib, respectively . If this folder is not automatically checked by the linker, then export the environment variablesexport LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/<USERNAME>/.local/libexport LIBRARY_PATH=$LIBRARY_PATH:/home/<USERNAME>/.local/libsuch that the shared library is found during link and runtime. If the headers are not discovered automatically, add a corresonding
include_directoriesdirective in yourCMakeLists.txt. Finally, your target shouldtarget_link_librariesagainstonnxruntime.B) Create libraryConfig.cmake to enable
find_package(onnxruntime)This solution is much cleaner and does not require messing up the shared library paths nor a global installation. Again, extract the release into something like
/home/<USERNAME>/.local/includeand/home/<USERNAME>/.local/lib. Then, addonnxruntimeConfig.cmakeunder/home/<USERNAME>/.local/share/cmake/onnxruntime. Once/home/<USERNAME>/.local/share/cmakeis on yourCMAKE_PREFIX_PATHone can simplyfind_package(onnxruntime)and link the corresponding targets against${onnxruntime_LIBRARIES}.If the maintainers are interested, I can create a PR with the Cmake configuration. For the time being, I'm pasting
onnxruntimeConfig.cmakebelow: