You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio集成OpenCV遇__bss_start符号链接错误求解决

问题描述

我在Android Studio中开发集成OpenCV库的项目,使用NDK r23与CMake 3.18.1,运行时出现链接错误:

C/C++: ld: error: found local symbol '__bss_start' in global part of symbol table in file C:/Users/HYPER_STOCK_TABRIZ/AndroidStudioProjects/Slam3/app/src/main/jniLibs/arm64-v8a/libopencv_java3.so

已尝试相关解决方案但未解决,项目配置如下:

app/build.gradle 配置

plugins {
    id 'com.android.application'
}

android {
    namespace 'com.example.slam3'
    compileSdk 33

    defaultConfig {
        applicationId "com.example.slam3"
        minSdk 29
        targetSdk 33
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags '-std=c++11'
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }


    externalNativeBuild {
        cmake {
            path file('src/main/cpp/CMakeLists.txt')
            version '3.18.1'
        }
    }
    buildFeatures {
        viewBinding true
    }
}

dependencies {
    implementation 'androidx.appcompat:appcompat:1.6.0'
    implementation 'com.google.android.material:material:1.8.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation project(path: ':openCV')
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}

CMakeLists.txt 配置

set(pathToProject C:/Users/HYPER_STOCK_TABRIZ/AndroidStudioProjects/Slam3)
set(pathToOpenCV C:/Users/HYPER_STOCK_TABRIZ/AndroidStudioProjects/OpenCV-android-sdk)

#For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.18.1)

set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")

include_directories(${pathToOpenCV}/sdk/native/jni/include)

# Declares and names the project.
project("slam3")

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
        slam3

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        native-lib.cpp)

add_library(lib_opencv SHARED IMPORTED)

set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${pathToProject}/app/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries(slam3 ${log-lib} lib_opencv)

解决方案

这个错误源于OpenCV预编译库的符号表异常,或是与当前NDK版本的链接器不兼容,可按以下步骤尝试解决:

  • 替换兼容版本的OpenCV库
    卸载当前OpenCV Android SDK,下载与NDK r23兼容的版本(推荐OpenCV 4.5.x及以上),确保包含arm64-v8a等目标架构的预编译库,替换jniLibs目录下的旧库文件。

  • 调整CMake链接配置
    将OpenCV的动态库链接改为静态库链接,避免符号冲突,修改CMakeLists.txt相关部分:

    # 替换原有的add_library和set_target_properties代码块
    add_library(lib_opencv STATIC IMPORTED)
    set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${pathToOpenCV}/sdk/native/staticlibs/${ANDROID_ABI}/libopencv_java3.a)
    # 链接OpenCV依赖的所有静态库
    file(GLOB opencv_libs ${pathToOpenCV}/sdk/native/staticlibs/${ANDROID_ABI}/libopencv_*.a)
    target_link_libraries(slam3 ${log-lib} ${opencv_libs})
    

    同时删除app/build.gradle的dependencies中implementation project(path: ':openCV')这一行,避免重复引入。

  • 清理缓存后重新构建
    执行Build -> Clean Project,再执行Build -> Rebuild Project,清除旧编译缓存;检查jniLibs目录,确保只保留当前使用架构的库文件。

  • 降级NDK版本(备选)
    若以上方法无效,可尝试将NDK版本降级到r21或r22,这些版本与旧版OpenCV库兼容性更稳定,修改local.properties中的ndk.dir指向对应版本的NDK路径。

内容的提问来源于stack exchange,提问作者Zizooo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 11:01:29