有时候需要跑在GPU上运行的cuda程序,如何在QtCreator上编译呢?我本想QtCreator上添加cuda的nvcc编译器,但是设置下拉框里没有NVCC这一项。
我就想,能否用nvcc编译了.cu文件,然后和QtCreator的项目程序编译链接在一起,比较偏门。
执行过程:
1.前提已经安装了cuda工具包,这样你才有nvcc编译器。假如你有myfun.h和myfun.cu文件,编写makefile,或直接在终端使用nvcc 编译.cu文件,生成可执行程序.o文件,假设名为myfun.o 。
2.在项目文件.pro中添加
OBJECTS += myfun.o
3.然后使用你的QtCreator构建项目吧。
------------------------------------------------------补充-------------------------------------------------------------
另外一种通用的方法:
可在.pro文件中添加
转载 Ubuntu上使用QT creator运行cuda程序_狸猫文的博客-CSDN博客
# CUDA settings <-- may change depending on your system
CUDA_SOURCES += trans.cu
CUDA_SDK = "/usr/local/cuda-10.2/" # Path to cuda SDK install
CUDA_DIR = "/usr/local/cuda-10.2/" # Path to cuda toolkit install
# DO NOT EDIT BEYOND THIS UNLESS YOU KNOW WHAT YOU ARE DOING....
SYSTEM_NAME = ubuntu # Depending on your system either 'Win32', 'x64', or 'Win64'
SYSTEM_TYPE = 64 # '32' or '64', depending on your system
CUDA_ARCH = sm_72 # Type of CUDA architecture, for example 'compute_10', 'compute_11', 'sm_10' 'sm_50'
NVCC_OPTIONS = --use_fast_math
# include paths
INCLUDEPATH += $$CUDA_DIR/include
# library directories
QMAKE_LIBDIR += $$CUDA_DIR/lib64/
CUDA_OBJECTS_DIR = ./
# Add the necessary libraries
CUDA_LIBS = -lcuda -lcudart
# The following makes sure all path names (which often include spaces) are put between quotation marks
CUDA_INC = $$join(INCLUDEPATH,'" -I"','-I"','"')
#LIBS += $$join(CUDA_LIBS,'.so ', '', '.so')
LIBS += $$CUDA_LIBS
# Configuration of the Cuda compiler
CONFIG(debug, debug|release) {
# Debug mode
cuda_d.input = CUDA_SOURCES
cuda_d.output = $$CUDA_OBJECTS_DIR/${QMAKE_FILE_BASE}.o
cuda_d.commands = $$CUDA_DIR/bin/nvcc -D_DEBUG $$NVCC_OPTIONS $$CUDA_INC $$NVCC_LIBS --machine $$SYSTEM_TYPE -arch=$$CUDA_ARCH -c -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME}
cuda_d.dependency_type = TYPE_C
QMAKE_EXTRA_COMPILERS += cuda_d
}
else {
# Release mode
cuda.input = CUDA_SOURCES
cuda.output = $$CUDA_OBJECTS_DIR/${QMAKE_FILE_BASE}.o
cuda.commands = $$CUDA_DIR/bin/nvcc $$NVCC_OPTIONS $$CUDA_INC $$NVCC_LIBS --machine $$SYSTEM_TYPE -arch=$$CUDA_ARCH -c -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME}
cuda.dependency_type = TYPE_C
QMAKE_EXTRA_COMPILERS += cuda
}
--------------------------------------------------------------------
另外可参考Linux(Ubuntu)配置QtCreator使用cuda-gdb调试Cuda .cu_HIIWAR_ZB的博客-CSDN博客