Description
在 Android NDK 下编译 Dobby 时,common/os_arch_features.h 中会报错:use of undeclared identifier 'OSMemory'。
环境
平台: Android (x86_64 / arm64-v8a)
NDK: 25.1.8937393
编译器: clang++ (LLVM)
目标: x86_64-none-linux-android24 / aarch64-none-linux-android24
复现步骤
克隆仓库:git clone https://github.com/jmpews/Dobby.git
在 Android 项目中通过 CMake 以 add_subdirectory(Dobby) 方式集成
执行编译(例如 ./gradlew :app:assembleDebug)
编译错误
In file included from .../platform-posix.cc:27:
In file included from .../PlatformUnifiedInterface/platform.h:3:
In file included from .../source/dobby/common.h:12:
.../common/os_arch_features.h:39:44: error: use of undeclared identifier 'OSMemory'
auto page = (void *)ALIGN_FLOOR(address, OSMemory::PageSize());
^
.../common/os_arch_features.h:40:8: error: use of undeclared identifier 'OSMemory'
if (!OSMemory::SetPermission(page, OSMemory::PageSize(), kReadExecute)) {
^
.../common/os_arch_features.h:40:38: error: use of undeclared identifier 'OSMemory'
if (!OSMemory::SetPermission(page, OSMemory::PageSize(), kReadExecute)) {
^
.../common/os_arch_features.h:40:60: error: use of undeclared identifier 'kReadExecute'
if (!OSMemory::SetPermission(page, OSMemory::PageSize(), kReadExecute)) {
^
根因分析
存在循环包含,导致 OSMemory 在使用时尚未定义:
platform.h 包含 common.h
common.h 包含 os_arch_features.h
os_arch_features.h 包含 platform.h
当 platform-posix.cc 先包含 platform.h 时,platform.h 尚未解析完成,os_arch_features.h 中的 #include "PlatformUnifiedInterface/platform.h" 会被 #pragma once 跳过,此时 OSMemory 和 MemoryPermission 还未声明,因此报错。
建议修复
将 OSMemory 和 MemoryPermission 抽到独立头文件,打破循环依赖,例如:
新建 source/PlatformUnifiedInterface/platform_memory.h,仅包含:
MemoryPermission 枚举
OSMemory 类声明
os_arch_features.h 改为包含 platform_memory.h,而不是 platform.h
platform.h 包含 platform_memory.h,并删除其中重复的 MemoryPermission 和 OSMemory 定义
Description
在 Android NDK 下编译 Dobby 时,common/os_arch_features.h 中会报错:use of undeclared identifier 'OSMemory'。
环境
平台: Android (x86_64 / arm64-v8a)
NDK: 25.1.8937393
编译器: clang++ (LLVM)
目标: x86_64-none-linux-android24 / aarch64-none-linux-android24
复现步骤
克隆仓库:git clone https://github.com/jmpews/Dobby.git
在 Android 项目中通过 CMake 以 add_subdirectory(Dobby) 方式集成
执行编译(例如 ./gradlew :app:assembleDebug)
编译错误
In file included from .../platform-posix.cc:27:
In file included from .../PlatformUnifiedInterface/platform.h:3:
In file included from .../source/dobby/common.h:12:
.../common/os_arch_features.h:39:44: error: use of undeclared identifier 'OSMemory'
auto page = (void *)ALIGN_FLOOR(address, OSMemory::PageSize());
^
.../common/os_arch_features.h:40:8: error: use of undeclared identifier 'OSMemory'
if (!OSMemory::SetPermission(page, OSMemory::PageSize(), kReadExecute)) {
^
.../common/os_arch_features.h:40:38: error: use of undeclared identifier 'OSMemory'
if (!OSMemory::SetPermission(page, OSMemory::PageSize(), kReadExecute)) {
^
.../common/os_arch_features.h:40:60: error: use of undeclared identifier 'kReadExecute'
if (!OSMemory::SetPermission(page, OSMemory::PageSize(), kReadExecute)) {
^
根因分析
存在循环包含,导致 OSMemory 在使用时尚未定义:
platform.h 包含 common.h
common.h 包含 os_arch_features.h
os_arch_features.h 包含 platform.h
当 platform-posix.cc 先包含 platform.h 时,platform.h 尚未解析完成,os_arch_features.h 中的 #include "PlatformUnifiedInterface/platform.h" 会被 #pragma once 跳过,此时 OSMemory 和 MemoryPermission 还未声明,因此报错。
建议修复
将 OSMemory 和 MemoryPermission 抽到独立头文件,打破循环依赖,例如:
新建 source/PlatformUnifiedInterface/platform_memory.h,仅包含:
MemoryPermission 枚举
OSMemory 类声明
os_arch_features.h 改为包含 platform_memory.h,而不是 platform.h
platform.h 包含 platform_memory.h,并删除其中重复的 MemoryPermission 和 OSMemory 定义