undefined reference error for linking CUDA static or shared library with gcc
Asked Answered
E

2

9

gcc and CUDA question

Hi,

I have compiled a CUDA shared library but can't link it with the main program that uses it. I am compiling the main program with gcc.

The code:

simplemain.c

    #include <stdio.h>
    #include <stdlib.h>



    void fcudadriver();

    int main()
    {
      printf("Main \n");
      fcudadriver();
      return 0;
    }

test.cu

    __global__ void fcuda()
    {
    }

    void fcudadriver()
    {
      fcuda<<<1,1>>>();
    }

I compile test.cu as --> It works

    nvcc --compiler-options '-fPIC' -o libtest.so --shared test.cu

I compile simplemain.c as ---> It gives error :(

    gcc simplemain.c -L. -ltest
    /tmp/ccHnB4Vh.o:simplemain.c:function main: error: undefined reference to 'fcudadriver'
    collect2: ld returned 1 exit status
Euhemerism answered 20/1, 2013 at 5:16 Comment(2)
+1 for including a complete, compilable, simple exampleSubstrate
Bibrak: Please mark @RobertCrovella's answer as accepted if it answers your question.Oops
S
3

try using g++ instead of gcc. nvcc uses c++ style linking conventions. (You don't need to rename any files.)

alternatively, if you must use gcc, preface your void fcudadriver() function definition like this:

extern "C" void fcudadriver()
Substrate answered 20/1, 2013 at 5:51 Comment(0)
A
3

C and C++ name the functions in different way.

Since nvcc treat the CPU code in .cu file as C++, you could rename your simplemain.c to simplemain.cpp, and compile it with g++

Another solution could be adding extern "C" before the function definition in the .cu file.

Ambassador answered 20/1, 2013 at 5:45 Comment(0)
S
3

try using g++ instead of gcc. nvcc uses c++ style linking conventions. (You don't need to rename any files.)

alternatively, if you must use gcc, preface your void fcudadriver() function definition like this:

extern "C" void fcudadriver()
Substrate answered 20/1, 2013 at 5:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.