我在Qt 5.2和MSVC2012环境下尝试编译一个CUDA文件。在开始我的项目之前,我仔细阅读了这个问题和回答:在Windows上使用Qt Creator编译CUDA代码。但是,即使我只是复制了代码并在Qt中生成了2个文件,仍然出现了一些错误。
错误信息如下:
/main.cpp
/vectorAddition.cu
错误信息如下:
error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MDd_DynamicDebug' doesn't match value 'MTd_StaticDebug' in vectorAddition_cuda.o
我完全不知道如何修复这个bug。我在网上读了一些关于如何解决MSVC2012中的不匹配问题的文章,但很少有人提到如何在Qt的.pro文件中解决它。有人遇到过这个问题吗?
我还附上了这里的pro文件(根据我的目录进行了小修改)
#-------------------------------------------------
#
# Project created by QtCreator 2014-10-05T13:22:19
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = untitled
TEMPLATE = app
SOURCES += main.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
# Define output directories
DESTDIR = debug
OBJECTS_DIR = debug/obj
CUDA_OBJECTS_DIR = debug/obj
# Source files
# 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:/ProgramData/NVIDIA Corporation/NVIDIA GPU Computing SDK 4.0/C" # Path to cuda SDK install
CUDA_DIR = "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.5" # 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_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
QMAKE_LIBDIR += $$CUDA_DIR/lib/$$SYSTEM_NAME \
$$CUDA_SDK/common/lib/$$SYSTEM_NAME \
$$CUDA_SDK/../shared/lib/$$SYSTEM_NAME
# Add the necessary libraries
LIBS += -lcuda -lcudart
# MSVCRT link option (static or dynamic, it must be the same with your Qt SDK link option)
MSVCRT_LINK_FLAG_DEBUG = "/MDd"
MSVCRT_LINK_FLAG_RELEASE = "/MD"
# The following makes sure all path names (which often include spaces) are put between quotation marks
CUDA_INC = $$join(INCLUDEPATH,'" -I"','-I"','"')
# 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
cuda_d.commands = $$CUDA_DIR/bin/nvcc.exe -D_DEBUG $$NVCC_OPTIONS $$CUDA_INC $$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
cuda.commands = $$CUDA_DIR/bin/nvcc.exe $$NVCC_OPTIONS $$CUDA_INC $$LIBS --machine $$SYSTEM_TYPE -arch=$$CUDA_ARCH -c -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME}
cuda.dependency_type = TYPE_C
QMAKE_EXTRA_COMPILERS += cuda
}
这是我的日志
13:58:14: Running steps for project untitled...
13:58:14: Configuration unchanged, skipping qmake step.
13:58:14: Starting: "C:\Qt\Qt5.2.0\Tools\QtCreator\bin\jom.exe"
C:\Qt\Qt5.2.0\Tools\QtCreator\bin\jom.exe -f Makefile.Debug
"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.5\bin\nvcc.exe" -D_DEBUG --use_fast_math -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.5/include" -I"C:/ProgramData/NVIDIA Corporation/NVIDIA GPU Computing SDK 4.0/C/common/inc/" -I"C:/ProgramData/NVIDIA Corporation/NVIDIA GPU Computing SDK 4.0/C/../shared/inc/" -lcuda -lcudart --machine 32 -arch=sm_30 -c -o debug\obj\vectorAddition_cuda.obj ..\untitled\vectorAddition.cu
vectorAddition.cu
echo 1 /* CREATEPROCESS_MANIFEST_RESOURCE_ID */ 24 /* RT_MANIFEST */ "debug\\untitled.exe.embed.manifest">debug\untitled.exe_manifest.rc
if not exist debug\untitled.exe if exist debug\untitled.exe.embed.manifest del debug\untitled.exe.embed.manifest
if exist debug\untitled.exe.embed.manifest copy /Y debug\untitled.exe.embed.manifest debug\untitled.exe_manifest.bak
link /NOLOGO /DYNAMICBASE /NXCOMPAT /NODEFAULTLIB:msvcrtd.lib /SUBSYSTEM:WINDOWS "/MANIFESTDEPENDENCY:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' publicKeyToken='6595b64144ccf1df' language='*' processorArchitecture='*'" /MANIFEST /MANIFESTFILE:debug\untitled.exe.embed.manifest /OUT:debug\untitled.exe @C:\Users\JYOU\AppData\Local\Temp\untitled.exe.4748.3744.jom
main.obj : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MDd_DynamicDebug' doesn't match value 'MTd_StaticDebug' in vectorAddition_cuda.obj
moc_mainwindow.obj : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MDd_DynamicDebug' doesn't match value 'MTd_StaticDebug' in vectorAddition_cuda.obj
LINK : fatal error LNK1181: cannot open input file 'C:\Program.obj'
jom: C:\Qt\Qt5.2.0\Tools\QtCreator\bin\build-untitled-Desktop_Qt_5_2_0_MSVC2012_32bit-Debug\Makefile.Debug [debug\untitled.exe] Error 1181
jom: C:\Qt\Qt5.2.0\Tools\QtCreator\bin\build-untitled-Desktop_Qt_5_2_0_MSVC2012_32bit-Debug\Makefile [debug] Error 2
13:58:18: The process "C:\Qt\Qt5.2.0\Tools\QtCreator\bin\jom.exe" exited with code 2.
Error while building/deploying project untitled (kit: Desktop Qt 5.2.0 MSVC2012 32bit)
When executing step 'Make'
13:58:18: Elapsed time: 00:04.
"问题重复"的解释:
我还阅读了这篇文章Mismatch Detected for 'RuntimeLibrary'。但是,不同之处在于我的问题涉及在Qt中设置NVCC,这比在MSVC环境中更加复杂。即使通过以下代码设置了/MDd,如果没有正确设置NVCC,cu文件仍然无法编译。简单更改运行时库并未完全解决我的问题。最终是NVCC的设置解决了问题。# MSVCRT link option (static or dynamic, it must be the same with your Qt SDK link option)
MSVCRT_LINK_FLAG_DEBUG = "/MDd"
MSVCRT_LINK_FLAG_RELEASE = "/MD"