compiling cuda with gcc results in: error: ‘threadIdx’ undeclared

How can I force GCC to compile with threadIdx lines in my code?

I’m attempting to compile my cuda application with a c wrapper.

To generate the .so file I run:
nvcc -arch=sm_11 -o libtest.so --shared -Xcompiler -fPIC main.cu

Then I attempt to compile the c wrapper with:
gcc -std=c99 -I/usr/local/cuda/include -o main -L. -ltest main.c

This results in the following error a few layers down in my code(an include to an included file): error: ‘threadIdx’ undeclared

Should I be using different flags when creating the .so file?

Note: Everything does compile and run just fine as a GPU application without any C wrapping.

nvcc uses g++ for the host code (and some other compiler for the kernels). To link the functions in your library with your c-file (compiled with gcc), you need to annotate your functions with extern “C”. See also http://en.wikipedia.org/wiki/Name_mangling#Handling_of_C_symbols_when_linking_from_C.2B.2B.

Possibly the simplest solution is to compile your main.c file with g++ (maybe rename to main.cc first), and see if this solves your problem. Also make sure that your kernel is not compiled with gcc or g++, only with nvcc, since this can cause the “error: ‘threadIdx’ undeclared” problem as well.

Just to confirm I should only add extern “C” to the methods called from my .c(or .cc) file? Or to all methods in my .cu files?

I re-factored with the extern “C”, renamed to main.cc and attempted to compile with g++. And it looks like the error changed to be:
error: ‘threadIdx’ was not declared in this scope

I also added the extern keyword to my function declarations in the .c file which allowed me to compile. Thanks!