链接Qt + CUDA +外部库

时间:2014-11-20 18:16:00

标签: qt opengl ubuntu cuda

我已经编写了一个PRO文件来为我在Linux下的CUDA应用程序生成一个GUI(已在windows下编译,现在将相同的组件移植到ubuntu 1404)。

我首先编译了一个帮助库“mylib.a”,并将其与我的应用程序“myapp.cu”相关联,并测试它编译并运行良好而没有Qt在图片中(通过nsight eclipse完成)。

对于使用Qt进行构建,我生成了以下PRO文件:

#############################
# basic PRO file for qmake
##############################


# QT libs to use
QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
##########################################
# QT basic files
##########################################
SOURCES+=main.cpp
SOURCES+=myGUI.cpp

HEADERS += myGUI.h
FORMS   += myGUI.ui

# Project dir and outputs
PROJECT_DIR = $$system(pwd)
OBJECTS_DIR = $$PROJECT_DIR/Obj
DESTDIR = ../bin

##########################################
# CUDA source files
##########################################
CUDA_SOURCES += myApp.cu
CUDA_SOURCES += kernel1.cu
CUDA_SOURCES += kernel2.cu

##########################################
# CUDA related components
##########################################

CUDA_DIR = /usr/local/cuda
CUDA_ARCH = sm_35
NVCCFLAGS = --compiler-options -fno-strict-aliasing -use_fast_math --ptxas-options=-v


##########################################
# include paths
##########################################
# CUDA
INCLUDEPATH += $$CUDA_DIR/include


# Additional dependencies

INCLUDEPATH += /usr/include/

##########################################
# library directories
##########################################
QMAKE_LIBDIR += $$CUDA_DIR/lib64
QMAKE_LIBDIR += $$CUDA_DIR/samples/common/lib


##########################################
#  LIBS
##########################################

LIBS += -lnvToolsExt
LIBS += -lopengl
#libcudart_static
#LIBS += -lcudart_static
LIBS += -lcuda
LIBS += -lcudart
LIBS += -lGL
# My library is added here
LIBS += -lmyLib 

# join the includes in a line
CUDA_INC = $$join(INCLUDEPATH,' -I','-I',' ')


##########################################
# Extra compiler configuration for CUDA
##########################################

cuda.input = CUDA_SOURCES
cuda.output = ${OBJECTS_DIR}${QMAKE_FILE_BASE}.o

cuda.commands = $$CUDA_DIR/bin/nvcc $$CUDA_DEFINES -m64 -g -arch=$$CUDA_ARCH -c $$NVCCFLAGS $$CUDA_INC $$LIBS  ${QMAKE_FILE_NAME} -c -o ${QMAKE_FILE_OUT}

cuda.dependency_type = TYPE_C 
cuda.depend_command = $$CUDA_DIR/bin/nvcc $$CUDA_DEFINES -g -M $$CUDA_INC $$NVCCFLAGS ${QMAKE_FILE_NAME}
# Tell Qt that we want add more stuff to the Makefile
QMAKE_EXTRA_UNIX_COMPILERS += cuda

1 个答案:

答案 0 :(得分:1)

在.pro文件中尝试这些。我用它在Linux上成功链接到CUDA:

# Define output directories

CONFIG(release, debug|release): CUDA_OBJECTS_DIR = release/cuda
else: CUDA_OBJECTS_DIR = debug/cuda


# This makes the .cu files appear in your project
OTHER_FILES +=  vectorAddition.cu

# CUDA settings <-- may change depending on your system
CUDA_SOURCES += vectorAddition.cu


unix{
    CUDA_SDK = "/usr/local/cuda-5.5/"   # Path to cuda SDK install
    CUDA_DIR = "/usr/local/cuda-5.5/"   # Path to cuda toolkit install
}

unix: SYSTEM_NAME = unix

SYSTEM_TYPE = 32            # '32' or '64', depending on your system
CUDA_ARCH = sm_30           # Type of CUDA architecture, for example 'compute_10', 'compute_11', 'sm_10'
NVCC_OPTIONS = --use_fast_math

# include paths
INCLUDEPATH += $$CUDA_DIR/include
               #$$CUDA_SDK/common/inc/ \
               #$$CUDA_SDK/../shared/inc/

# library directories
unix:{
    QMAKE_LIBDIR += $$CUDA_DIR/lib
}


# Add the necessary libraries
unix:
{
    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"','"')
    NVCC_LIBS = $$join(CUDA_LIBS,' -l','-l', '')
    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}_cuda.o

    unix: 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}_cuda.o

    unix: 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
}