Compiling CUDA files with Qt 5.12



  • Hi, I'm trying to compile my Qt project containing a CUDA file (.cu). In QtCreator, I've edited the .pro file based on http://blog.qt.io/blog/2015/03/03/qt-weekly-28-qt-and-cuda-on-the-jetson-tk1/. The relevant part is as follows —

    # File(s) containing my CUDA code
    CUDA_SOURCES = CodeCUDA.cu
    
    # Location of CUDA on my system (Arch Linux)
    CUDA_DIR = /opt/cuda
    
    INCLUDEPATH += $$CUDA_DIR/include
    LIBS += -L$$CUDA_DIR/lib64 -lcuda -lcudart
    
    # Compute capability of my GPU
    CUDA_ARCH = sm_30
    
    cuda.input = CUDA_SOURCES
    cuda.output = ${QMAKE_FILE_BASE}_cuda.o
    cuda.commands = $$CUDA_DIR/bin/nvcc -c -arch=$$CUDA_ARCH -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME}
    cuda.dependency_type = TYPE_C
    cuda.depend_command = $$CUDA_DIR/bin/nvcc -M ${QMAKE_FILE_NAME}
    
    QMAKE_EXTRA_COMPILERS += cuda
    

    Yet when I try to build this, I get the error

    :-1: error: No rule to make target 'CodeCUDA.o', needed by 'CodeCUDA_cuda.o'.  Stop.
    

    [Edit]

    In the automatically generated Makefile, I see this —

    compiler_cuda_make_all: CodeCUDA_cuda.o
    compiler_cuda_clean:
    	-$(DEL_FILE) CodeCUDA_cuda.o
    CodeCUDA_cuda.o: ../OpenGL/CodeCUDA.cu \
    		CodeCUDA.o \
    		\: \
    		../OpenGL/CodeCUDA.cu \
    		/ \
    		/usr/include/stdc-predef.h \
    		/ \
    

    Which is then followed by a long list of CUDA headers. If I manually change this to the following, my project compiles and runs correctly —

    compiler_cuda_make_all: CodeCUDA_cuda.o
    compiler_cuda_clean:
    	-$(DEL_FILE) CodeCUDA_cuda.o
    CodeCUDA_cuda.o: ../OpenGL/CodeCUDA.cu \
    		/ \
    		/usr/include/stdc-predef.h \
    		/ \
    

    Any ideas what is causing the 'incorrect' generation of my Makefile?


  • Lifetime Qt Champion

    Hi,

    What platform are you on ?



  • I'm on (Arch) Linux, 64-bit.


  • Qt Champions 2017

    I don't know why it tries to generate that dependency, but as a long shot can you try without changing the objects file name, i.e.:

    cuda.output = ${QMAKE_FILE_BASE}.o
    

  • Lifetime Qt Champion

    Tested it on macOS and it worked correctly.

    Can you show the rest of your .pro file ?



  • Thanks guys for looking into it. @kshegunov, in that case the error changes to

    :-1: error: No rule to make target ':', needed by 'CodeCUDA.o'.  Stop.
    

    Part of the Makefile then looks like this —

    		../OpenGL/CodeCUDA.cu \
    		\: \
    		../OpenGL/CodeCUDA.cu \
    		/ \
    		/usr/include/stdc-predef.h \
    		/ \
    

    This is the entire .pro file —

    #-------------------------------------------------
    #
    # Project created by QtCreator 2019-01-07T11:13:21
    #
    #-------------------------------------------------
    
    QT += core gui
    
    greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
    
    TARGET = TemplateShowMeshOpenGL
    TEMPLATE = app
    
    # The following define makes your compiler emit warnings if you use
    # any feature of Qt which has been marked as deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS
    
    # You can also make your code fail to compile if you use deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    
    CONFIG += c++11
    
    SOURCES += \
            main.cpp \
            mainwindow.cpp \
        mainview.cpp
    
    HEADERS += \
            mainwindow.h \
        mainview.h
    
    FORMS += \
            mainwindow.ui
    
    # File(s) containing my CUDA code
    CUDA_SOURCES = CodeCUDA.cu \
                 KernelVBO.cu
    
    # Location of CUDA on my system (Arch Linux)
    CUDA_DIR = /opt/cuda
    
    INCLUDEPATH += $$CUDA_DIR/include
    LIBS += -L$$CUDA_DIR/lib64 -lcuda -lcudart
    
    # Compute capability of my GPU
    CUDA_ARCH = sm_30
    
    cuda.input = CUDA_SOURCES
    cuda.output = ${QMAKE_FILE_BASE}_cuda.o
    cuda.commands = $$CUDA_DIR/bin/nvcc -c -arch=$$CUDA_ARCH -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME}
    cuda.dependency_type = TYPE_C
    cuda.depend_command = $$CUDA_DIR/bin/nvcc -M ${QMAKE_FILE_NAME}
    
    QMAKE_EXTRA_COMPILERS += cuda
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    
    RESOURCES += \
        resources.qrc
    

    I'm using Qt Creator 4.8.1, QMake version 3.1, and Qt version 5.12.0.


  • Lifetime Qt Champion

    @Ailurus said in Compiling CUDA files with Qt 5.12:

    CUDA_SOURCES

    What if you try with:

    CUDA_SOURCES += \
                 $$PWD/CodeCUDA.cu \
                 $$PWD/KernelVBO.cu
    


  • Unfortunately that does not make a difference. Hmm, could it be a setting somewhere in Build Settings? Effective qmake call is set to

    /usr/bin/qmake /home/pieter/Dropbox/CUDA/OpenGL/TemplateShowMeshOpenGL.pro -spec linux-g++ CONFIG+=debug CONFIG+=qml_debug && /usr/bin/make qmake_all
    

    Manually changing the Makefile is not a big issue, at this point I'm just curious why it is generated like it is.


  • Lifetime Qt Champion

    That version of qmake is the one from your distribution and likely not the one from your 5.12 installation.

    Use the full path to it to ensure the correct one is used.



  • Hmm, it's the only qmake I have on my machine. Info —

    $ which qmake
    /usr/bin/qmake
    $ /usr/bin/qmake -v
    QMake version 3.1
    Using Qt version 5.12.0 in /usr/lib
    

  • Lifetime Qt Champion

    Good to know.

    In that case, can you try with the pre-built version ? Just to compare the output.



  • This is definitely a problem when upgrading from an older version of qmake to qmake 3.1 which is part of Qt 5.12.3.
    For some reason qmake 3.1 parses the output of a compile depend_command differently than before, which breaks existing .pro files that use a custom compiler for CUDA. Qmake ends up constructing a dependency list that looks like this:

    ...
    cuda/foo.cuh \
    cuda/foo.cu \
    buildDir/MyCudaLib.o \ 
    \: \               <--------- THIS IS WRONG
    cuda/bar.cu \
    / \
    /usr/local/cuda/include/cuda_runtime.h \
    ...
    

    The solution is to pipe the output of nvcc to:

    sed \"s/^.*: //\"
    

    to filter out the garbage, ie:

    cuda.depend_command = $$CUDA_DIR/bin/nvcc -g -M $$CUDA_INC $$NVCCFLAGS ${QMAKE_FILE_NAME} | sed \"s/^.*: //\"
    

Log in to reply