How to include OptiX 8.0 in CMake project?

I have downloaded and installed OptiX 8.0 and Cuda 12.0 and want to use them in my CMake based project. My understanding is that for OptiX I need only add the include directory, Cuda can be used with the find_package command:

find_package(CUDA REQUIRED)

set(OPTIX_DIR $ENV{OptiX_DIR}include)
message("OptixDir: ${OPTIX_DIR}")
# prints: C:\ProgramData\NVIDIA Corporation\OptiX SDK 8.0.0\include

target_include_directories(
   ${PROJECT_NAME} PUBLIC
   ${OPTIX_DIR}
)

In one of my files, I included “optix.h”.
When I go and compile this (Win 11 / VS 2022), I get an error

C:\ProgramData\NVIDIA Corporation\OptiX SDK 8.0.0\include\optix_host.h(35,10): fatal error C1083: Cannot open include file: ‘cuda.h’: No such file or directory

I cannot find any information about how to actually use OptiX 8.0 in my own project and the SDK uses find_package(OptiX), which doesn’t work at all.

OptiX is based on CUDA and the optix_host.h and some other headers include cuda.h by default to be able to use CUDA driver API calls with the resp. CUDA types.

That cuda.h header ships with the CUDA Toolkit and can be found inside its include folder of your local CUDA toolkit installation.

That the compiler is not finding it inside your project means that the CUDA include directory is not present inside the CMake include directories.

When using CMake’s native CUDA language support via project(my_project_name LANGUAGES CXX CUDA) things happen semi-automatically for native CUDA applications.
The include directories are added to the project when linking against the CUDA runtime API.
set(CMAKE_CUDA_RUNTIME_LIBRARY Shared)
https://cmake.org/cmake/help/latest/variable/CMAKE_CUDA_RUNTIME_LIBRARY.html

Actually after some searching, when using the CMake CUDA language feature, the CUDA Toolkit include directory is stored inside the CMake variable CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES.

I’m not using that method for my OptiX examples but generate custom build rules for my *.cu files instead and for that I need the OptiX and CUDA include directories as CMake string variables.

For that I find the CUDA toolkit via find_package(CUDAToolkit 10.0 REQUIRED) which searches in default locations and environment variables, esp. CUDA_PATH.
https://cmake.org/cmake/help/latest/module/FindCUDAToolkit.html#findcudatoolkit

FindCUDAToolkit.cmake replaced the legacy FindCUDA.cmake which is deprecated since CMake 3.10 and shouldn’t be used anymore.
https://cmake.org/cmake/help/latest/module/FindCUDA.html#findcuda

(That find_package(CUDAToolkit 10.0 REQUIRED) looks for at least CUDA 10.0 but will take any newer one specified with the environment variable CUDA_PATH under Windows, which on my development system points to CUDA 12.2 currently.)

include_directories(
  # ... other include directories
  ${CUDAToolkit_INCLUDE_DIRS}
)

target_link_libraries( my_project_name
  # ... other libs
  CUDA::cudart
  CUDA::cuda_driver
)

Examples using CMake’s native FindCUDAToolkit.cmake script and some custom build rules translating CUDA input code to PTX or OptiX IR can be found in my OptiX examples.
These are not using anything from the OptiX SDK than the include files, means it’s a different OptiX application framework example.
https://forums.developer.nvidia.com/t/optix-advanced-samples-on-github/48410/4
Specifically look at all CMakeLists.txt files using the scripts inside this folder:
https://github.com/NVIDIA/OptiX_Apps/tree/master/3rdparty/CMake

That helped, thank you. I had linker errors afterwards, so for completeness sake, this is how it compiled and linked:

# setup CUDA, necessary for optix
find_package(CUDAToolkit 12.0 REQUIRED)
	
target_include_directories(
	${PROJECT_NAME} PUBLIC
	${CUDAToolkit_INCLUDE_DIRS}
)
message(${CUDAToolkit_LIBRARY_DIR})
target_link_libraries(
	${PROJECT_NAME} PUBLIC
	CUDA::cudart
)
	
# setup optix as an alternative to vulkan
set(OPTIX_DIR $ENV{OptiX_DIR}include)
message("OptixDir: ${OPTIX_DIR}")
# prints: C:\ProgramData\NVIDIA Corporation\OptiX SDK 8.0.0\include

target_include_directories(
	${PROJECT_NAME} PUBLIC
	${OPTIX_DIR}
)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.