【问题标题】:Struct included in two header files, neither of which I own结构包含在两个头文件中,我都不拥有
【发布时间】:2020-07-01 12:58:09
【问题描述】:

我正在尝试在 C++ 中使用 SCHED_DEADLINE,我需要将 struct sched_attr 发送到 ioctl,所以我包含了 <linux/sched/types.h>,该标题也恰好包含 strut sched_param。我还包括<string>,其中包括c++ 的标准头文件,最终包括include\bits\sched.h,它还定义了struct sched_param。编译器(显然)并不特别喜欢这个。我在代码中使用了标头保护 (_BITS_TYPES_STRUCT_SCHED_PARAM) 让编译器不包含 include\bits\sched.h,但感觉应该有一个“更好”的方法。

> Executing task in folder sched_deadline_testing: arm-linux-gnueabihf-g++ -o LSM9DS0 src/LSM9DS0.cpp -Wall --pedantic <

In file included from src/LSM9DS0.cpp:36:
/usr/arm-linux-gnueabihf/include/linux/sched/types.h:7:8: error: redefinition of ‘struct sched_param’
 struct sched_param {
        ^~~~~~~~~~~
In file included from /usr/arm-linux-gnueabihf/include/bits/sched.h:74,
                 from /usr/arm-linux-gnueabihf/include/sched.h:43,
                 from /usr/arm-linux-gnueabihf/include/pthread.h:23,
                 from /usr/arm-linux-gnueabihf/include/c++/8/arm-linux-gnueabihf/bits/gthr-default.h:35,
                 from /usr/arm-linux-gnueabihf/include/c++/8/arm-linux-gnueabihf/bits/gthr.h:148,
                 from /usr/arm-linux-gnueabihf/include/c++/8/ext/atomicity.h:35,
                 from /usr/arm-linux-gnueabihf/include/c++/8/bits/basic_string.h:39,
                 from /usr/arm-linux-gnueabihf/include/c++/8/string:52,
                 from src/LSM9DS0.cpp:20:
/usr/arm-linux-gnueabihf/include/bits/types/struct_sched_param.h:23:8: note: previous definition of ‘struct sched_param’
 struct sched_param

代码是here,如果这很重要的话。我也尝试过这种交叉编译和本地编译,两者都会导致问题。

【问题讨论】:

    标签: c++ include


    【解决方案1】:

    你运气不好。这两个标头不兼容。您只需避免将&lt;linux/sched/types.h&gt;&lt;string&gt; 包含在同一个翻译单元中。可能值得注意的是,作为 Linux 内核头文件,前者可能不打算在 C++ 中使用,也没有尝试使其兼容。

    【讨论】:

    • 实际上使 Linux 头文件与 C++ 兼容在过去受到当权者的积极反对。 LKML FAQ 15.3。正在提出的论点很不正确,但祝他们改变主意......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-10
    相关资源
    最近更新 更多