简体   繁体   中英

Compiling c++ and cuda code with MinGW in QTCreator

Im trying to compile simple cuda program (i took source code from Compiling Cuda code in Qt Creator on Windows ) This is my .pro file:

TARGET = Cuda

# Define output directories
DESTDIR = release
OBJECTS_DIR = release/obj
CUDA_OBJECTS_DIR = release/cuda

# Sourcefiles
SOURCES += main.cpp

# 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

CUDA_SDK = "C:/Cuda/CudaSamples"   # Path to cuda SDK install
CUDA_DIR = "C:/Cuda/CudaToolkit"            # Path to cuda toolkit install
SYSTEM_NAME = Win32         # Depending on your system either 'Win32', 'x64', or 'Win64'
SYSTEM_TYPE = 32            # '32' or '64', depending on your system
CUDA_ARCH = sm_11           # Type of CUDA architecture, for example 'compute_10', 'compute_11', 'sm_10'
TEMP = 'C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin' #tried to add VS compiler path, seems woring. When i changed it to higher version error appeared
VS = $$join(TEMP,'" -ccbin "','-ccbin "','"') #joing path to VS compiler with quotes
NVCC_OPTIONS = --use_fast_math $$VS

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

# library directories
QMAKE_LIBDIR += $$CUDA_DIR/lib/$$SYSTEM_NAME \
                $$CUDA_SDK/common/lib/$$SYSTEM_NAME \
                $$CUDA_SDK/../shared/lib/$$SYSTEM_NAME \
                C:/CUDA/VS10/VC/lib \   #tried to add VS runtime libraries, after that some errors disappeared, and some new emerged so i guess it worked
QMAKE_LIBS += -lmsvcrt -llibcmt -llibcpmt #after adding VS runtime to QMAKE_LIBDIR this line seems to have no effect
# Add the necessary libraries
LIBS += -lcuda -lcudart
#LIBS += -lmsvcr100
QMAKE_CFLAGS_RELEASE += -v
MSVCRT_LINK_FLAG_DEBUG   = "/MTd"
MSVCRT_LINK_FLAG_RELEASE = "/MT"

# The following makes sure all path names (which often include spaces) are put between quotation marks
CUDA_INC = $$join(INCLUDEPATH,'" -I"','-I"','"')
#this was nested in CONFIG (see link above) but i dont think it worked
    cuda.input = CUDA_SOURCES
    cuda.output = $$CUDA_OBJECTS_DIR/${QMAKE_FILE_BASE}_cuda.o
    cuda.commands = $$CUDA_DIR/bin/nvcc.exe $$NVCC_OPTIONS $$CUDA_INC $$LIBS --machine $$SYSTEM_TYPE -arch=$$CUDA_ARCH \
                    -Xcompiler "/wd4819,/EHsc,/W3,/nologo,/O2,/Zi" \ 
                    -Xptxas -v \
                    -Xcompiler $$MSVCRT_LINK_FLAG_RELEASE \
     -c -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME} #Xptxas -v is command for some extra output during cuda compiling (nvvc). -Xcompiler, honesly i have no idea what "/wd4819,/EHsc,/W3,/nologo,/O2,/Zi" \ means,i copied those from some chinese webpage and again some errors went away
    cuda.dependency_type = TYPE_C
    QMAKE_EXTRA_COMPILERS += cuda

HEADERS += \
    vectorAddition.h #empty header, i was just trying something.

This is compiler output:

13:09:17: Running steps for project Cuda...
13:09:17: Starting: "F:\Programs\Qt5.1.1\Tools\mingw48_32\bin\mingw32-make.exe" clean
F:/Programs/Qt5.1.1/Tools/mingw48_32/bin/mingw32-make -f Makefile.Release clean
mingw32-make[1]: Entering directory 'F:/workspaceSchool/build-Cuda-Desktop_Qt_5_1_1_MinGW_32bit-Release'
del release\cuda\vectorAddition_cuda.o
del release\cuda\vectorAddition_cuda.o release\obj\main.o
Could Not Find F:\workspaceSchool\build-Cuda-Desktop_Qt_5_1_1_MinGW_32bit-Release\release\cuda\vectorAddition_cuda.o
mingw32-make[1]: Leaving directory 'F:/workspaceSchool/build-Cuda-Desktop_Qt_5_1_1_MinGW_32bit-Release'
F:/Programs/Qt5.1.1/Tools/mingw48_32/bin/mingw32-make -f Makefile.Debug clean
mingw32-make[1]: Entering directory 'F:/workspaceSchool/build-Cuda-Desktop_Qt_5_1_1_MinGW_32bit-Release'
del release\cuda\vectorAddition_cuda.o
Could Not Find F:\workspaceSchool\build-Cuda-Desktop_Qt_5_1_1_MinGW_32bit-Release\release\cuda\vectorAddition_cuda.o
del release\cuda\vectorAddition_cuda.o debug\obj\main.o
Could Not Find F:\workspaceSchool\build-Cuda-Desktop_Qt_5_1_1_MinGW_32bit-Release\release\cuda\vectorAddition_cuda.o
Could Not Find F:\workspaceSchool\build-Cuda-Desktop_Qt_5_1_1_MinGW_32bit-Release\debug\obj\main.o
mingw32-make[1]: Leaving directory 'F:/workspaceSchool/build-Cuda-Desktop_Qt_5_1_1_MinGW_32bit-Release'
13:09:18: The process "F:\Programs\Qt5.1.1\Tools\mingw48_32\bin\mingw32-make.exe" exited normally.
13:09:18: Starting: "F:\Programs\Qt5.1.1\5.1.1\mingw48_32\bin\qmake.exe" F:\workspaceSchool\Cuda\Cuda.pro -r -spec win32-g++ -v
QMake version 3.0
Using Qt version 5.1.1 in F:\Programs\Qt5.1.1\5.1.1\mingw48_32\lib
13:09:18: The process "F:\Programs\Qt5.1.1\5.1.1\mingw48_32\bin\qmake.exe" exited normally.
13:09:18: Starting: "F:\Programs\Qt5.1.1\Tools\mingw48_32\bin\mingw32-make.exe" 
F:/Programs/Qt5.1.1/Tools/mingw48_32/bin/mingw32-make -f Makefile.Release
mingw32-make[1]: Entering directory 'F:/workspaceSchool/build-Cuda-Desktop_Qt_5_1_1_MinGW_32bit-Release'
C:\Cuda\CudaToolkit\bin\nvcc.exe --use_fast_math -ccbin "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\bin" -I"C:/Cuda/CudaToolkit/include" -I"C:/Cuda/CudaSamples/common/inc/" -I"C:/Cuda/CudaSamples/../shared/inc/" -lcuda -lcudart --machine 32 -arch=sm_11 -Xcompiler /wd4819,/EHsc,/W3,/nologo,/O2,/Zi -Xptxas -v -Xcompiler /MT -c -o release\cuda\vectorAddition_cuda.o ..\Cuda\vectorAddition.cu
ptxas : info : 0 bytes gmem
ptxas : info : Compiling entry function '__cuda_dummy_entry__' for 'sm_11'
ptxas : info : Used 0 registers
g++ -c -pipe -fno-keep-inline-dllexport -O2 -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_NEEDS_QMAIN -I..\Cuda -I"C:\Cuda\CudaToolkit\include" -I"C:\Cuda\CudaSamples\common\inc" -I"C:\Cuda\CudaSamples\..\shared\inc" -I"..\..\Programs\Qt5.1.1\5.1.1\mingw48_32\include" -I"..\..\Programs\Qt5.1.1\5.1.1\mingw48_32\include\QtGui" -I"..\..\Programs\Qt5.1.1\5.1.1\mingw48_32\include\QtCore" -I"release" -I"." -I"..\..\Programs\Qt5.1.1\5.1.1\mingw48_32\mkspecs\win32-g++" -o release\obj\main.o ..\Cuda\main.cpp
In file included from C:\Cuda\CudaToolkit\include/device_types.h:53:0,
                 from C:\Cuda\CudaToolkit\include/builtin_types.h:56,
                 from ..\Cuda\main.cpp:2:
C:\Cuda\CudaToolkit\include/host_defines.h:79:0: warning: "__cdecl" redefined [enabled by default]
 #define __cdecl
 ^
..\Cuda\main.cpp:1:0: note: this is the location of the previous definition
 #include <cuda.h>
 ^
In file included from C:\Cuda\CudaToolkit\include/builtin_types.h:60:0,
                 from ..\Cuda\main.cpp:2:
C:\Cuda\CudaToolkit\include/vector_types.h:73:0: warning: ignoring #pragma warning  [-Wunknown-pragmas]
 #pragma warning(push)
 ^
C:\Cuda\CudaToolkit\include/vector_types.h:74:0: warning: ignoring #pragma warning  [-Wunknown-pragmas]
 #pragma warning(disable: 4201 4408)
 ^
C:\Cuda\CudaToolkit\include/vector_types.h:350:0: warning: ignoring #pragma warning  [-Wunknown-pragmas]
 #pragma warning(pop)
 ^
g++ -Wl,-s -Wl,-subsystem,windows -mthreads -o release\Cuda.exe release/cuda/vectorAddition_cuda.o release/obj/main.o  -lglu32 -lopengl32 -lgdi32 -luser32 -lmingw32 -lqtmain -LC:\Cuda\CudaToolkit\lib\Win32 -LC:\Cuda\CudaSamples\common\lib\Win32 -LC:\Cuda\CudaSamples\..\shared\lib\Win32 -LC:\CUDA\VS10\VC\lib -LQMAKE_LIBS -L+= -L-lmsvcrt -L-llibcmt -L-llibcpmt -lcuda -lcudart -LF:\Programs\Qt5.1.1\5.1.1\mingw48_32\lib -lQt5Gui -lQt5Core 
Warning: .drectve `/FAILIFMISMATCH:"_MSC_VER=1600" /FAILIFMISMATCH:"_ITERATOR_DEBUG_LEVEL=0" /DEFAULTLIB:"libcpmt" /DEFAULTLIB:"LIBCMT" /DEFAULTLIB:"OLDNAMES" ' unrecognized
f:/programs/qt5.1.1/tools/mingw48_32/bin/../lib/gcc/i686-w64-mingw32/4.8.0/../../../../i686-w64-mingw32/lib/../lib/crt2.o:crtexe.c:(.text+0xcf): undefined reference to `_imp___fmode'
f:/programs/qt5.1.1/tools/mingw48_32/bin/../lib/gcc/i686-w64-mingw32/4.8.0/../../../../i686-w64-mingw32/lib/../lib/crt2.o:crtexe.c:(.text+0x2de): undefined reference to `_imp___acmdln'
f:/programs/qt5.1.1/tools/mingw48_32/bin/../lib/gcc/i686-w64-mingw32/4.8.0/../../../../i686-w64-mingw32/lib/../lib/crt2.o:crtexe.c:(.text+0x3cf): undefined reference to `_imp____initenv'
f:/programs/qt5.1.1/tools/mingw48_32/bin/../lib/gcc/i686-w64-mingw32/4.8.0/../../../../i686-w64-mingw32/bin/ld.exe: f:/programs/qt5.1.1/tools/mingw48_32/bin/../lib/gcc/i686-w64-mingw32/4.8.0/../../../../i686-w64-mingw32/lib/../lib/crt2.o: bad reloc address 0x0 in section `.CRT$XCAA'
collect2.exe: error: ld returned 1 exit status
Makefile.Release:78: recipe for target 'release\Cuda.exe' failed
mingw32-make[1]: Leaving directory 'F:/workspaceSchool/build-Cuda-Desktop_Qt_5_1_1_MinGW_32bit-Release'
makefile:34: recipe for target 'release' failed
mingw32-make[1]: *** [release\Cuda.exe] Error 1
mingw32-make: *** [release] Error 2
13:09:28: The process "F:\Programs\Qt5.1.1\Tools\mingw48_32\bin\mingw32-make.exe" exited with code 2.
Error while building/deploying project Cuda (kit: Desktop Qt 5.1.1 MinGW 32bit)
When executing step 'Make'
13:09:28: Elapsed time: 00:11.

And all issues (Can't post images due low rep): issues

If im not mistaken, then problem is with linking cuda object file (vectorAddition_cuda.o) which is compiled by nvvc.exe with cl.exe and with VS libs. Then MinGW( g++ compiler) is trying somehow link together cuda object file and c++ object file (main.o) with all libs into the executable file. g++ however doesnt know about VS runtime libs and is throwing these errors. So i guess i need somehow pass VS libs to g++. How can i fix this?

I'm a bit confused, are you using MinGW or Visual? The title seems to state that you are using MinGW but the project file seems to use a mix of both. You can't mix those two. If you compiled (or downloaded the binary directly from NVidia) CUDA with Visual Studio 2010, you HAVE to use VS10 to compile your project, otherwise it won't work.

I never used CUDA myself but it seems that the system requirements mention only Visual Studio 2008, 2010 and 2012. If you want to use it with Qt, it's possible, you just have to grab a Qt compiled with VS (there are builds for 32 and 64 bit for both on the download page . You can get Visual Studio Express for free as long as you don't create any commercial application with it.

To use QtCreator with the MSVC backend compiler go to Tools > Options > Build and Run > Kits and add a new Kit with the MSVC compiler, cdb as the debugger and the Qt version you just downloaded (it must have been compiled with the same Visual Studio version otherwise it won't work). Then open your project, go to the Projects tab (on the left) and select the Kit you just created. You should probably clean your .pro file as well before everything work smoothly.

On a side note, there are a few things that seems out of place in your linker line:

g++ -Wl,-s -Wl,-subsystem,windows -mthreads -o release\Cuda.exe release/cuda/vectorAddition_cuda.o release/obj/main.o  -lglu32 -lopengl32 -lgdi32 -luser32 -lmingw32 -lqtmain -LC:\Cuda\CudaToolkit\lib\Win32 -LC:\Cuda\CudaSamples\common\lib\Win32 -LC:\Cuda\CudaSamples\..\shared\lib\Win32 -LC:\CUDA\VS10\VC\lib -LQMAKE_LIBS -L+= -L-lmsvcrt -L-llibcmt -L-llibcpmt -lcuda -lcudart -LF:\Programs\Qt5.1.1\5.1.1\mingw48_32\lib -lQt5Gui -lQt5Core 

First this -L+= , which might be caused by the escaping backslash at the end of the QMAKE_LIBDIR .

Then the syntax -L-lmsvcrt seems wrong. It might be because you are using QMAKE_LIBS , I personally never had to use it, and according to the documentation you shouldn't either as it is an internal variable. Same goes for QMAKE_LIBDIR btw. I would just use the LIBS variable for any external dependency.

You can add these to your .pro file which be used to link to CUDA on Windows using VS2010 and on Linux:

# 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

win32:{
    CUDA_SDK = "C:/ProgramData/NVIDIA Corporation/NVIDIA GPU Computing SDK 5.5/C"   # Path to cuda SDK install
    CUDA_DIR = "C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v5.5"           # Path to cuda toolkit install
}
else: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
}

win32: SYSTEM_NAME = win32         # Depending on your system either 'Win32', 'x64', or 'Win64'
else: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
win32:{
    QMAKE_LIBDIR += $$CUDA_DIR/lib/$$SYSTEM_NAME
                    #$$CUDA_SDK/common/lib/$$SYSTEM_NAME \
                    #$$CUDA_SDK/../shared/lib/$$SYSTEM_NAME
}
else:unix:{
    QMAKE_LIBDIR += $$CUDA_DIR/lib
}


win32:{
# The following library conflicts with something in Cuda
QMAKE_LFLAGS_RELEASE = /NODEFAULTLIB:msvcrt.lib
QMAKE_LFLAGS_DEBUG   = /NODEFAULTLIB:msvcrtd.lib
}

# Add the necessary libraries
win32:{
    CUDA_LIBS = cuda cudart
    # 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 += $$join(CUDA_LIBS,'.lib ', '', '.lib')
}
else: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

    win32: cuda_d.commands = $$CUDA_DIR/bin/nvcc.exe -D_DEBUG $$NVCC_OPTIONS $$CUDA_INC $$NVCC_LIBS --machine $$SYSTEM_TYPE -arch=$$CUDA_ARCH -c -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME}
    else: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

    win32: cuda.commands = $$CUDA_DIR/bin/nvcc.exe $$NVCC_OPTIONS $$CUDA_INC $$NVCC_LIBS --machine $$SYSTEM_TYPE -arch=$$CUDA_ARCH -c -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME}
    else: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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM