# Creating a CUDA DLL

**URL:** <https://forums.developer.nvidia.com/t/creating-a-cuda-dll/2357>\
**Category:** CUDA Programming and Performance\
**Created:** [January 17, 2008, 4:51pm UTC](https://forums.developer.nvidia.com/t/creating-a-cuda-dll/2357 "2008-01-17T16:51:09Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![papag62](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@papag62](https://forums.developer.nvidia.com/u/papag62)\
**Post date:** [January 17, 2008, 4:51pm UTC](https://forums.developer.nvidia.com/t/creating-a-cuda-dll/2357/1 "2008-01-17T16:51:09Z")

</div>

I have been trying to use CUDA to remove lens distortion and do stereo rectification on a calibrated stereo camera pair. A majority of what I am doing used OpenCV. What I would like to do is create a .dll from my cuda project that just takes two pixel arrays as input and outputs two new pixel arrays. How would I go about doing this? I am using VS2005.

Thanks  
PS: The reason I need the .dll is because I was unable to link to the opencv libraries in c but can in c++. If anybody has any other suggestions it will be appreciated.

---

<div class="post-metadata">

**Author:** ![AustinMcElroy](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@AustinMcElroy](https://forums.developer.nvidia.com/u/AustinMcElroy)\
**Post date:** [January 17, 2008, 5:10pm UTC](https://forums.developer.nvidia.com/t/creating-a-cuda-dll/2357/2 "2008-01-17T17:10:31Z")

</div>

> [@](#):
>
> I have been trying to use CUDA to remove lens distortion and do stereo rectification on a calibrated stereo camera pair. A majority of what I am doing used OpenCV. What I would like to do is create a .dll from my cuda project that just takes two pixel arrays as input and outputs two new pixel arrays. How would I go about doing this? I am using VS2005.
> 
> Thanks
> 
> PS: The reason I need the .dll is because I was unable to link to the opencv libraries in c but can in c++. If anybody has any other suggestions it will be appreciated.
> 
> [snapback]311350[/snapback]

Specifically, what help do you need? Do you have the program written and compiled in C and just need to turn it into .dll? Do you know how to write DLLs but not CUDA programs?

It is definitely possible to write a DLL, as I am currently running CUDA through Labview via a DLL.

Thanks,

Austin

---

<div class="post-metadata">

**Author:** ![papag62](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@papag62](https://forums.developer.nvidia.com/u/papag62)\
**Post date:** [January 17, 2008, 5:27pm UTC](https://forums.developer.nvidia.com/t/creating-a-cuda-dll/2357/3 "2008-01-17T17:27:13Z")

</div>

I have no problem generating and compiling CUDA code. I don’t have this specific code written yet though, it’s about ~80%. I need to know how to write DLLs and compile to a DLL.

---

<div class="post-metadata">

**Author:** ![AustinMcElroy](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@AustinMcElroy](https://forums.developer.nvidia.com/u/AustinMcElroy)\
**Post date:** [January 17, 2008, 7:34pm UTC](https://forums.developer.nvidia.com/t/creating-a-cuda-dll/2357/4 "2008-01-17T19:34:59Z")

</div>

> [@](#):
>
> I have no problem generating and compiling CUDA code. I don’t have this specific code written yet though, it’s about ~80%. I need to know how to write DLLs and compile to a DLL.
> 
> [snapback]311367[/snapback]

Ok, DLLs aren’t to terrible to create. The basic DLL structure is as follows:

#include \<allmyheadershere.h\>

void FunctionDeclarationHere(void){

```
 return;

```

}

\_\_declspec(dllexport) void GPU\_Code(float \*ptrVarOne, float \*ptrVarTwo)

{

```
FunctionDeclarationHere();

return;

```

}

To configure VS2005 do the following:

1.) Goto Project Properties → Configuration Properties → Project Details → Configuration Type → Change to DLL

2.) Goto Project Properties → Configuration Properties → Linker → General → Change the Output File to something that ends in .DLL

I think that is all. It has been a while since I actually configured the project, so let me know and I can hunt through the project properties again.

Thanks,

Austin

---

<div class="post-metadata">

**Author:** ![papag62](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@papag62](https://forums.developer.nvidia.com/u/papag62)\
**Post date:** [January 17, 2008, 7:47pm UTC](https://forums.developer.nvidia.com/t/creating-a-cuda-dll/2357/5 "2008-01-17T19:47:07Z")

</div>

Just to make sure I understand the FunctionDeclarationHere() is my cuda c function that has the call to the CUDA kernel and CPU\_Code() would be the function used when accesing the dll from another project. Is my understanding correct?

Thanks for your time.

---

<div class="post-metadata">

**Author:** ![AustinMcElroy](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@AustinMcElroy](https://forums.developer.nvidia.com/u/AustinMcElroy)\
**Post date:** [January 17, 2008, 7:52pm UTC](https://forums.developer.nvidia.com/t/creating-a-cuda-dll/2357/6 "2008-01-17T19:52:41Z")

</div>

> [@](#):
>
> Just to make sure I understand the FunctionDeclarationHere() is my cuda c function that has the call to the CUDA kernel and CPU\_Code() would be the function used when accesing the dll from another project. Is my understanding correct?
> 
> Thanks for your time.
> 
> [snapback]311432[/snapback]

Yes, I think that is correct. The example, when applied to CUDA, should look like:

#include \<allmyheadershere.h\>

**global** FunctionDeclarationHere(void){ //GPU function

return;

}

\_\_declspec(dllexport) void GPU\_Code(float \*ptrVarOne, float \*ptrVarTwo)

{

\<\<\<x,y,z\>\>\>FunctionDeclarationHere();

return;

}

---

<div class="post-metadata">

**Author:** ![papag62](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@papag62](https://forums.developer.nvidia.com/u/papag62)\
**Post date:** [January 17, 2008, 7:58pm UTC](https://forums.developer.nvidia.com/t/creating-a-cuda-dll/2357/7 "2008-01-17T19:58:16Z")

</div>

Thanks alot I’ll give that a try and let you know how things go.

---

<div class="post-metadata">

**Author:** ![AustinMcElroy](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@AustinMcElroy](https://forums.developer.nvidia.com/u/AustinMcElroy)\
**Post date:** [February 11, 2008, 6:46pm UTC](https://forums.developer.nvidia.com/t/creating-a-cuda-dll/2357/8 "2008-02-11T18:46:08Z")

</div>

> [@](#):
>
> Thanks alot I’ll give that a try and let you know how things go.
> 
> [snapback]311437[/snapback]

Did this ever work out for you?

---

<div class="post-metadata">

**Author:** ![papag62](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@papag62](https://forums.developer.nvidia.com/u/papag62)\
**Post date:** [February 11, 2008, 8:37pm UTC](https://forums.developer.nvidia.com/t/creating-a-cuda-dll/2357/9 "2008-02-11T20:37:48Z")

</div>

I was distracted by some other work and have never gotten around to trying it.

---

<div class="post-metadata">

**Author:** ![casybaby](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@casybaby](https://forums.developer.nvidia.com/u/casybaby)\
**Post date:** [June 1, 2008, 1:02am UTC](https://forums.developer.nvidia.com/t/creating-a-cuda-dll/2357/10 "2008-06-01T01:02:51Z")

</div>

Hi all, I am working on JNI and CUDA also. I am using Visual Studio 2005 with the CUDA build rule. The jni.h is included to my .cu file, and the directories for jni.h is set by adding $(JAVA\_HOME)\include and $(JAVA\_HOME)\include\win32 to myProject-\>Properties-\>Configuration Properties-\>C/C+±\>General-\>Additional Include Directories; and by adding $(JAVA\_HOME)\lib to Linker-\>Additional Library Directories. But I still get the error says that jni.h not found. I am wondering if it is because I am using cuda’s compiler that VS cannot recognize the path I set. Any suggestion of how to set the JNI environment will be appreciated.

---

<div class="post-metadata">

**Author:** ![espe](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@espe](https://forums.developer.nvidia.com/u/espe)\
**Post date:** [August 10, 2008, 10:38pm UTC](https://forums.developer.nvidia.com/t/creating-a-cuda-dll/2357/11 "2008-08-10T22:38:30Z")

</div>

Doesn’t work for me.  
Unresolved external symbol.  
Has anyone else tried it?

---

<div class="post-metadata">

**Author:** ![espe](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@espe](https://forums.developer.nvidia.com/u/espe)\
**Post date:** [August 10, 2008, 11:02pm UTC](https://forums.developer.nvidia.com/t/creating-a-cuda-dll/2357/12 "2008-08-10T23:02:02Z")

</div>

OK, I guess there are some naming conventions.  
I suggest that you should use a tool like DLL Export to see the real names of the functions that are inside the DLL.

Once the names are correct everything works fine (hopefully)…

---

<div class="post-metadata">

**Author:** ![Sarnath](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/sarnath/32/9742_2.png) [@Sarnath](https://forums.developer.nvidia.com/u/Sarnath)\
**Post date:** [August 12, 2008, 4:58am UTC](https://forums.developer.nvidia.com/t/creating-a-cuda-dll/2357/13 "2008-08-12T04:58:52Z")

</div>

Austin,

Jus my 2 cents:

**First cent:**

function declaration is different from function definition. I think you have mixed both of them in your replies.

```auto
void gpuKernel(float *, int ); --- function declaration

```

```auto
void gpuKernel(float *b, int n) -- function definition

{

 Â  for(int i=blockIdx.x*blockDim.x+threadIdx.x; Â i<n; i+= gridDim.x*blockDim.x)

 Â  {

 Â  Â  Â  b[i] = 0;

 Â  }

}

```

**Second cent:**

While linking DLLs to applications, you should link against the “LIB” and not the DLL file. If you do it against the DLL - you will get LNK1107 “invalid or corrupt file! Cannot read offset 0x1F0” or some mesage like that from the linker!

Linking against the LIB is the best thing to do - It is called implicit linking and the DLL function call in your application looks the same as any other function call!

Best Regards,

Sarnath

---

<div class="post-metadata">

**Author:** ![Aric](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Aric](https://forums.developer.nvidia.com/u/Aric)\
**Post date:** [August 21, 2008, 1:46pm UTC](https://forums.developer.nvidia.com/t/creating-a-cuda-dll/2357/14 "2008-08-21T13:46:51Z")

</div>

Hello,

If you want that your dll contains the functions with the good names you must declare your functions like that

```auto
extern "C" __declspec(dllexport) int myfunction(int a);

```

---

<div class="post-metadata">

**Author:** ![impravin22](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@impravin22](https://forums.developer.nvidia.com/u/impravin22)\
**Post date:** [August 12, 2019, 3:35am UTC](https://forums.developer.nvidia.com/t/creating-a-cuda-dll/2357/15 "2019-08-12T03:35:11Z")

</div>

Hello

Can anyone tell me an example of creating CUDA DLL and how to call it in FORTRAN.

Best Regards  
Praveen

---

<div class="post-metadata">

**Author:** ![njuffa](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@njuffa](https://forums.developer.nvidia.com/u/njuffa)\
**Post date:** [August 12, 2019, 1:53pm UTC](https://forums.developer.nvidia.com/t/creating-a-cuda-dll/2357/16 "2019-08-12T13:53:14Z")

</div>

You can call CUDA code in a DLL from Fortran the same way you call any C API in a DLL from Fortran.

Here is a very _simplistic_ example. I used the Microsoft C/C++ compiler and Intel Fortran on Windows here. For a real project, you would probably want to read up on how to use ISO C bindings in Fortran.

We have three source files: cuda\_dll.cu and cuda\_dll.h for building the DLL, and the main program written in Fortan in main.f90 that calls into the DLL.

cuda\_dll.cu:

```auto
#include <stdio.h>
#include <stdlib.h>
#include "cuda_dll.h"

__global__ void kernel (int n)
{
    printf ("kernel: n = %d\n", n);
}

void wrapper (int *n)
{
    printf ("wrapper: calling kernel()\n");
    kernel<<<1,1>>>(*n);
    cudaDeviceSynchronize();
}

```

cuda\_dll.h:

```auto
#ifdef CUDADLL_EXPORTS
    #define DLLEXPORT __declspec(dllexport)
#else
    #define DLLEXPORT __declspec(dllimport)
#endif

extern "C" DLLEXPORT void wrapper (int *n);

```

main.f90:

```auto
program call_cuda
write (*,*) "Calling wrapper from Fortran"
call wrapper (5)
stop
end program call_cuda

```

Build the DLL (which on Windows results in two files, the actual dynamically linked library with a .dll extension and a stub library with the .lib extension):

```auto
nvcc --shared -DCUDADLL_EXPORTS -o cuda_dll.dll cuda_dll.cu

```

Now use the Intel Fortran compiler, ifort, to build the main program and link to the library. Note that we need to adjust the symbol generation of the Fortran compiler to generate lowercase symbol names to match the symbols exported from the library:

```auto
ifort -names:lowercase -Fefortran_main.exe main.f90 cuda_dll.lib

```

Now run the resulting executable fortran\_main.exe, which produces the following output:

```auto
Calling wrapper from Fortran
wrapper: calling kernel()
kernel: n = 5

```
