HarmonyOS鸿蒙Next中CMake打包so问题

HarmonyOS鸿蒙Next中CMake打包so问题 cke_183.png

啊啊啊啊!我要疯了!我正在用CMake打包so,这是个啥问题啊?被AI拐带了一上午也没解决(而且AI一会儿我一个样,我都不敢听了)。


更多关于HarmonyOS鸿蒙Next中CMake打包so问题的实战教程也可以访问 https://www.itying.com/category-93-b0.html

5 回复

不用急,看看官方的demo,

https://developer.huawei.com/consumer/cn/codelabsPortal/carddetails/tutorials_AREngine-C

我也跑过,最近研究AR,按照里面的方法就行了

#
#  Copyright (c) 2024-2025 Huawei Device Co., Ltd.
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
#

# the minimum version of CMake.
cmake_minimum_required(VERSION 3.5.0)

# project
project(ARSample)

set(CMAKE_CXX_STANDARD 17)
set(NATIVE_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})

find_library(arengine-lib libarengine_ndk.z.so)

# include directory
include_directories(
    ${NATIVE_ROOT_PATH}/src
    ${NATIVE_ROOT_PATH}/src/utils
    ${NATIVE_ROOT_PATH}/src/graphic
    ${NATIVE_ROOT_PATH}/thirdparty/glm
    ${NATIVE_ROOT_PATH}/thirdparty/stb
)

# Build shared library.
add_library(entry SHARED
    # common
    ${NATIVE_ROOT_PATH}/src/module.cpp
    ${NATIVE_ROOT_PATH}/src/napi_manager.cpp
    ${NATIVE_ROOT_PATH}/src/global.cpp
    ${NATIVE_ROOT_PATH}/src/utils/app_util.cpp
    ${NATIVE_ROOT_PATH}/src/utils/app_file.cpp
    ${NATIVE_ROOT_PATH}/src/utils/renderer_ref.cpp
    ${NATIVE_ROOT_PATH}/src/graphic/GLUtils.cpp
    ${NATIVE_ROOT_PATH}/src/graphic/RenderAttribute.cpp
    ${NATIVE_ROOT_PATH}/src/graphic/RenderContext.cpp
    ${NATIVE_ROOT_PATH}/src/graphic/RenderSurface.cpp

    # world
    ${NATIVE_ROOT_PATH}/src/world/world_ar_application.cpp
    ${NATIVE_ROOT_PATH}/src/world/world_background_renderer.cpp
    ${NATIVE_ROOT_PATH}/src/world/world_object_renderer.cpp
    ${NATIVE_ROOT_PATH}/src/world/world_plane_renderer.cpp
    ${NATIVE_ROOT_PATH}/src/world/world_render_manager.cpp
    ${NATIVE_ROOT_PATH}/src/world/world_file_manager.cpp

    # depth
    ${NATIVE_ROOT_PATH}/src/depth/depth_ar_application.cpp
    ${NATIVE_ROOT_PATH}/src/depth/depth_background_renderer.cpp
    ${NATIVE_ROOT_PATH}/src/depth/depth_render_manager.cpp
    ${NATIVE_ROOT_PATH}/src/depth/depth_background_no_renderer.cpp

     # mesh
    ${NATIVE_ROOT_PATH}/src/mesh/mesh_ar_application.cpp
    ${NATIVE_ROOT_PATH}/src/mesh/mesh_background_renderer.cpp
    ${NATIVE_ROOT_PATH}/src/mesh/mesh_render_manager.cpp
    ${NATIVE_ROOT_PATH}/src/mesh/scenemesh_display_renderer.cpp
    ${NATIVE_ROOT_PATH}/src/mesh/mesh_object_renderer.cpp

    # image
    ${NATIVE_ROOT_PATH}/src/image/image_ar_application.cpp
    ${NATIVE_ROOT_PATH}/src/image/image_background_renderer.cpp
    ${NATIVE_ROOT_PATH}/src/image/image_render_manager.cpp
    ${NATIVE_ROOT_PATH}/src/image/image_render_base.cpp
    ${NATIVE_ROOT_PATH}/src/image/image_line_render.cpp

    # semanticdense
    ${NATIVE_ROOT_PATH}/src/semanticdense/semanticdense_ar_application.cpp
    ${NATIVE_ROOT_PATH}/src/semanticdense/semanticdense_background_renderer.cpp
    ${NATIVE_ROOT_PATH}/src/semanticdense/semanticdense_point_cloud_renderer.cpp
    ${NATIVE_ROOT_PATH}/src/semanticdense/semanticdense_render_manager.cpp
    ${NATIVE_ROOT_PATH}/src/semanticdense/semanticdense_cube_renderer.cpp
)

# Link the OpenHarmony native library (using OpenGL).
target_link_libraries(entry PUBLIC
    EGL
    GLESv3
    hilog_ndk.z
    ace_ndk.z
    ace_napi.z
    z
    uv
    ace_napi.z
    rawfile.z
    libohfileio.so
    image_source
    pixelmap
    pixelmap_ndk.z
    ${arengine-lib}
    libnative_display_manager.so
)

https://gitcode.com/HarmonyOS_Codelabs/arengine-codelab-ar-sample

更多关于HarmonyOS鸿蒙Next中CMake打包so问题的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


我给您举一个常见场景,看是否和您截图里一致:CMake 打包 so 时如果是 find_library 找不到库,或 target_link_libraries 链接失败,通常先看三处:1)CMakeLists.txt 里的库名是否写对,例如 AR Engine 官方示例里是 find_library(arengine-lib libarengine_ndk.z.so);2)build-profile.json5 的 abiFilters 是否和设备一致,优先确认 arm64-v8a;3)自编/三方 so 是否放在模块可搜索目录下。

麻烦补充截图里的完整错误文本、CMakeLists.txt、DevEco Studio/SDK 版本和目标 ABI,我这边才能判断是库路径、ABI 还是链接参数问题。参考:AR Engine Codelab(官方示例)https://developer.huawei.com/consumer/cn/codelabsPortal/carddetails/tutorials_AREngine-C

从你光标高亮条那看报错意思是,CMakeLists.txt 中没有链接华为库或 AR Engine 相关的 .so 动态链接库或静态库

在HarmonyOS Next中,使用CMake打包so文件需配置ohos-sdk提供的工具链。在CMakeLists.txt中设置CMAKE_TOOLCHAIN_FILE指向$OHOS_SDK/native/build/cmake/ohos.toolchain.cmake,并通过OHOS_ARCH指定目标架构(如arm64-v8a)。执行cmake -B buildcmake --build build生成so文件。注意用OHOS_STL=c++_shared控制C++运行时。

由于未提供具体报错信息,根据“CMake 打包 so”的常见问题,整理几个可能原因:

  1. CMake 工具链未正确配置
    需显式指定 OHOS NDK 中的 ohos.toolchain.cmake,并传入平台与架构参数,例如:
    -DOHOS_STL=c++_shared -DOHOS_ARCH=arm64-v8a -DOHOS_PLATFORM=OHOS
    否则会出现 “Could not find toolchain” 或找不到系统头文件。

  2. native api 版本不匹配
    OHOS API Version ≥9 才支持某些符号,若项目中使用了新 API 却设置了低版本,会报 undefined symbol。检查 CMake 中的 OHOS_API_VERSIONtarget_compile_definitionsOHOS_API_LEVEL

  3. 未链接必要的平台库
    例如 NAPI 库需 ace_napi.z,渲染库需 ace_ndk.z 等。缺省时会报 “undefined reference”。确认 target_link_libraries 中已包含所需系统库。

  4. ABI 与设备不匹配
    若打包时只生成 armeabi-v7a,而设备只支持 arm64-v8a,会导致运行时加载失败。检查构建命令或 build-profile.json5 中的 abiFilters

  5. STL 类型不一致
    so 与其依赖库使用的 C++ 标准库必须一致。若某处用了 c++_static,另一处用 c++_shared,会导致符号冲突或找不到。

  6. 缺少 -fPIC 编译选项
    构建动态库时必须开启位置无关代码,否则链接静态库时会报 “requires unsupported dynamic reloc” 错误。

请对照构建日志中的具体错误信息定位上述问题。

回到顶部