Search code examples
linkercudanvcc

NVCC refuses to link my object files


I am trying to compile a project by compiling object files and then linking them together, nothing fancy:

hello.o : hello.h hello.cu
    nvcc hello.cu -c -o hello.o
#...
main.o : $(objs)
    nvcc *.o -o exec

When I get to the link phase, just about every method is shown to be missing and undeclared, despite the fact that nm shows that each is in fact sitting within my object files, and their names are unmangled. What is going on here?


Solution

  • Ok, so my question's formulation was incorrect. In fact, the problem was that I was compiling C code like so:

    hello.o : hello.h hello.cu
        nvcc hello.c -c -o hello.o
    #...
    main.o : $(objs)
        nvcc *.o -o exec
    

    which caused nvcc to pass the .c files to gcc. When I grepped 'nm *.o' for the name of my method, I found that the object files emitted by gcc had unmangled names, while the .cu files, which were compiled by g++, expected mangled names.

    My solution was to rename all the .c files in my project to .cu, although I think (but haven't tested) that leaving them as .c and calling g++ on them explicitly in the makefile should be enough.