# nvcc linking problem

**URL:** <https://forums.developer.nvidia.com/t/nvcc-linking-problem/13916>\
**Category:** CUDA Programming and Performance\
**Created:** [December 20, 2009, 2:39pm UTC](https://forums.developer.nvidia.com/t/nvcc-linking-problem/13916 "2009-12-20T14:39:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nuliknol](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@nuliknol](https://forums.developer.nvidia.com/u/nuliknol)\
**Post date:** [December 20, 2009, 2:39pm UTC](https://forums.developer.nvidia.com/t/nvcc-linking-problem/13916/1 "2009-12-20T14:39:03Z")

</div>

Hi,

this is the continuation of the same problem i posted here before but found no solution.

The problem is , when i compile .cu files with nvcc it adds strange characters into the function name, and because of this i can not link it with another code i compile with mpicc. (it gives “undefined reference” error )

For example this simple function:

```auto
void gpu_init() {

	cudaSetDevice( cutGetMaxGflopsDeviceId() );

}

```

is compiled into :

```auto
0000000000011808 <_Z8gpu_initv>:

   11808: 55 push %rbp

   11809: 48 89 e5 mov	%rsp,%rbp

   1180c: e8 00 00 00 00 callq 11811 <_Z8gpu_initv+0x9>

   11811: 89 c7 mov	%eax,%edi

   11813: e8 00 00 00 00 callq 11818 <_Z8gpu_initv+0x10>

   11818: c9 leaveq 

   11819: c3 retq

```

when i link with another object file that uses this function i get:

```auto
main-master.c:(.text+0xa7f): undefined reference to `gpu_init'

```

my question is , how do i tell nvcc to compile the what that it doesn’t add the “\_Z8” and “v” characters into the function name when it creates the object file? (declaring ‘extern’ doesn’t help, because nvcc changes the function name, while other compilers do not)

Thanks in advance

---

<div class="post-metadata">

**Author:** ![avidday](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/avidday/32/59219_2.png) [@avidday](https://forums.developer.nvidia.com/u/avidday)\
**Post date:** [December 20, 2009, 2:51pm UTC](https://forums.developer.nvidia.com/t/nvcc-linking-problem/13916/2 "2009-12-20T14:51:41Z")

</div>

The “strange” characters are the product standard C++ function name mangling, and it isn’'t nvcc that is doing it, it is your host compiler, which is treating the host cost as C++. If you declared functions like this:

```auto
extern "C" void func();

```

the problem should go away, There should also be a nvcc switch to force it to treat host code in .cu files as C as default, but off the top of my head I can’t recall what it is, but I am sure the nvcc manual will reveal all. Either approach should fix your problem.

---

<div class="post-metadata">

**Author:** ![nuliknol](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@nuliknol](https://forums.developer.nvidia.com/u/nuliknol)\
**Post date:** [December 20, 2009, 4:10pm UTC](https://forums.developer.nvidia.com/t/nvcc-linking-problem/13916/3 "2009-12-20T16:10:33Z")

</div>

> [@](#):
>
> ```auto
> extern "C" void func();
> 
> ```

thanks. just “extern” wasn’t enough, but the “C” addition did the job

---

<div class="post-metadata">

**Author:** ![Gregory\_Diamos](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Gregory\_Diamos](https://forums.developer.nvidia.com/u/Gregory_Diamos)\
**Post date:** [December 20, 2009, 6:21pm UTC](https://forums.developer.nvidia.com/t/nvcc-linking-problem/13916/4 "2009-12-20T18:21:18Z")

</div>

> [@](#):
>
> thanks. just “extern” wasn’t enough, but the “C” addition did the job

From http://www.faqs.org/docs/Linux-mini/C+±dlopen.html :

Warning

There are two different forms of the extern “C” declaration: extern “C” as used above, and extern “C” { â€¦ } with the declarations between the braces. The first (inline) form is a declaration with extern linkage and with C language linkage; the second only affects language linkage. The following two declarations are thus equivalent:

extern “C” int foo;

extern “C” void bar();

and

extern “C” {

```
 extern int foo;

 extern void bar();

```

}

As there is no difference between an extern and a non-extern function declaration, this is no problem as long as you are not declaring any variables. If you declare variables, keep in mind that

extern “C” int foo;

and

extern “C” {

```
int foo;

```

}

are not the same thing.

For further clarifications, refer to [ISO14882], 7.5, with special attention to paragraph 7, or to [STR2000], paragraph 9.2.4.
