c++ - C/C++ C4047 的间接级别与 'int' 不同?

标签 c++ c visual-studio windows-ce

所以我尝试编译 SQLite(使用通常使用的不同目标设备),我收到一个奇怪的警告和错误,我不确定如何修复它意味着什么。这是我尝试从 Visual Studio 中编译后收到的消息:

4>Compiling...
4>interop.c
4> sqlite3.c(11857) : error C2220: warning treated as error - no 'object' file generated
4> sqlite3.c(11857) : warning C4013: 'localtime' undefined; assuming extern returning int
4> sqlite3.c(11857) : warning C4047: '=' : 'tm *' differs in levels of indirection from 'int'
4> sqlite3.c(28379) : error C2040: 'localtime' : 'tm *(const time_t *)' differs in levels of indirection from 'int ()'

下面是这些行的代码:

#else
{
    struct tm *pTm;
    sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER))
    pTm = localtime(&t);                                //Line 11857
    y.Y = pTm->tm_year + 1900;                          
    y.M = pTm->tm_mon + 1;                              
    y.D = pTm->tm_mday;                                 
    y.h = pTm->tm_hour;                                 
    y.m = pTm->tm_min;                                  
    y.s = pTm->tm_sec;                                  
    sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER));
}
#endif

以及 localtime() 的方法定义,它提示第 28379 行和间接访问:

/*************************************************************************
** This section contains code for WinCE only.
*/
/*
** WindowsCE does not have a localtime() function.  So create a
** substitute.
*/
struct tm *__cdecl localtime(const time_t *t)                
{                                                               // Line 28379
  static struct tm y;
  FILETIME uTm, lTm;
  SYSTEMTIME pTm;
  sqlite3_int64 t64;
  t64 = *t;
  t64 = (t64 + 11644473600)*10000000;
  uTm.dwLowDateTime = (DWORD)(t64 & 0xFFFFFFFF);
  uTm.dwHighDateTime= (DWORD)(t64 >> 32);
  FileTimeToLocalFileTime(&uTm,&lTm);
  FileTimeToSystemTime(&lTm,&pTm);
  y.tm_year = pTm.wYear - 1900;
  y.tm_mon = pTm.wMonth - 1;
  y.tm_wday = pTm.wDayOfWeek;
  y.tm_mday = pTm.wDay;
  y.tm_hour = pTm.wHour;
  y.tm_min = pTm.wMinute;
  y.tm_sec = pTm.wSecond;
  return &y;
}

编辑 这也是结构定义:

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 */
};

我真的无法弄清楚“‘tm*’在间接级别上与‘int’的不同”是什么意思或如何修复它,我什至不确定问题是什么,因为这个编译适用于 Win32 但不适用于 WinCE。

有没有人了解警告\错误或如何修复它?非常感谢任何帮助!

谢谢!

最佳答案

localtime 函数的声明在使用时不可用。根据 C 规则,编译器因此假定它是一个返回 int 的未知函数。然后它会警告将该 int 转换为 tm*(这就是“间接级别”的含义)。

解决方案是在某处声明 localtime 以便编译器在编译 sqlite3.c 时可以看到它,就像在该文件包含的头文件中一样。

第二个“间接级别”消息(错误)似乎是因为您在使用 localtime 之后定义它,所以编译器已经猜到签名具有 int 返回类型,而您的实际签名与此不同。

确保 localtime 的正确声明在使用它的地方之前可用。简单地说,你需要放置线

struct tm *__cdecl localtime(const time_t *t);

第 11857 行之前的某处。您可以将定义(函数体)放在任何您想要的地方,只需确保在任何地方使用函数之前已知声明(函数签名)。

关于c++ - C/C++ C4047 的间接级别与 'int' 不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6380981/

相关文章:

visual-studio - nuget dll 被另一个进程使用

visual-studio - WCF 64 位不起作用

c++ - 编译需要#include "stdafx.h"

c++ - 类声明中固定大小的 C 样式数组

c# - "add as link"选项发生了什么?

c - X11 X图像处理

c - 在 C 中模拟 `alloca()`

C++按键: getch, cin.get?

c++ - 使用 CMake C++17 项目设置 Travis CI

c - 如何通过比较 C 中的字符串来检查输入是否有效