包含标头不起作用

Including header doesn't work

本文关键字:不起作用 包含标      更新时间:2023-10-16

我正在尝试一个在time.hsys/time.h标头中使用多个声明的库。无论我做什么,我都无法使用任何 cpp 文件中的标头。定义似乎根本没有定义。

这就是 time.h 的样子——

#ifndef IW_STD_TIME_H
#define IW_STD_TIME_H
#include <sys/types.h>
#ifndef _TIME_T_DEFINED
#define _TIME_T_DEFINED
typedef long time_t;
#endif
#define CLOCKS_PER_SEC 1000
#ifndef _CLOCK_T_DEFINED
typedef long clock_t;
#define _CLOCK_T_DEFINED
#endif
#ifndef _TM_DEFINED
struct tm
{
    int tm_sec;     /* seconds after the minute - [0,59] */
    int tm_min;     /* minutes after the hour - [0,59] */
    int tm_hour;    /* hours since midnight - [0,23] */
    int tm_mday;    /* day of the month - [1,31] */
    int tm_mon;     /* months since January - [0,11] */
    int tm_year;    /* years since 1900 */
    int tm_wday;    /* days since Sunday - [0,6] */
    int tm_yday;    /* days since January 1 - [0,365] */
    int tm_isdst;   /* daylight savings time flag */
};
#define _TM_DEFINED
#endif
struct timespec
{
    time_t tv_sec;          /* Seconds.  */
    long int tv_nsec;       /* Nanoseconds.  */
};
S3E_BEGIN_C_DECL
#ifdef __ARMCC_VERSION
#define localtime localtime_rvct
#define localtime_r localtime_rvct_r
#endif
time_t time(time_t *t);
void tzset(void);
char *asctime(const struct tm *tm);
char *asctime_r(const struct tm *tm, char *buf);
char *ctime(const time_t *timep);
char *ctime_r(const time_t *timep, char *buf);
struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result);
struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);
time_t mktime(struct tm *tm);
size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);
clock_t clock(void);
double difftime(time_t time1, time_t time0);
int nanosleep(const struct timespec *req, struct timespec *rem);

#if defined __GNUC__ && !defined __APPLE__ && !defined I3D_ARCH_MIPS
    extern char *_tzname[2];
    extern int _daylight;
    extern long int _timezone;
    #define tzname _tzname
    #define daylight _daylight
    #define timezone _timezone
#else
    extern char *tzname[2];
    extern int daylight;
    extern long int timezone;
#endif
#define CLOCK_REALTIME 1
#define CLOCK_MONOTONIC 2
typedef int clockid_t;
int clock_getres(clockid_t clk_id, struct timespec *res);
int clock_gettime(clockid_t clk_id, struct timespec *tp);
int clock_settime(clockid_t clk_id, const struct timespec *tp);
S3E_END_C_DECL
#include <sys/time.h>
#endif /* !IW_STD_TIME_H */

这就是 sys/time.h 的定义 -

#ifndef IW_STD_SYS_TIME_H
#define IW_STD_SYS_TIME_H
#include "s3eTypes.h"
#include <time.h>
#ifndef _WINSOCK2API_
S3E_BEGIN_C_DECL
#ifndef _TIME_T_DEFINED
#define _TIME_T_DEFINED
typedef long time_t;
#endif
typedef long suseconds_t;
struct timeval
{
    time_t      tv_sec;     /* seconds */
    suseconds_t tv_usec;    /* microseconds */
};
struct timezone
{
    int tz_minuteswest;     /* minutes west of Greenwich */
    int tz_dsttime;         /* type of DST correction */
};
int gettimeofday(struct timeval *tv, struct timezone *tz);
int settimeofday(const struct timeval *tv , const struct timezone *tz);
int utimes(const char *filename, const struct timeval times[2]);
S3E_END_C_DECL
#endif  // XBOX
#endif // !IW_STD_SYS_TIME_H

我将它们作为#include<time.h>包含在 cpp 文件中,甚至typedef long time_t;都不起作用。但是,当我将声明放在 cpp 文件本身中时,它可以工作。我确定我在这里错过了一些愚蠢的东西。知道吗?

PS:文件中提到的宏是这样定义的-

#define S3E_BEGIN_C_DECL extern "C" {
#define S3E_END_C_DECL }

更新:

错误是这样的——

错误

3 错误 C2079:"tv"使用未定义的结构"timeval"错误 4 错误 C3861:"获取一天的时间":找不到标识符

似乎根本没有定义

标头中的所有定义。这些标头到处都在发生这种情况。

哦,

这是因为我的项目中已经有一个time.h,并且使用#include_next指令替换默认标头。我之前注释掉了该指令,所以我想这就是为什么 IDE 显示正确的文件,但编译器会考虑替换的文件。我用#include更改了#include_next,放置了默认文件的更具体的路径,错误解决了。