c - `__noinline__` GLib和CUDA宏冲突

标签 c gcc cuda glib

我正在开发一个在 C 中同时使用 GLib 和 CUDA 的应用程序。似乎在为 .cu 文件导入 glib.h 和 cuda_runtime.h 时存在冲突。

7 个月前 GLib 进行了更改以避免与 pixman 的宏冲突。他们在 gmacros.h 中的标记 noinline 前后添加了 __:https://gitlab.gnome.org/GNOME/glib/-/merge_requests/2059

鉴于 gcc 声称:

You may optionally specify attribute names with __ preceding and following the name. This allows you to use them in header files without being concerned about a possible macro of the same name. For example, you may use the attribute name __noreturn__ instead of noreturn.

但是,CUDA 确实在其宏中使用了____noinline__ 就是其中之一。他们承认可能存在冲突,并添加了一些编译器检查以确保它不会在常规 c 文件中发生冲突,但似乎在 .cu 文件中它仍然适用:

#if defined(__CUDACC__) || defined(__CUDA_ARCH__) || defined(__CUDA_LIBDEVICE__)
/* gcc allows users to define attributes with underscores,
   e.g., __attribute__((__noinline__)).
   Consider a non-CUDA source file (e.g. .cpp) that has the
   above attribute specification, and includes this header file. In that case,
   defining __noinline__ as below  would cause a gcc compilation error.
   Hence, only define __noinline__ when the code is being processed
   by a  CUDA compiler component.
*/
#define __noinline__ \
        __attribute__((noinline))

我是 CUDA 开发的新手,这显然是他们和 gcc 都知道的一个可能问题,所以我只是缺少编译器标志或其他东西吗?或者这是 GLib 需要解决的真正冲突?

环境 glib 2.70.2,cuda 10.2.89,gcc 9.4.0

编辑:我提出了一个 GLib 问题 here

这可能不是 GLib 的错,但考虑到目前答案中的不同意见,我将把它留给那里的开发人员来决定是否与 NVidia 一起提出。

我现在已经使用了 nemequ 的解决方法,它编译时没有任何提示。

最佳答案

海湾合作委员会的 documentation状态:

You may optionally specify attribute names with __ preceding and following the name. This allows you to use them in header files without being concerned about a possible macro of the same name. For example, you may use the attribute name __noreturn__ instead of noreturn.

现在,这只是假设您避免使用编译器和库使用的双下划线名称;他们可能使用这样的名字。因此,如果您使用 NVCC - NVIDIA 可以声明“我们使用 noinline 但您不能使用它”。

...事实上,基本上是这样的:宏被保护如下:

#if defined(__CUDACC__) || defined(__CUDA_ARCH__) || defined(__CUDA_LIBDEVICE__)
#define __noinline__  __attribute__((noinline))
#endif /* __CUDACC__  || __CUDA_ARCH__ || __CUDA_LIBDEVICE__ */
  • __CUDA_ARCH__ - 仅为设备端代码定义,其中 NVCC 是编译器(此处忽略 clang CUDA 支持)。
  • __CUDA_LIBDEVICE__ - 不知道它在哪里使用,但你肯定没有构建它,所以你不关心它。
  • __CUDACC__在NVCC编译代码时定义。

因此在常规的主机端代码中,包含此 header 不会与 Glib 的定义冲突。

底线:NVIDIA(基本上)在这方面做了正确的事情,这不应该是一个真正的问题。

关于c - `__noinline__` GLib和CUDA宏冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70301375/

相关文章:

macos - Mac OS GCC for C 中定义了什么?

android - 如何链接在android中使用ndk-build创建的静态库

c++ - LNK2038 : mismatch detected for 'RuntimeLibrary' : value 'MT_StaticRelease' doesn't match value 'MD_DynamicRelease' in file. 对象

c - C中 "int const* "的确切含义是什么?

c - 如果我们使用 = 运算符而不是 memcpy() 分配不同的结构会怎么样

c - 如何使用格式字符串攻击覆盖变量

c - 为什么会出错?我对 strtok() 有问题;这么长时间

c - 处理结构时何时使用 "->"与 "."?

cuda - 编程cuda内核时整数的大小是多少

c++ - 计算 thrust::device_vector 上的梯度