【问题标题】:GNU GCC Codeblocks win32 function not declared未声明 GNU GCC 代码块 win32 函数
【发布时间】:2021-05-18 14:08:06
【问题描述】:

这是我项目(Implot)中库中的一段代码:

#ifdef _WIN32
    t.S = _mkgmtime(ptm);

...

#ifdef _WIN32
  if (gmtime_s(ptm, &t.S) == 0)

...

#ifdef _WIN32
  if (localtime_s(ptm, &t.S) == 0)

这个项目刚刚从 Visual Studio 2019 移植到 Codeblocks (C++17)。由于某种原因,这无法编译,当我搜索错误时,我在网上找不到任何东西。

error '_mkgmtime' was not declared in this scope
error 'gmtime_s' was not declared in this scope
error 'localtime_s' was not declared in this scope

我知道这些错误是因为缺少函数,这很可能是由于缺少包含文件,但是这些已在 Visual Studio 2019 中使用,没有对任何源代码进行任何更改,并且在那里运行良好。为什么这不起作用,我该如何解决?

【问题讨论】:

  • 首先,您是在构建 C 还是 C++ 代码? C 和 C++ 是两种不同的语言,具有不同的功能。例如,自 C11 标准起,C 就有一个 gmtime_s 函数,但 C++ 没有这样的函数。
  • C++。但如果这是真的,那为什么 Visual Studio 可以正常工作呢?另外我怎样才能让它在 GCC 上工作?
  • 虽然 C 标准有许多带有 _s 后缀的“安全”函数,但它们都是代表 Microsoft 添加的,而且 Microsoft 的实现并不总是遵循该标准。将这些函数视为 Microsoft(和 Visual Studio 编译器)特定的。
  • Visual Studio 也倾向于使用下划线前缀(如_mkgmtime)来表示非可移植和非标准函数。
  • @Someprogrammerdude 我找不到gmtime_s 的替代品。我也不确定mktime 是否是_mkgmtime 的正确替代品?

标签: c++ gcc codeblocks


【解决方案1】:

如果您阅读 the Visual Studio predefined macros documentation,您可以找到编译器特定的宏,这些宏可用于识别您的代码是否由 Visual Studio C++ 编译器构建。

例如_MSC_VER 宏,您可以检查它来代替_WIN32

如:

#ifdef _MSC_VER
auto result = gmtime_s(ptm, &t.S);// Visual Studio
#else
auto result = gmtime(ptm);        // Anything else (like e.g. GCC)
#endif

if (result == nullptr) ...

【讨论】:

  • 是的,编译成功了!谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 2016-02-08
  • 1970-01-01
  • 1970-01-01
  • 2010-11-23
  • 1970-01-01
  • 2014-03-31
相关资源
最近更新 更多