未定义类型的无效使用和存储大小未知

2023-12-05

我正在尝试将一些函数移动到单独的文件中c项目。

I made util.h文件与

#ifndef _UTIL_H
#define _UTIL_H

#include <signal.h>
#include <termios.h>
#include <time.h>

...

extern struct timeval tv1, tv2, dtv;

void time_start();

long time_stop();

我做了util.c文件与

#include "util.h"

...
struct timeval tv1, tv2, dtv;

void time_start() { gettimeofday(&tv1, &timezone); }

long time_stop()
{
    gettimeofday(&tv2, &timezone);
    dtv.tv_sec = tv2.tv_sec - tv1.tv_sec;
...

在cmake中我有

add_executable(mpptd mpptd.c util.c)

我在编译过程中收到以下错误

[build] ../settings/daemons/util.c: In function ‘time_stop’:
[build] ../settings/daemons/util.c:14:8: error: invalid use of undefined type ‘struct timeval’
[build]      dtv.tv_sec = tv2.tv_sec - tv1.tv_sec;
[build]         ^

and

[build] ../settings/daemons/util.c: At top level:
[build] ../settings/daemons/util.c:7:16: error: storage size of ‘tv1’ isn’t known
[build]  struct timeval tv1, tv2, dtv;
[build]                 ^~~

这里可能出了什么问题?为什么“存储大小”错误晚于“未定义类型”错误?谁不早点走?


struct timeval定义在 sys/time.h 中。您需要将其包括在内。

#ifndef _UTIL_H
#define _UTIL_H

#include <signal.h>
#include <termios.h>
#include <time.h>
#include <sys/time.h>

...
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

未定义类型的无效使用和存储大小未知 的相关文章

随机推荐