linux redefinition of 'struct timspec'解决

问题出处

要做一个linux kernel 应用层去测试rtc 和系统时间的功能和精度,写的简单应用遇到编译错误。

问题现象

编译linux 内核应用rtc 测试:

#include <stdio.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <time.h>
#include <sys/time.h>
#include <linux/rtc.h>
#include <linux/time.h>
int  main(int arg, char argv[])
{
	int fid, status, count;
	int data = 0x1234;
	int lut[12] = { 0 };
	int temp_0, temp_1;
	int fd = -1;
	int retval = 0;
	int irqcount = 0;
	struct rtc_time rtc_tm;
	int alarm_time = 10;
	struct timeval tv;

	fid = open("/dev/fh_rtc_misc", O_RDWR);
	if (fid < 0) {
		printf("open fh rtc misc device failed\n");
		return 0;
	}
	for (count = 0; count < 12; count++) {

		lut[count] = 0x81000000; //负向最大的offset =0x81
	}

	status = ioctl(fid, RTC_SET_LUT, &lut[0]);
	if (status < 0) {
		printf("ioctl error\n");
	}
	lut[0] = 1;
	lut[1] = 0xd; //设置index=3
	status = ioctl(fid, SET_REG_VALUE, &lut[0]);
	if (status < 0) {
		printf("ioctl error\n");
	}
	sleep(1);

	gettimeofday(&tv,NULL);
	printf("rtc time is  %x ,sys time is %x\n",lut[0],tv.tv_sec);

编译报错
报错信息

问题排查

提示是timespec 结构体重复定义,最有可能就是include的几个文件中真的重复定义了:

#include <time.h>
#include <sys/time.h>
#include <linux/rtc.h>
#include <linux/time.h>

<time.h> 中没有直接定义timespec,在他引用的 <bits/types/struct_timespec.h>
struct timespec
{
_time_t tv_sec;/*Seconds */
_syscall_slong_t tv_nsec;/*Nanoseconds. */
}
两个成员:tv_sec 记录系统元年距今的秒数,tv_nsec 纳秒
#include <sys/time.h>
中没有定义
#include <linux/rtc.h>
也没有定义
#include <linux/time.h> 也就最有可能了
果然其中就直接定义了timespec 结构体

struct timespec
{
_kernel_time_t tv_sec;/*seconds */

long tv_nsec;/*nanoseconds. */

}

注释内容一致,大小写有点区别而已。应用的适用性考虑肯定选择去掉#include <linux/time.h>
再次编译
重新编译结果

问题总结

这时一个系统中存在重复定义,应用中没有明确应用范围导致的错。这种bug 估计也要一直存在下去了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值