如何为cpp和h文件创建别名(并忽略编译器__attributes__) [英] How to create alias for cpp and h files (and ignoring compiler __attributes__)

查看:47
本文介绍了如何为cpp和h文件创建别名(并忽略编译器__attributes__)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为cpp和h文件创建别名,以便GCC可以编译具有不同命名后缀的文件?

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

如何使GCC忽略某些编译器属性,而不必将其包装在 #ifdef #endif 语句中?

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

我正在用CUDA编写一个库,希望在其中可以在CPU和GPU上创建相同的对象.

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.

如果用户没有NVCC(这是仅标头的库),我希望用户能够使用GCC进行编译.

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.

无论如何,我可以让GCC编译 .cu .cuh 文件,而忽略 __ host __ __ device __ __ global __ 编译器属性,而不必简​​单地包装所有

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

个: #ifdef #endif 语句中的 __ host __ __ device __ __ global __ ?

IE:#ifdef NVCC//具有nvcc这样的解释 .cu 文件

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

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

ELSE://具有GCC/Clang/etc这样的解释 .cu 文件的文件(以及 .h 文件的文件):

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

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

推荐答案

您可以在随处可见的头文件中执行以下操作:

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__

您刚刚使用

CUDAHOSTDEV

我不知道这是否更容易,但是如果您是从头开始编写内容的话,键入的内容可能会更少.

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

您可以看到一些特定于CUDA的 nvcc 编译器定义了

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

此外,我非常确定您可以将 -x 开关传递给gcc/g ++,以告诉它可以识别/使用哪种语言,而与文件后缀无关.

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.

您可能还想查看一个现成的高质量" CUDA模板库,例如 thrust 看看他们做什么.

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

这篇关于如何为cpp和h文件创建别名(并忽略编译器__attributes__)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆