I am converting a numerical model written in c to support cuda. There are c some features that do not link with c++ linker. For example, by just changing the .c to .cpp postfix for all c files (not any cuda functions yet), the compilation went through but link failed due to some unresolved symbols.
So I am wondering whether there is a specific file postfix other than .cu that can instruct cuda to use c compiler/linker explicitly. Thanks
Hailiang
not a postfix, but individual function declarations and definitions can be tagged as extern “C” to resolve your linking issues. It disables the C++ specific name mangling of functions, so that other C code can link to it.
alternatively extern “C” can also apply to an entire block of code using curly brackets
extern “C”
{
// some function definitions/declarations here
}
Thanks! Could you take a look at my files and see why they do not even compile? This example is a very simplified version that can duplicate my problem. It is not allowed to add attachment so I put my example in code blocks blow.
main.cu
#include "func.h"
#include <stdio.h>
int main()
{
double v = 1.0;
printf("%f converted value is %f", v, convert(v, 2));
return 0;
}
file1.cu
const double Var[6] = { 1.0, 448.831, 0.64632,
0.02832, 28.317, 2.4466 };
file2.cu
#include "func.h"
extern double Var[]; // var defined in file1.cpp
double convert(double v, int i)
{
return Var[i] * v;
}
func.h
#pragma once
double convert(double, int);