linking/compiling with nvcc/gcc

I was wondering if I can use functions compiled with gcc used inside a .cu file compiled with nvcc, and then link successfully with gcc; I know I can do the other way around as long as I declare extern "C the functions inside .cu file compiled with nvcc. so for example if I have some timer_start function inside utils.c compiled with gcc and want to use timer_start inside the .cu file, and then link the utils.c main.c and .cu file with gcc ?

Yes, I regularly do this, although I just compile all the files in one command line:

nvcc -o myprog kernel.cu main.c

I would not want to link with nvcc; or compile a normal .c file with nvcc as well if I have a huge code base, I would want to compile everything with gcc, only the cu file with nvcc and again switch back to gcc for linking. Its no big deal, there are mostly util functions that I can declare in the cu file again with a slight name change, I was just curious if someone else had the same kind of problem and had a workaround. Thanks.

Sure you can. It’s totally legit to have a cuda-specific library that you link with a C/C++ /python/other application.

e.g.:

$ nvcc -o minimal.o -Xcompiler -fPIC -shared minimal.cu

$ ld -o libminimal.so minimal.o

I’ve been trying the same thing too, ie. let non-cuda functions in NVCC-compiled .cu files use utility functions from GCC compiled .c files.

I have to do the following in the .cu files to avoid undefined reference errors from the linker:

/* 

 * Tell the C++ compiler that our util functions are C 

 * see http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html#faq-32.3

 */

extern "C" {

#include "header_to_c_code.h"

}

I’m pretty sure it’s because I use the CUDA runtime API and/or the SDK supplied cutil.h, both of which are written in C++.