Compile OPTIX 7.0 as .exe

My program just finishes without entering the rg() program, i don’t get any error warnings. I added a simple print statement to the rg() which isn’t reached.

Wait, there is no error message in your host code if a *.ptx file wasn’t found?
The OptiX SDK throws a std::runtime_error exception when that happens, which could mean that you’re not correctly catching exceptions?
Please have a look into the OptiX 7 SDK code and search this function: getPtxStringFromFile.

Where do I have to put the PTX file?

That’s a good question, because there is actually a problem with how the OptiX SDK examples determine the location of the *.ptx and data files.
Find out more about it here: https://forums.developer.nvidia.com/t/sdk-samples-sutil-getptxstring-file-path/70963/2

Examples showing my method instead can be found here: https://github.com/NVIDIA/OptiX_Apps
Search that code for calls to readPTX.

CUDA_USE_STATIC_CUDA_RUNTIME is enabled in CMake, does this statically link the cuda runtime?
(It’s still the same CMakeLists that came with the OptiX 7.0 samples)

You can simply check which libraries your project links to inside Visual Studio. Just open your project’s properties and look at Linker->Input or Linker->Command Line
When using the CUDA Runtime API that will either contain cudart.lib or cudart_static.lib.
When using the CUDA Driver API that will link against cuda.lib which is always dynamic and loads the nvcuda64.dll from the driver repository.

If you really want to have just a single executable which contains everything, NVRTC is out of the question.
In addition to your *.cu code and headers, and the CUDA headers mentioned, that would also require the OptiX headers.
Means the target machine needs both a CUDA toolkit and the matching OptiX SDK version installed, because the license in either SDK does not allow to ship their headers individually. (Which is kind of unfortunate for OptiX 7 because the API is headers only.)

Anyway if you want to put any PTX source code into your executable binary there are normally three ways:

  1. Put the source code into string constants manually like already mentioned above.
  2. Put the source code into string constants by using the bin2c.exe tool inside the CUDA toolkit bin folder. That just creates constant arrays from any binary data you can compile into your app.
  3. Under Visual Studio put the source code into custom binary resources and load them from there. String tables won’t work because they are too small.
    Searching a little found this thread, which shows the necessary calls:
    https://stackoverflow.com/questions/9240188/how-to-load-a-custom-binary-resource-in-a-vc-static-library-as-part-of-a-dll
    I haven’t done that in long time and don’t know how to manage that with CMake. This is not portable and Windows only.

The issue with these methods is getting this automated in your project’s build. That needs to get the individual dependencies right.
Means the PTX files would need to be compiled first, then either converted to const array code or to binary resources and finally the project would needs to be built.
Or maybe have two projects for the nvcc compiler and the main project using the generated *.ptx code afterwards.
In the past I also used a simple batch script which just called nvcc for all *.cu files.

The question is if this is worth the effort. My OptiX 7 examples use a lot of other libraries for window management, OpenGL wrapping, loading images and mesh data from scene files. There is too much stuff to care about linking all that statically and if any of these is linking a Visual Studio runtime dynamically then you’re back at the beginning.
My only concern is that when packing all files necessary for a standalone executable, unpacking it into any local (unprotected) folder and running it from there just works.