1
votes

I am working through a sample program that uses both C++ source code as well as CUDA. This is the essential content from my four source files.

matrixmul.cu (main CUDA source code):

#include <stdlib.h>
#include <cutil.h>
#include "assist.h"
#include "matrixmul.h"

int main (int argc, char ** argv)
{
    ...
    computeGold(reference, hostM, hostN, Mh, Mw, Nw);  //reference to .cpp file
    ...
}

matrixmul_gold.cpp (C++ source code, single function, no main method):

void computeGold(float * P, const float * M, const float * N, int Mh, int Mw, int Nw)
{

    ...
}

matrixmul.h (header for matrixmul_gold.cpp file)

#ifndef matrixmul_h
#define matrixmul_h

extern "C"
void computeGold(float * P, const float * M, const float * N, int Mh, int Mw, int Nw);
#endif

assist.h (helper functions)

I am trying to compile and link these files so that they, well, work. So far I can get matrixmul_gold.cpp compiled using:

g++ -c matrixmul_gold.cpp

And I can compile the CUDA source code with out errors using:

nvcc -I/home/sbu/NVIDIA_GPU_Computing_SDK/C/common/inc -L/home/sbu/NVIDIA_GPU_Computing_SDK/C/lib matrixmul.cu -c -lcutil_x86_64

But I just end up with two .O files. I've tried a lot of different ways to link the two .O files but so far it's a no-go. What's the proper approach?

UPDATE: As requested, here is the output of:

nm matrixmul_gold.o matrixmul.o | grep computeGold

nm: 'matrixmul.o': No such file
0000000000000000 T _Z11computeGoldPfPKfS1_iii

I think the 'matrixmul.o' missing error is because I am not actually getting a successful compile when running the suggested compile command:

nvcc -I/home/sbu/NVIDIA_GPU_Computing_SDK/C/common/inc -L/home/sbu/NVIDIA_GPU_Computing_SDK/C/lib -o matrixmul matrixmul.cu matrixmul_gold.o -lcutil_x86_64

UPDATE 2: I was missing an extern "C" from the beginning of matrixmul_gold.cpp. I added that and the suggested compilation command works great. Thank you!

1
There will never be a matrixmul object emitted, because the nvcc invocation doesn't make one, it just compiles and links to the executable directly from a temporary object. The problem is name mangling. Your compute_gold definition lacks an extern "C", so you are getting C++ linkage in the object file, but later telling the compiler to expect C linkage. Change one or the other...talonmies

1 Answers

4
votes

Conventionally you would use whichever compiler you are using to compile the code containing the main subroutine to link the application. In this case you have the main in the .cu, so use nvcc to do the linking. Something like this:

$ g++ -c matrixmul_gold.cpp
$ nvcc -I/home/sbu/NVIDIA_GPU_Computing_SDK/C/common/inc \
-L/home/sbu/NVIDIA_GPU_Computing_SDK/C/lib \
-o matrixmul matrixmul.cu matrixmul_gold.o -lcutil_x86_64

This will link an executable binary called matrimul from matrixmul.cu, matrixmul_gold.o and the cutil library (implicitly nvcc will link the CUDA runtime library and CUDA driver library as well).