Using Windows Forms (/clr) with CUDA

Anybody has any idea of incorporating CUDA properly with Windows Forms programs in Visual Studio 2005? I am stuck with this for quite some time and couldn’t resolve these errors

1>LIBCMTD.lib(stdexcpt.obj) : error LNK2005: “public: __thiscall std::exception::exception(class std::exception const &)” (??0exception@std@@QAE@ABV01@@Z) already defined in MSVCRTD.lib(MSVCR80D.dll)
1>LIBCMTD.lib(stdexcpt.obj) : error LNK2005: "public: virtual char const * __thiscall std::exception::what(void)const " (?what@exception@std@@UBEPBDXZ) already defined in MSVCRTD.lib(MSVCR80D.dll)
1>LIBCMTD.lib(dbgheap.obj) : error LNK2005: _malloc already defined in MSVCRTD.lib(MSVCR80D.dll)
1>LIBCMTD.lib(dbgheap.obj) : error LNK2005: __malloc_dbg already defined in MSVCRTD.lib(MSVCR80D.dll)
1>LIBCMTD.lib(dbgheap.obj) : error LNK2005: _calloc already defined in MSVCRTD.lib(MSVCR80D.dll)
1>LIBCMTD.lib(dbgheap.obj) : error LNK2005: _realloc already defined in MSVCRTD.lib(MSVCR80D.dll)
1>LIBCMTD.lib(dbgheap.obj) : error LNK2005: __realloc_dbg already defined in MSVCRTD.lib(MSVCR80D.dll)
1>LIBCMTD.lib(dbgheap.obj) : error LNK2005: _free already defined in MSVCRTD.lib(MSVCR80D.dll)
1>LIBCMTD.lib(dbgheap.obj) : error LNK2005: __free_dbg already defined in MSVCRTD.lib(MSVCR80D.dll)
1>LIBCMTD.lib(hooks.obj) : error LNK2005: “void __cdecl terminate(void)” (?terminate@@YAXXZ) already defined in MSVCRTD.lib(MSVCR80D.dll)
1>LIBCMTD.lib(_file.obj) : error LNK2005: ___iob_func already defined in MSVCRTD.lib(MSVCR80D.dll)
1>LIBCMTD.lib(dbghook.obj) : error LNK2005: __crt_debugger_hook already defined in MSVCRTD.lib(MSVCR80D.dll)
1>LIBCMTD.lib(typinfo.obj) : error LNK2005: “public: void __thiscall type_info::_type_info_dtor_internal_method(void)” (?_type_info_dtor_internal_method@type_info@@QAEXXZ) already defined in MSVCRTD.lib(MSVCR80D.dll)
1>LIBCMTD.lib(typinfo.obj) : error LNK2005: “private: __thiscall type_info::type_info(class type_info const &)” (??0type_info@@AAE@ABV0@@Z) already defined in MSVCRTD.lib(ti_inst.obj)
1>LIBCMTD.lib(typinfo.obj) : error LNK2005: “private: class type_info & __thiscall type_info::operator=(class type_info const &)” (??4type_info@@AAEAAV0@ABV0@@Z) already defined in MSVCRTD.lib(ti_inst.obj)
1>LIBCMTD.lib(printf.obj) : error LNK2005: _printf already defined in MSVCRTD.lib(MSVCR80D.dll)

I guess the command line commands used for cppIntegration will not work in this case. Anybody has a setting which works?

A cheap(?) hack could be changing the project settings and ignoring the LIBCMTD.lib library.

For this:

Go to the project settings → Linker → Input. There you would see an option “Ignore Specific Library” put in LIBCMTD.lib.

I hope this works.

Hi there. I have tried your way. The codes can compile. Unfortunately, the program exits itself once executed indicating that there is a corruption of the heap and there is a bug in my program.

This is what i am trying to do. I have been trying to call CUDA functions using my existing windows forms program in Visual Studio 2005. Then i encountered this LNK2005 problem which actually means that libraries are linked in the wrong order. To analyze the problem, I did a simple test. I start an entirely new windows forms applications and added a .cu and kernel file to it and i managed to get it compile properly. This is good news since it means that CUDA is working.

Then, what i did was to ensure that there are no linkage problems in my exisitng windows forms applications by making sure that everything compiles (before adding a .cu and kernel file - similar to what i did with the new windows forms applications which compiles the codes successfully). Then this LNK2005 error appears whenever i try to compile it. To my surprise, this problem will disappear immediately once i delete off the .cu and kernel file from the project. So is this something wrong with CUDA or something wrong with my application? Since LNK2005 will not appear once i remove the two files, it means my application is fine. On the other hand, CUDA can compile properly on a new windows forms project. I have totally no idea now.

Hmm… I must warn you that I am not an expert at this and though I have solved this once, it was a long time ago and a trial-and-error solution. So feel free to ignore my suggestions as they are based on the same t&e procedure I had followed then.

Another project setting that comes into play here if I remember right is the /MTd or /MT option. I think that is in the Project Options > C/C++ > Runtime Library. But I assume that you would have found this already since these show up as soon as you google the link error 2005.

The next thing I would do is check the #include statements in all my code and see if I can I have some file included twice (and remove the right one). I would also order these #includes as required (This ‘order’ is very vague to explain and what I had once done to check this was compare with the orders in the examples)
[Yes, by order I do mean whether #include<stdio.h> is before #include<abcd.h>] Especially check the afx*.h files.

By any chance, are you trying to use OpenGL on the winforms?
Also, could you find which line (of code) the program crashes at?

[url=“The Official NVIDIA Forums | NVIDIA”]The Official NVIDIA Forums | NVIDIA
talks about another project setting

Hi! Thanks for your suggestions. I did in fact came across the /MTd or /MT option. However, it creates another compilation error stating that /clr with /MTd or /MT is not compatible. So as a result, i am stuck with /MDd or /MD (with the latter producing less LNK2005 errors). Then, i cross check with the new windows forms application that i created before and this actually compiles without any problems with the /MDd option. So since this is not the problem, I performed a simple experiment. Firstly, i ensure that my current application (with my existing codes that i have developed so far) can be compiled without any CUDA files in the project. Then, what i did was to add two .cu files (one of them is the kernel file) and a .cuh file. These files are totally empty. Without specifying anything for the command line for the main .cu file, the program compiles without the LNK2005 error. However, once i specify the command line, outputs and additional dependencies under main.cu->properties->general, the LNK2005 error is generated. The following is the command line which i specified. Please tell me if there is any problem with it. Thanks!

$(CUDA_BIN_PATH)\nvcc.exe -ccbin “$(VCInstallDir)bin” -c -D_DEBUG -DWIN32 -D_CONSOLE -D_MBCS -Xcompiler /EHsc,/W3,/nologo,/Wp64,/Od,/Zi,/RTC1,/MTd -I"$(CUDA_INC_PATH)" -I"C:\Program Files\NVIDIA Corporation\NVIDIA CUDA SDK\common\inc" -L"C:\Program Files\NVIDIA Corporation\NVIDIA CUDA SDK\common\lib" -o $(ConfigurationName)\stereo.obj stereo.cu

i have tried different was of doing it. I have tried putting quotes around $(CUDA_BIN_PATH)\nvcc.exe (as suggested by one of the threads in the forum) and since i am using MDd, EHa in my main project, i changed the command line to MDd and EHa respectively. However, none of these helps. I have checked that the links to the libraries and include files in the main project properties are specified appropriately (similar to what i have specified in the new windows forms application and it is working, so i guess it must be right). So if you have any idea what this problem could be, please tell me. I am more than willing to try it out even if you are not sure whether it will work.

Hi, I have identified the main source of the problem. But before that, this is the setting. I have a number of files in my exisiting project which uses a number of different libraries. Without any changes, if i include the CUDA files into the project with the command line as stated in the previous post, then i will receive multiple LNK2005 errors during compilation. But keep in mind, these CUDA files are technically not linked to any other files in the project. In other words, these CUDA files are just sitting in the project and compiles during compilation but it is not linked to the other cpp files I have in the project. I break down my entire application and then identified that if i include files that links to the OpenSceneGraph libraries or ARIA libraries, then LNK 2005 will be generated. If i keep the files which links to these libraries out of the project, then it compiles well with CUDA files. However, technically speaking, these libraries shouldn’t conflict because it is not used by the same file. How can this happen and how can this be fixed?

So, did you do this already:

The next thing I would do is check the include statements in all my code and see if I can I have some file included twice (and remove the right one). I would also order these #includes as required (This ‘order’ is very vague to explain and what I had once done to check this was compare with the orders in the examples)

[Yes, by order I do mean whether #include<stdio.h> is before #include<abcd.h>] Especially check the afx*.h files.

Because this seems to be the only thing popping in my head right now.

Hey I talked with one of the ‘gurus’. This is what he said: (in my words :P )
There are 2 ways to solve it.

  1. Go through the stdafx and fix the order in which the libs get linked.
  2. In the Project Settings at the Ignore Specific Library -
    a. Ignore Libcmtd.lib
    b. Add (in this order) msvcrt80.lib , libcmtd.lib.

He told me to google ‘weak linking’ for further research on the topic. I did and there are at least a couple of codeguru posts that sounds very similar to what we have here :) links: [url=“http://www.codeguru.com/forum/archive/index.php/t-102337.html”]http://www.codeguru.com/forum/archive/inde...p/t-102337.html[/url]
[url=“http://www.codeguru.com/forum/archive/index.php/t-112676.html”]http://www.codeguru.com/forum/archive/inde...p/t-112676.html[/url] (this also has a link to Microsoft’s KB article about this topic)

When I reached that KB article I remembered thats how I had solved my problem the last time… just had reached it some other way :D

Hope this helps and you get to some awesome code running External Image

Hi there! Thanks alot for your suggestions. These articles are really good. Unfortunately, I still could not compile my codes as it is. It seems that CUDA libraries will not work with the external libraries that I have. I tried both methods in the article (the force linker way and the /verbose way to identify the libraries which cause the problem). Since the external libraries i am using are not within my control (as to how they are compiled), i guess i have not much options but to have a major rennovation on my codes to incorporate CUDA. Anyways, although this was not successfuly, but in the process, i did learn alot. Thanks!

The problem is that only the multithreading( /MT ) CUDA libraries is deployed.
NVIDIA should deploy the following cuda runtime libraries:

cudart_vs2005_MT.lib → VS2005 multithreaded release
cudart_vs2005_MTd.lib → VS2005 multithreaded debug
cudart_vs2005_MD.lib → VS2005 multithreaded DLL release
cudart_vs2005_MDd.lib → VS2005 multithreaded DLL debug

and also add some __cplusplus_cli in the headers to support .NET/Managed C++/CLR IJW… and also to add /EHa exception support.

Currently only the /MT multithreaded release libraries are provided… so you will get a lot of C CRT conflicts, leaks and compilation/linking errors if you use a different model than that… the problem is specially bad for Managed C++/WinForms in debug mode…

@NVIDIA, Is there anything that you would like to tell us about this?

Last time, I tried using /CLR, NVCC just abruptly aborted (not even a linker error).

Best Regards,

Sarnath