基于Linux内核标头中的功能的条件编译 [英] Conditional compilation based on functionality in Linux kernel headers

查看:119
本文介绍了基于Linux内核标头中的功能的条件编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑使用从导出到用户空间的Linux标头中的某些功能的情况,例如 perf_event_open .

Consider the case where I'm using some functionality from the Linux headers exported to user space, such as perf_event_open from <linux/perf_event.h>.

由于成员已添加到perf_event_attr,例如

The functionality offered by this API has changed over time, as members have been added to the perf_event_attr, such as perf_event_attr.cap_user_time.

如果这些新功能在本地可用,我如何编写可以编译和使用这些新功能的源代码,但是如果这些新功能不使用和不使用它们,则如何优雅地回退?

How can I write source that compiles and uses these new functionalities if they are available locally, but falls back gracefully if they aren't and doesn't use them?

尤其是如何在预处理器中检测这些东西是否可用?

In particular, how can I detect in the pre-processor whether this stuff is available?

我以这个perf_event_attr为例,但是我的问题是一个普遍的问题,因为结构成员,新结构,定义和函数一直都在添加.

I've used this perf_event_attr as an example, but my question is a general one because structure members, new structures, definitions and functions are added all the time.

请注意,这里我仅考虑在同一进程上运行该进程的情况:如果要在一个主机上编译而在另一个主机上运行,​​则需要不同的技巧.

Note that here I'm only considering the case where a process is compiled on the same system that it will run on: if you want to compile on one host and run on another you need a different set of tricks.

推荐答案

使用/usr/include/linux/version.h中的宏:

#include <linux/version.h>

int main() {
#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,16)
                                      // ^^^^^^ change for the proper version when `perf_event_attr.cap_user_time` was introduced
   // use old interface
#else
   // use new interface
   // use  perf_event_attr.cap_user_time
#endif
}

这篇关于基于Linux内核标头中的功能的条件编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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