从输出中的文本来看,您似乎正在使用 CMake 构建示例。看起来构建系统认为您想要编译目标文件而不是 PTX 文件。您看到的错误消息也表示此症状。
从 CMake 中编译 CUDA 代码有多种方法。
如果您使用的是 CUDA 运行时,则通常执行以下操作:
cuda_add_executable(myprogram main.cpp mycuda.cu myheader.h)
这将创建一个名为 myprogram 的可执行文件,该可执行文件由两个目标文件组成:main.cpp.o 和 mycuda.cu.o。CUDA 运行时要求 cuda 文件中的代码符合 CUDA 运行时 API。
除此之外cuda_add_executable
还有cuda_add_library
编译库而不是可执行文件。这两个宏都使用另一个名为的宏cuda_wrap_srcs
,它完成了大部分繁重的工作,即生成构建命令来编译 CUDA 代码。除其他外,此宏有一个参数,用于指定您是要使用 CUDA 运行时还是仅针对 PTX(参数是 OBJ 或 PTX)。
要编译 OptiX 着色器,您必须以 PTX 为目标。在我们发布的 SDK 中,这是通过一个名为 CMake 的函数来处理的,该函数OPTIX_add_sample_executable
可以在<install>/SDK/CMakeLists.txt
. 这个函数的主要作用是调用cuda_wrap_srcs
指定的 PTX 选项。我在这里也包含了该功能以供参考。
#########################################################
# OPTIX_add_sample_executable
#
# Convenience function for adding samples to the code. You can copy the contents of this
# function into your individual project if you wish to customize the behavior. Note that
# in CMake, functions have their own scope, whereas macros use the scope of the caller.
function(OPTIX_add_sample_executable target_name)
# These calls will group PTX and CUDA files into their own directories in the Visual
# Studio projects.
source_group("PTX Files" REGULAR_EXPRESSION ".+\\.ptx$")
source_group("CUDA Files" REGULAR_EXPRESSION ".+\\.cu$")
# Separate the sources from the CMake and CUDA options fed to the macro. This code
# comes from the CUDA_COMPILE_PTX macro found in FindCUDA.cmake. We are copying the
# code here, so that we can use our own name for the target. target_name is used in the
# creation of the output file names, and we want this to be unique for each target in
# the SDK.
CUDA_GET_SOURCES_AND_OPTIONS(source_files cmake_options options ${ARGN})
# Create the rules to build the PTX from the CUDA files.
CUDA_WRAP_SRCS( ${target_name} PTX generated_files ${source_files} ${cmake_options}
OPTIONS ${options} )
# Here is where we create the rule to make the executable. We define a target name and
# list all the source files used to create the target. In addition we also pass along
# the cmake_options parsed out of the arguments.
add_executable(${target_name}
${source_files}
${generated_files}
${cmake_options}
)
# Most of the samples link against the sutil library and the optix library. Here is the
# rule that specifies this linkage.
target_link_libraries( ${target_name}
sutil
optix
${optix_rpath}
)
endfunction()