C 中的问题,包括 time.h



我正在做的这个大型项目中有一个文件,叫做timeKeeper.h(和.c(

--------------编辑---------------修复了布尔的东西仍然坏了

--------------
#ifndef TIMEKEEPER_H_
#define TIMEKEEPER_H_
#include <time.h>
#include <stdbool.h>
bool isAfter(time_t time);
void setTime(char seconds, char minutes, char hours, char day,
        char month, char year);
    void tickSeconds(void);
time_t getCurrentTime(void);
time_t createTime(char seconds, char minutes, char hours, char day,
        char month, char year);
void startTime(void);
time_t addSeconds(int seconds, time_t time);
long timeRemaining(time_t time);
void rtc_set(char seconds, char minutes, char hours, char days, char months,
    char year);
 #endif

当我尝试构建我的项目时,这个文件(以及任何使用 time.h 的文件(中存在一堆错误。以下是timeKeeper.h中的一些错误:

expected ')' before 'time'        Line 6
expected '"', ',', ';','asm', or '__attribute__' before 'getCurrentTime'    Line 10

我怀疑timeKeeper不知道time_t是什么,即使它有

#include <time.h>

我也收到这样的错误

implicit declaration of function 'localtime'

在我的 timeKeeper.c 文件中。 是的,timeKeeper.c #includes timeKeeper.h

任何帮助将不胜感激。

----附加信息-----我正在使用 Atmel Studio 6.0这里是时间守护者

#include "FreeRTOS.h"
#include "task.h"
#include "print_funcs.h"
#include "timeKeeper.h"
#include "telemetryLookup.h"
void timeTask(void* pvParameters);
time_t TIME;
blah blah blah......

----编辑2------

我添加了#include <stdbool.h>并将第 6 行的Bool更改为 bool,但错误仍然存在。

C 中没有类型 Bool。有一个类型 bool ,但只有当您有 #include <stdbool.h> 时,它才可见。

当我添加#include <stdbool.h>并将Bool更改为bool时,您的代码编译没有错误。(我也不得不删除行号。

我还建议为参数选择一个除time以外的名称,以isAfter和其他函数。 time 也是 <time.h> 中声明的函数的名称。我认为在这种情况下它实际上不会引起冲突,但唯一名称可以避免混淆。

由于Bool是无法识别的类型名称,因此编译器在

Bool isAfter(time_t time);

之后您看到的任何错误都可能是虚假的。如果遇到语法错误,请首先修复最早报告行上的错误,然后重新编译。

编辑:

根据进一步的评论,添加#include <stdbool.h>并将Bool更改为bool似乎没有帮助。你得到的特定信息似乎暗示time_t不被认可 - 这当然不应该发生。

尝试一个简单的 2 行源文件(并确保文件名具有.c后缀(:

#include <time.h>
time_t foo;

如果失败了,我能想到的唯一解释是您的编译器和/或运行时库安装以某种方式损坏了。

您是否可以选择查看预处理的源代码?如果是这样,将其应用于上述 2 行源应该会显示 <time.h> 的预处理内容,其中应包括 time_t 的定义。

(假设您使用符合标准的 C 编译器,并且我们正在处理微控制器,即独立环境。

让我引用C标准:

一个符合要求的独立实现应接受任何严格符合的程序,该程序不使用复杂类型,并且库子句(条款 7(中指定的功能的使用仅限于标准标头的内容 <float.h> <iso646.h> <limits.h> <stdarg.h> <stdbool.h> <stddef.h> <stdint.h>

<time.h>的功能通常由操作系统提供,但微控制器不一定运行操作系统。因此,您可能需要自己提供此功能。


引用这个线程。

挑战:

damien_d - 2009年2月27日 - 上午01:03

发布主题:时间.h 和/或 C 的计时库

我正在使用 winavr 20081205。

似乎没有实现 time.h 头文件,无论是通过查看源代码,还是在主页上 file:///C:/WinAVR-20081205/doc/avr-libc...dules.html ,或者当我尝试将头文件包含在项目中时。例如:

法典:

#include "time.h"

time_t testTime;

生成以下编译器错误:

法典:

../Clock/clock.c:4:18: error: time.h: No such file or directory ../Clock/clock.c:6: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'testTime'

make: *** [clock.o] Error 1

响应:

科什奇 - 2009年2月27日 - 上午02:23

帖子主题:RE:时间.h 和/或 C 的计时库

报价: 似乎没有实现 time.h 头文件

当然没有。time.h 中的功能处理运行代码的操作系统的功能。AVR 没有操作系统。

报价: 或任何time_t将在 AVR-GCC 上定义的任何内容。

这是问题的核心。系统上time_t的完整定义取决于操作系统。

确认:

damien_d - 2009 年 2 月 28 日 - 上午 06:03

帖子主题:RE:时间.h 和/或 C 的计时库

我今天学到了一些新东西 - 非托管 C 环境只需要标准标头的子集。

猜是时候找到 time.h 的 BSD 许可版本(和实现(并尽情破解它了。

在第 6 行有Bool的定义。也许你不包括定义/Typedef?

time()time.h中定义的函数,但您也尝试将其用作多个原型中的参数名称。

最新更新