Linking issues with included libraries

Hi, I’m still learning C and Cuda, so this might be a basic c problem!

I created a cuda module and a usual c module. The c module is included by the cuda header file.

The compliation process of all files is successful. However, when trying to link the object files,

I’m notified about some undefined references. Those references a connected to the non-Cuda

C library, and not the cuda library itself (cudart or else).



#include "demo.h"

int main(void) {




Cuda library

#include <stdio.h>

#include "demo.h"

#include "inclib.h"

void demofunc() {



Cuda header


#include "inclib.h"

void demofunc();

C library

#include <stdio.h>

//#include "inclib.h"

void incfunc() {



C header


void incfunc();

Compiled and linked

gcc -o inclib.o -c inclib.c

gcc -o app.o -c app.c

nvcc -o demo.o -c

g++ -o App demo.o app.o inclib.o -L/usr/local/cuda/lib -lcudart

displayed problem:

app.o: In function `main':

app.c:(.text+0x12): undefined reference to `demofunc'

Really strange is that the undefined reference is connected to the function “demofunc” from

the cuda library. I would habe expected that if something is missing it would be the “incfunc” from

the c library because I didn’t include that one in “app.c”.

Greeting and thanks,


Very nice way to explore the languages. To solve your issue, obviously something wrong with the function demofunc(), but it is defined and compiled, then think of its declaration as:

[codebox]#ifdef __cplusplus

extern “C” {


void demofunc();

#ifdef __cplusplus

} /* closing brace for extern “C” */




But I want to give a comment on your code:

  • why include inclib.h in demo.h and though no harm, but not needed

I think only inlclude these prototype declaration just when you define the function prototyped or you will call them for compiling syntax check.

So recommend that you #include inclib.h

  • in inclib.c, in case that you define incfunc() differing from the prototype

  • in app.c, to help check the correct calling of incfunc()

but nothing with demo.h,… :-)