'cuda 10.2 in Qt 5.14 ubuntu 18.04

I am planning to start cuda programming in Qt framework. I would like to start with a simple example.
system information :
OS : ubuintu 18.04 LTS
Qt version : 5.14
Compiler : GCC
CUDA version : 10.2
GPU : NVIDIA GTX 1060 with compute capability 6.1

I searched a lot and came across this useful topic: https://cudaspace.wordpress.com/2012/07/05/qt-creator-cuda-linux-review/

I followed the topic step by step and made my project's .pro file with my Cuda architecture and other essentials. This is my project.pro file contents :

QT -= gui
QT += core
CONFIG += c++11 console
CONFIG -= app_bundle
DEFINES += QT_DEPRECATED_WARNINGS
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
DESTDIR     = $$system(pwd)
OBJECTS_DIR = $$DESTDIR/Obj
# C++ flags
QMAKE_CXXFLAGS_RELEASE =-03
CUDA_SOURCES += cuda_code.cu=
SOURCES += main.cpp \
       cuda_code.cu
CUDA_DIR = /usr/local/cuda
INCLUDEPATH  += $$CUDA_DIR/include
QMAKE_LIBDIR += $$CUDA_DIR/lib64
LIBS += -lcudart -lcuda
CUDA_ARCH = sm_61                
NVCCFLAGS     = --compiler-options -fno-strict-aliasing -use_fast_math --ptxas-options=-v
CUDA_INC = $$join(INCLUDEPATH,' -I','-I',' ')
cuda.commands = $$CUDA_DIR/bin/nvcc -m64 -O3 -arch=$$CUDA_ARCH -c $$NVCCFLAGS \
            $$CUDA_INC $$LIBS  ${QMAKE_FILE_NAME} -o ${QMAKE_FILE_OUT} \
            2>&1 | sed -r \"s/\\(([0-9]+)\\)/:\\1/g\" 1>&2
cuda.dependency_type = TYPE_C 
cuda.depend_command = $$CUDA_DIR/bin/nvcc -O3 -M $$CUDA_INC $$NVCCFLAGS ${QMAKE_FILE_NAME}
cuda.input = CUDA_SOURCES
cuda.output = ${OBJECTS_DIR}${QMAKE_FILE_BASE}_cuda.o
QMAKE_EXTRA_COMPILERS += cuda

Now, this is the main.cpp contents

#include <QtCore/QCoreApplication>
#include <iostream>
using namespace std;
#include <cuda_runtime.h>
#include <cuda_code.cu>
extern "C"
cudaError_t cuda_main();
int main(int argc, char *argv[])
 {
   QCoreApplication a(argc, argv);

   // run your cuda application
   cudaError_t cuerr = cuda_main();
   // check for errors is always a good practice!
   if (cuerr != cudaSuccess) cout << "CUDA Error: " << cudaGetErrorString( cuerr ) << endl;
   return a.exec();
 }

And this is the .cu file contents :

#include <thrust/host_vector.h>
#include <thrust/device_vector.h>
#include <thrust/sort.h>
extern "C"
cudaError_t cuda_main()
{
   // generate 16M random numbers on the host
   thrust::host_vector<int> h_vec(1 << 24);
   thrust::generate(h_vec.begin(), h_vec.end(), rand);
   // transfer data to the device
   thrust::device_vector<int> d_vec = h_vec;
   // sort data on the device (805 Mkeys/sec on GeForce GTX 480)
   thrust::sort(d_vec.begin(), d_vec.end());
   // transfer data back to host
   thrust::copy(d_vec.begin(), d_vec.end(), h_vec.begin());
   return cudaGetLastError();
 }

first of all, I make it with build -> run make and it is made successfully. but when I wanna run it, this error shows up :

make: *** No rule to make target 'cuda_code.o', needed by 'Obj/cuda_code_cuda.o'.  Stop.
19:18:20: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project untitled6 (kit: Desktop Qt 5.14.0 GCC 64bit)
When executing step "Make"

I looked for a solution and tested some without any success. I would appreciate any help.



Solution 1:[1]

I solved this problem with the @Gred's solution in this link :

https://forum.qt.io/topic/114853/cuda-10-2-in-qt-5-14-ubuntu-18-04/12

This line in the .pro file:

SOURCES += main.cpp \
   cuda_code.cu

should get changed to :

SOURCES += main.cpp

And then remove #include "cuda_code.cu" from the main.cpp.

And Now everything works happily :).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1