gpt4 book ai didi

c - 在 header 中使用 ifdef linux 等是否有意义?

转载 作者:太空宇宙 更新时间:2023-11-04 03:11:45 25 4
gpt4 key购买 nike

我读了一些关于使用 #ifdef#elif 等宏来为各种平台提供实现的文章,几乎每个人都说应该尽可能避免,因为它很难维护。但是我目前正在考虑它在头文件中的用法如下:

#ifdef __linux__
typedef struct my_linux_struct my_struct
#elif __APPLE__
typedef struct my_macos_struct my_struct
#else
#error Platform not supported
#endif

尽管目的相同,struct my_linux_structstruct my_macos_struct 的实现是完全不同的。

所以我为每个平台都有一个专用的 c 文件,其中定义了平台的结构(struct my_linux_structstruct my_macos_struct)。

我对结构进行不同命名的好处是避免了实现中的名称冲突。

这样写条件宏是一种常见的做法吗?或者我们应该尽可能避免写这样的东西。

最佳答案

Is it a common practice to write conditional macro like that? Or we should avoid writing such things whenever possible.

介于两者之间。

最好将差异放在 .c 文件中,但不要“尽可能”避免。

将特定于平台的代码保留在 .h 文件之外更好,但这不是法律

关于c - 在 header 中使用 ifdef linux 等是否有意义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55665861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com