在QTCreator中用MinGW编译c ++和cuda代码

我试图编译简单的CUDA程序(我从编译在Qt Creator在Windows上的CUDA代码的源代码)这是我的.PRO文件:

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. 

这是编译器输出:

 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. 

和所有问题(不能张贴图像由于低代表): 问题

如果我没有弄错,那么问题是连接由nvvc.exe编译的cuda目标文件(vectorAddition_cuda.o)与cl.exe和VS libs。 然后MinGW( g ++编译器)尝试以某种方式将cuda对象文件和c ++对象文件(main.o)与所有库链接到可执行文件中。 g ++但是不知道VS运行库,并抛出这些错误。 所以我想我需要以某种方式将VS库传递给g ++。 我怎样才能解决这个问题?

Solutions Collecting From Web of "在QTCreator中用MinGW编译c ++和cuda代码"

我有点困惑,你使用MinGW还是Visual? 标题似乎说明您正在使用MinGW,但项目文件似乎使用两者的组合。 你不能混合这两个。 如果您使用Visual Studio 2010编译(或直接从NVidia下载二进制文件)CUDA,则必须使用VS10编译您的项目,否则将无法工作。

我从来没有使用过CUDA,但似乎系统要求只提到Visual Studio 2008,2010和2012.如果你想使用它与Qt,这是可能的,你只需要抓住一个Qt与VS编译(有32的版本在下载页面上都是64位,只要你没有创建任何商业应用程序,你可以免费获得Visual Studio Express。

要将QtCreator与MSVC后端编译器一起使用,请转到工具>选项>构建和运行>工具包,然后添加一个带有MSVC编译器的新工具包,作为调试器的cdb和刚刚下载的Qt版本(必须使用相同的Visual编译工作室版本,否则将无法正常工作)。 然后打开您的项目,转到项目选项卡(左侧),并选择您刚刚创建的套件。 你应该清理你的.pro文件,一切工作顺利。

在附注中,有一些东西在你的连接器行中似乎不合适:

 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 

首先这个-L+= ,这可能是由QMAKE_LIBDIR结尾处的反斜线引起的。

那么语法-L-lmsvcrt似乎是错误的。 这可能是因为你使用QMAKE_LIBS ,我个人从来没有使用它,根据文档,你不应该因为它是一个内部变量。 顺便说一句QMAKE_LIBDIR btw。 我只是使用LIBS变量的任何外部依赖。

您可以将这些添加到您的.pro文件,该文件用于链接到Windows上的CUDA使用VS2010和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 }