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).
Example:
app.c
#include "demo.h"
int main(void) {
demofunc();
incfunc();
}
Cuda library
demo.cu
#include <stdio.h>
#include "demo.h"
#include "inclib.h"
void demofunc() {
printf("funtion1");
}
Cuda header
demo.h
#include "inclib.h"
void demofunc();
C library
inclib.cu
#include <stdio.h>
//#include "inclib.h"
void incfunc() {
printf("function2");
}
C header
inclib.h
void incfunc();
Compiled and linked
gcc -o inclib.o -c inclib.c
gcc -o app.o -c app.c
nvcc -o demo.o -c demo.cu
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,
twentyone
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” {
endif
void demofunc();
ifdef __cplusplus
} /* closing brace for extern “C” */
endif
[/codebox]
Done.
But I want to give a comment on your code:
why include inclib.h in demo.h and demo.cu? 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, demo.cu… :-)
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).
Example:
app.c
#include "demo.h"
int main(void) {
demofunc();
incfunc();
}
Cuda library
demo.cu
#include <stdio.h>
#include "demo.h"
#include "inclib.h"
void demofunc() {
printf("funtion1");
}
Cuda header
demo.h
#include "inclib.h"
void demofunc();
C library
inclib.cu
#include <stdio.h>
//#include "inclib.h"
void incfunc() {
printf("function2");
}
C header
inclib.h
void incfunc();
Compiled and linked
gcc -o inclib.o -c inclib.c
gcc -o app.o -c app.c
nvcc -o demo.o -c demo.cu
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,
twentyone