5

I tried to compile a cuda file in Qt 5.2 and MSVC2012 environment. Before I started my project, I carefully read the question and reply in :Compiling Cuda code in Qt Creator on Windows. But there are still some errors popping out even though I simply copied the code and generated 2 files in qt

/main.cpp
/vectorAddition.cu

the error is :

error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MDd_DynamicDebug' doesn't match value 'MTd_StaticDebug' in vectorAddition_cuda.o

I just totally have no idea how to fix this bug. I read some article online about fixing the mismatch in MSVC2012. But few of them mentioned how to do it in Qt .pro file. Does any one happen to have this problem?

I also attached the pro file here (with minor modified according to my own directory)

#-------------------------------------------------
#
# 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
}

And here is my log

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.

Explanation for "question duplication":

I also read the article Mismatch Detected for 'RuntimeLibrary' But the difference is that my question is about NVCC set up in Qt which is more complex than in MSVC environment. Even though /MDd is set by the following code, without the correct setting of NVCC, cu file still not able to compile. Simple change of runtime library did not totally solve my question. It is the setting of NVCC eventually made it.

# 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"
Community
  • 1
  • 1
jiangstonybrook
  • 133
  • 2
  • 9
  • I voted for a reopen. Should this reopen, could you please move the solution to the answer? Thanks. – JasonMArcher Oct 06 '14 at 16:34
  • @JasonMArcher Do you want me to restart a new question ? i am new here not familiar with the operation. – jiangstonybrook Oct 06 '14 at 17:21
  • @jiang You should write an answer to your own question below, with the solution you show in your question. After that, edit your question to remove that part. – rubenvb Oct 06 '14 at 17:59
  • No. I assume the last part of your post is the solution to the problem? If so, then move it to a new answer below. Then you can mark it Accepted (next to the voting arrows) and people will be able to see clearly the solution. :) – JasonMArcher Oct 06 '14 at 18:00
  • @JasonMArcher I just post the solution. There will be 24 hours delay for marking it as Accepted. Thank you for your guidance:) – jiangstonybrook Oct 06 '14 at 18:27

1 Answers1

5

I got my question solved by reading another article http://robot9.me/cuda_qtcreator_windows/

As the question Mismatch Detected for 'RuntimeLibrary' suggested. MSVCRT link option needs to keep consistent. So the /MDd setting is added before NVCC config.

But the key is to set cuda.commands correctly. Different from question: Compiling Cuda code in Qt Creator on Windows (i tried that config but does not work for me) my nvcc is set like this:

   # 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"

   CONFIG(debug, debug|release) {
    #Debug settings
    # Debug mode
    cuda_d.input    = CUDA_SOURCES
    cuda_d.output   = $$CUDA_OBJECTS_DIR/${QMAKE_FILE_BASE}_cuda.obj
    cuda_d.commands = $$CUDA_DIR/bin/nvcc.exe -D_DEBUG $$NVCC_OPTIONS $$CUDA_INC $$LIBS \
                      --machine $$SYSTEM_TYPE -arch=$$CUDA_ARCH \
                      --compile -cudart static -g -DWIN32 -D_MBCS \
                      -Xcompiler "/wd4819,/EHsc,/W3,/nologo,/Od,/Zi,/RTC1" \
                      -Xcompiler $$MSVCRT_LINK_FLAG_DEBUG \
                      -c -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME}
    cuda_d.dependency_type = TYPE_C
    QMAKE_EXTRA_COMPILERS += cuda_d
}
else {
     # Release settings
     cuda.input    = CUDA_SOURCES
     cuda.output   = $$CUDA_OBJECTS_DIR/${QMAKE_FILE_BASE}_cuda.obj
     cuda.commands = $$CUDA_DIR/bin/nvcc.exe $$NVCC_OPTIONS $$CUDA_INC $$LIBS \
                    --machine $$SYSTEM_TYPE -arch=$$CUDA_ARCH \
                    --compile -cudart static -DWIN32 -D_MBCS \
                    -Xcompiler "/wd4819,/EHsc,/W3,/nologo,/O2,/Zi" \
                    -Xcompiler $$MSVCRT_LINK_FLAG_RELEASE \
                    -c -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME}
     cuda.dependency_type = TYPE_C
     QMAKE_EXTRA_COMPILERS += cuda
}

Even though I still do not fully understand some of cuda.commands, but it really works. Please refer to http://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/index.html#axzz3FOBrgXpc for more detail of NVCC config.

Here is the full version of my .pro file

#-------------------------------------------------
#
# Project created by QtCreator 2013-09-01T16:29:35
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = FFTW //your project name
TEMPLATE = app

HEADERS  += dialog.h \
    include/fftw3.h \   //header i need for my project 


SOURCES += main.cpp\
           dialog.cpp


INCLUDEPATH += $$PWD/
DEPENDPATH += $$PWD/

DESTDIR = debug           
OBJECTS_DIR = debug/obj           # directory where .obj files will be saved
CUDA_OBJECTS_DIR = debug/obj      # directory where .obj  of cuda file will be saved
# This makes the .cu files appear in your project

OTHER_FILES += intmapping.cu      # this is my cu file need to compile

# CUDA settings <-- may change depending on your system

CUDA_SOURCES += intmapping.cu     # let NVCC know which file you want to compile CUDA NVCC

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"
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_20           # Type of CUDA architecture, for example 'compute_10', 'compute_11', 'sm_10'
NVCC_OPTIONS += --use_fast_math # default setting

# include paths

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


# library directories
QMAKE_LIBDIR += $$join(CUDA_DIR,'" -I"','-I"','"')/lib/$$SYSTEM_NAME\
                $$join(CUDA_SDK,'" -I"','-I"','"')/common/lib/$$SYSTEM_NAME\
                $$join(CUDA_SDK,'" -I"','-I"','"')/../shared/lib/$$SYSTEM_NAME

# Add the necessary libraries
CUDA_LIBS= -lcuda -lcudart -lcufft
#add quotation for those directories contain space (Windows required)
CUDA_INC +=$$join(INCLUDEPATH,'" -I"','-I"','"')

LIBS += $$CUDA_LIBS
#nvcc config
# 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"

CONFIG(debug, debug|release) {
    #Debug settings
    # Debug mode
    cuda_d.input    = CUDA_SOURCES
    cuda_d.output   = $$CUDA_OBJECTS_DIR/${QMAKE_FILE_BASE}_cuda.obj
    cuda_d.commands = $$CUDA_DIR/bin/nvcc.exe -D_DEBUG $$NVCC_OPTIONS $$CUDA_INC $$LIBS \
                      --machine $$SYSTEM_TYPE -arch=$$CUDA_ARCH \
                      --compile -cudart static -g -DWIN32 -D_MBCS \
                      -Xcompiler "/wd4819,/EHsc,/W3,/nologo,/Od,/Zi,/RTC1" \
                      -Xcompiler $$MSVCRT_LINK_FLAG_DEBUG \
                      -c -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME}
    cuda_d.dependency_type = TYPE_C
    QMAKE_EXTRA_COMPILERS += cuda_d
}
else {
     # Release settings
     cuda.input    = CUDA_SOURCES
     cuda.output   = $$CUDA_OBJECTS_DIR/${QMAKE_FILE_BASE}_cuda.obj
     cuda.commands = $$CUDA_DIR/bin/nvcc.exe $$NVCC_OPTIONS $$CUDA_INC $$LIBS \
                    --machine $$SYSTEM_TYPE -arch=$$CUDA_ARCH \
                    --compile -cudart static -DWIN32 -D_MBCS \
                    -Xcompiler "/wd4819,/EHsc,/W3,/nologo,/O2,/Zi" \
                    -Xcompiler $$MSVCRT_LINK_FLAG_RELEASE \
                    -c -o ${QMAKE_FILE_OUT} ${QMAKE_FILE_NAME}
     cuda.dependency_type = TYPE_C
     QMAKE_EXTRA_COMPILERS += cuda
}

FORMS += \
     dialog.ui

win32: LIBS += -L$$PWD/lib/ -llibfftw3f-3 #library i need in my project

INCLUDEPATH += $$PWD/lib
DEPENDPATH += $$PWD/lib
Community
  • 1
  • 1
jiangstonybrook
  • 133
  • 2
  • 9
  • This helped me . The -Xcompiler tells compiler how to do preprocess/compile **host** code that may be linked with your other parts of (pure host) code later. That is why(and how) to keep the MT/MD thing the same in the two parts of code. – squid May 24 '16 at 02:27