2

I've a file A.cu containing function void A(). I've another file test_A.c which calls the cuda function A() and also has function declaration as

extern void A();

Now I compile and link them as follows

nvcc -c -o A.o A.cu 
gcc -o test_A test_A.c A.o /opt/cuda-4.0/cuda/lib64/libcudart.so

and I get error like

undefined reference to `A'

What am I missing?

0

1 Answer 1

5

The problem is probably C++ name mangling. CUDA code is compiled with a C++ compiler, not a C compiler. If you dump the contents of A.o you should find a symbol called something like _Z1Av, rather than A as the C compiler is expecting.

To overcome this, you can use the following declaration inside your A.cu file:

extern "C" void A(void)
{
    .......
}

This will tell the C++ compiler to use C conventions when compiling the code, and a suitably named function will be written to the object file A.o which the C compiler and linker can understand.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.