Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking Qt + CUDA + external library

I've written a PRO file to generate a GUI for my CUDA application under linux (already compiled under windows now porting same components to ubuntu 1404).

I've first compiled a helper library "mylib.a" and linked that with my application "myapp.cu" and tested that it compiles and runs nicely without Qt in the picture (done via nsight eclipse).

For building with Qt I've generated the following PRO file:

#############################
# 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
like image 325
Jimmy Pettersson Avatar asked Mar 12 '26 08:03

Jimmy Pettersson


1 Answers

Try these in your .pro file. I used it to link to CUDA on Linux successfully :

# 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
}
like image 176
Nejat Avatar answered Mar 14 '26 22:03

Nejat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!