0

How can I create aliases for cpp and h files so that GCC can compile files of different naming suffixes?

How can I make the GCC ignore certain compiler attributes without having to wrap them in #ifdef and #endif statements?


Elaboration:

I am writing a library with CUDA in which I want to be able to create the same objects both on the CPU and GPU.

If the user does not have NVCC (this is a header only library) I want the user to be able to compile with the GCC.

Is there anyway that I can make the GCC compile .cu and .cuh files while ignoring the __host__ __device__and __global__ compiler attributes without having to simply wrap all

of: __host__ __device__and __global__ in #ifdef #endif statements?


IE: #ifdef NVCC //have nvcc interpret .cu file as such

__host__ __device__ 
struct foo {
    foo() {/*impl*/ }
};

ELSE: //have GCC/Clang/etc interpret .cu file as such (and as an .h file):

struct foo {
    foo() {/*impl*/ }
};

2 Answers2

2

You can do something like this in a header file that you include everywhere:

#ifdef __CUDACC__ 
#define CUDAHOSTDEV __host__ __device__
#else
#define CUDAHOSTDEV
#endif

Then, wherever you would use

__host__ __device__

you just use

CUDAHOSTDEV

I don't know if that is any easier, but it probably is less typing if you're writing things from scratch.

You can see some CUDA-specific nvcc compiler defines here.

Also I'm pretty sure you can pass the -x switch to gcc/g++ to tell it what kind of language to recognize/use independent of the file suffix.

You might also want to look at an exising "high quality" CUDA template library like thrust to see what they do.

Robert Crovella
  • 143,785
  • 11
  • 213
  • 257
  • `-x language Specify explicitly the language for the following input files (rather than letting the compiler choose a default based on the file name suffix). This option applies to all following input files until the next '-x' option. Possible values for language are: c c-header cpp-output c++ c++-cpp-output objective-c objc-cpp-output assembler assembler-with-cpp ada f77 f77-cpp-input ratfor java treelang` Found the documentation for `-x` here: http://tigcc.ticalc.org/doc/comopts.html I'm gonna leave this question open a bit longer but I most likely give you best answer. – Joseph Franciscus Jan 11 '18 at 21:58
  • PS Thanks a ton this was a perfect solution. (I know you shouldn't say "thanks" on SO but I'm a rebel) – Joseph Franciscus Jan 11 '18 at 22:10
-1

You will need #ifdef / /#endif somewhere but you can often make these sorts of things look better by #defining symbols that expand to an empty value if the flag is not set:

#if defined(CCOMPILE_WITH_GPU)
#define MY_ATTRIBUTE xxx
#endif

MY_ATTRIBUTE void func();

rather than putting the #ifdef around every function declaration.

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23