【问题标题】:Android NDK compilation error with struct stat使用 struct stat 的 Android NDK 编译错误
【发布时间】:2017-06-23 02:59:49
【问题描述】:

我在将代码编译为 android native 时遇到问题。我在编译期间收到此错误:

'struct stat' has no member named 'st_ctim'

在 struct stat 的头文件中,该结构被称为“st_ctime”(注意末尾的“e”)

有没有一种方法可以在不修改代码的情况下编译代码? 是否有一个带有 struct stat (stat.h) 的 android native API 版本,其中包含成员 'st_ctim' 而不是 'st_ctime' ?

我正在针对 Android 原生 API 级别 26 进行编译。到目前为止,我已经看到在 API 级别 8、9、23 中存在同样的问题。

我正在报告 st_ctim 的问题,但所有其他成员都以“e”结尾(st_atime,st_mtime)

为了清楚起见,为什么 libc struct stat 和 android native struct stat 之间存在差异?

【问题讨论】:

  • 它是哪种语言?不要为不同的语言发送垃圾标签!
  • @Olaf 这是语言 C++,我有没有垃圾邮件?我正在使用 arm-linux-androideabi-g++ 作为编译器。我正在尝试编译的源代码是用 C++ 编写的
  • 是的,C 不是 C++;它们是不同的语言!
  • @Olaf 非常感谢您的编辑。关于问题本身的任何线索?

标签: android c++ struct android-ndk


【解决方案1】:

如果您使用的是现代版本的 NDK,您将拥有一个现代版的 struct stathttps://android.googlesource.com/platform/bionic/+/master/libc/include/sys/stat.h

请注意,您需要使用 NDK 的 unified headers 来获取最新的标头。这是 r15 中的默认设置,但为 r14 选择加入。

【讨论】:

  • 有趣的是我正在使用预构建的工具链进行交叉编译,它没有提到使用统一头文件的任何选项......而且我也在使用最新的 ndk r15 .. 谢谢你指点我那。我会相应地更新它 1-使用统一的标头和 2-使用 clang 而不是 gcc/g++ 构建系统!!
  • "prebuilt toolchain" 就像直接从 toolchains/blah 使用编译器并自己设置所有编译器标志一样?如果这就是你的意思,你应该尝试使用standalone toolchain
【解决方案2】:

st_ctim 是 stat 的新成员,它包含纳秒精度时间(struct timespec)。不幸的是,android 没有。这是lstat手册的摘录(Linux机器):

struct stat {
      /* I stripped several members here (st_mode, etc.) */

      /* Since Linux 2.6, the kernel supports nanosecond
         precision for the following timestamp fields.
         For the details before Linux 2.6, see NOTES. */

         struct timespec st_atim;  /* time of last access */
         struct timespec st_mtim;  /* time of last modification */
         struct timespec st_ctim;  /* time of last status change */

#define st_atime st_atim.tv_sec      /* Backward compatibility */
#define st_mtime st_mtim.tv_sec
#define st_ctime st_ctim.tv_sec
};

旧内核和旧标准不支持纳秒 时间戳字段。相反,有三个时间戳 字段——st_atime, st_mtime 和 st_ctime — 键入为 time_t,以一秒的精度记录时间戳。

从内核 2.5.48 开始,stat 结构支持三个文件时间戳字段的纳秒分辨率。纳秒 的组成部分 如果定义了合适的功能测试宏,则每个时间戳都可以通过 st_atim.tv_nsec 形式的名称获得。 纳秒时间戳 在 POSIX.1-2008 中进行了标准化,并且从 2.12 版开始,如果 _POSIX_C_SOURCE 是,glibc 会公开纳秒组件名称 定义为 200809L 或更大,或者 _XOPEN_SOURCE 定义为 700 或更大。直到并包括 glibc 2.19,该 如果定义了 _BSD_SOURCE 或 _SVID_SOURCE,则还定义了纳秒组件的定义。如果没有上述宏 已定义,然后以 st_atimensec 形式的名称公开纳秒值。

所以你的代码依赖于这个新特性,我认为你必须使用 st_ctime 而不是 st_ctim 来修改它。

【讨论】:

  • 感谢您的提示!好的,我相应地更改了代码,现在它通过了这一步。我不想这样做,因为它不是我的代码,但我想我别无选择。谢谢
  • “不幸的是,android 没有。”如果您使用的是现代 NDK,则情况并非如此。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-28
  • 1970-01-01
  • 1970-01-01
  • 2012-01-22
  • 1970-01-01
  • 2012-10-15
  • 1970-01-01
相关资源
最近更新 更多