2

我正在尝试在 32 位 Windows 7 系统上的 Qt Creator 4.8.0 中编译 Cuda 代码(以 .cu 文件的形式),但我目前失败了。

我整理了以下项目文件:

TARGET = TestCUDA
DESTDIR = release
OBJECTS_DIR = release/obj
CUDA_OBJECTS_DIR = release/cuda

SOURCES += main.cpp
CUDA_SOURCES += test.cu

CUDA_SDK = "C:/ProgramData/NVIDIA Corporation/NVIDIA GPU Computing SDK 4.2/C" # Path to cuda SDK install
CUDA_DIR = "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v4.2" # Path to cuda toolkit install

INCLUDEPATH += $$CUDA_DIR/include \
               $$CUDA_SDK/common/inc/ \
               $$CUDA_SDK/../shared/inc/
QMAKE_LIBDIR += $$CUDA_DIR/lib/Win32 \
                $$CUDA_SDK/common/lib/Win32 \    
                $$CUDA_SDK/../shared/lib/Win32
LIBS += -lcuda -lcudart
CUDA_INC = $$join(INCLUDEPATH,'" -I"','-I"','"')   # this is to put quotes around paths with spaces

cuda.input = CUDA_SOURCES
cuda.output = $$CUDA_OBJECTS_DIR/${QMAKE_FILE_BASE}_cuda.ptx
cuda.commands = $$CUDA_DIR/bin/nvcc.exe $$CUDA_INC $$LIBS --machine 32 -ptx -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME}
cuda.dependency_type = TYPE_C
QMAKE_EXTRA_COMPILERS += cuda

这应该在 release/cuda/ 文件夹中生成一个 test_cuda.ptx 模块。顺便说一句,这正是它的作用,哇!但它也给出了 LNK1107 错误:

C:\path\to\release\cuda\test_cuda.ptx:-1: error: LNK1107: invalid or corrupt file: cannot read at 0xFCC

我不知道为什么会发生这种情况,也不知道如何解决。此错误中指示的位置位于文件中间的某个位置test_cuda.ptx,但这没有任何帮助。同样奇怪的是,当我将同一个文件导入另一个 Cuda 应用程序时,它可以完美运行,因此文件没有损坏。main.cpp完全是空的:

int main(int argc, char* argv []) {}

so that's not where the linker could go wrong. It seems like the linker is linking stuff it shouldn't link, but I don't know why, or how to stop this. Anyone an idea?

4

1 回答 1

2

As mentioned by @asm you are compiling to PTX intermediate files and then trying to link them as object files. But counter to @asm's suggestion, you should not compile to cubin, but to object files. To do so, you want the -c option rather than the -ptx option:

cuda.input = CUDA_SOURCES
cuda.output = $$CUDA_OBJECTS_DIR/${QMAKE_FILE_BASE}_cuda.o
cuda.commands = $$CUDA_DIR/bin/nvcc.exe $$CUDA_INC $$LIBS --machine 32 -c -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME}

Caveat: not a QMAKE user so the above might not be exactly right. I second the recommendation for CMake.

于 2012-09-04T00:58:40.063 回答