3

I have a main code wich uses some libraries and I been compiling it like this:

gcc importedCFile1.c importedCFile2.c mainCode.c -O3 -lm -Wall -o maincode -lrt

Now I have added CUDA code in mainCode and changed its extension to .cu... So, how can I compile the whole thing?

I tried:

nvcc importedCFile1.c importedCFile2.c mainCode.cu -o maincode 

but I got a lot of "undefined reference" to my functions in the imported C files.

To include my C files I am using:

extern "C" {
   #include "importedCFile1.h"
   #include "importedCFile2.h"
}

And ´importedCFile1.c´ is using some functions and variables declared in ´importedCFile2.c´ and ´mainCode.cu´. Like this:

extern int **se;        // Variables from mainCode  
extern int *n;          
extern int numNodes;

extern int *getVector(int n);   // Function from mainCode
extern int iRand(int high);     // Function from importedCFile2

This functions are the cause of the undefined references. What should I do?

Also, how do I add the flags I need for the C code, such as -lrt, O3, lm and Wall??

EDIT: You can find a reduced example of the problem here:

https://github.com/mvnarvaezt/cuda/tree/master/minimalExample

If you compile the mainCode.c and importedCFile.c with gcc it works fine. If you compile mainCode.cu and importedCFile.c with nvcc you will get an undefined reference to anExample() (the function in importedCFile.c).

And you comment the header importing importedCFile.c and the call to anExampled() function it would work find.

user3117891
  • 145
  • 2
  • 12
  • Wrap the header file includes inside `extern "C" {#include ... }`. Otherwise the function names will get mangled according to C++ rules. – tera Mar 07 '17 at 19:49
  • @tera I did, but ´importedCFile1.c´ also uses some functions and variables from ´importedCFile2.c´ and ´mainCode.cu´, and I got "undefined reference" to those. I have in ´importedCFile1.c´ something like: extern int *getVector(int n); // This is a function in mainCode.cu extern int iRand(int high); // This is a function in importedCFile2.c – user3117891 Mar 07 '17 at 20:05
  • 2
    That should just work fine. Show us some code to demonstrate the problem. – tera Mar 07 '17 at 20:08
  • 1
    You can pass any option to the C compiler (like `-Wall`) by preceding it with `-Xcompiler`, and any option to the linker by preceding it with `-Xlinker`. `-l` and `-O`are also is understood by nvcc directly, where `-O3` even is the default. So your commandline should become `nvcc -lrt -lm -Xcompiler -Wall importedCFile1.c importedCFile2.c mainCode.cu -o maincode`. – tera Mar 07 '17 at 22:23
  • 3
    And your example (thanks for showing that) should still work just fine. Please post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – tera Mar 07 '17 at 22:25

1 Answers1

2

Your problem is that the C code in importedFile.c is trying to call back C++ functions in mainCode.cu.

In order to be callable from C, C++ functions must have C linkage. Declare getVector() as

extern "C" int *getVector(int n)  {

in mainCode.cu, and your example will compile fine.

tera
  • 7,080
  • 1
  • 21
  • 32