![](/img/trans.png)
[英]Struct defined in header and included in two source codes is only defined in one
[英]Struct included in two header files, neither of which I own
我正在尝试在 c++ 中使用 SCHED_DEADLINE,我需要将struct sched_attr
发送到和ioctl
,所以我包含了<linux/sched/types.h>
,即 header 也恰好包含strut sched_param
。 我还包括了<string>
,其中包括 c++ 的标准头文件,最终包括include\bits\sched.h
,它也定义了struct sched_param
。 编译器(显然)并不特别喜欢这个。 我在我的代码中使用了 header 保护( _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
如果这很重要,代码就在这里。 我也尝试过这种交叉编译和本地编译,两者都会导致问题。
你运气不好。 这两个标头不兼容。 您只需避免将<linux/sched/types.h>
和<string>
都包含在同一个翻译单元中。 It's probably worth noting that being a Linux kernel header, the former is probably not intended to be used in C++, nor has there been an attempt to make it compatible.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.