android - 在 CMake 中使用外部 C++ 库时出错

标签 android c++ android-studio cmake android-ndk

我正尝试在我的 Android 项目中使用这些库,但我遇到了很多类似的错误。我已经安装了所有工具(NDK、LLDB 和 CMake),但我仍然无法使用该库。 尝试运行项目后,无法识别头文件中的所有 C++ 函数,编译器显示 Error:error: undefined reference to 'std::string::c_str() const' 和类似的其他未定义C++ 函数的引用错误。查找完整的错误日志 here .

这是我的 CMakeLists.txt

# 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.4.1)

# 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.
             native-lib

             # Sets the library as a shared library.
             SHARED

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

# 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.

include_directories(src/main/cpp/include/ src/main/cpp/include/alglib3 src/main/cpp/include/device src/main/cpp/include/gsl src/main/cpp/include/base)

add_library ( gsl STATIC IMPORTED)
add_library ( gslcblas STATIC IMPORTED)
add_library ( alglib STATIC IMPORTED)
add_library ( crystalport STATIC IMPORTED)


set_target_properties( crystalport PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/cpp/libs/armeabi-v7a/libcrystalport_android.a)
set_target_properties( gsl PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/cpp/libs/armeabi-v7a/libgsl.a)
set_target_properties( gslcblas PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/cpp/libs/armeabi-v7a/libgslcblas.a)
set_target_properties( alglib PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/cpp/libs/armeabi-v7a/libalglib.a)

target_link_libraries( # Specifies the target library.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib}
                       alglib
                       crystalport
                       gslcblas
                       gsl
                       )

这是我的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "sadboy.circadian"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        externalNativeBuild {
            cmake {
                cppFlags "-std=c++11 -fexceptions"
            }
        }

        ndk {
            abiFilters "armeabi-v7a"
        }

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}

repositories{
    maven {url "https://jitpack.io"}
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    implementation 'com.android.support:support-annotations:27.1.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

最佳答案

使用gnuSTL_* 似乎可以构建:

 externalNativeBuild {
            cmake {
                cppFlags "-std=c++11 -fexceptions"
                arguments "-DANDROID_STL=gnustl_static"
            }
        }

还使用 extern "C"来避免我的 native-lib.cpp 文件中出现运行时错误。

关于android - 在 CMake 中使用外部 C++ 库时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50694589/

相关文章:

c++ - 提供 "safe"Push() 函数与 auto_ptr 一起使用

Android Studio TFS(团队基础服务器)

android - Android Studio 4.0.1,Gradle和React Native:存在时找不到AndroidManifest.XML

java - 在 BaseAdapter 中填充数据时出现 NullPointerException

Android WebView 未加载博客

android - 广播充电器在 Android O 中断开连接

android - 在 Android Canvas 上包装长文本

c++ - 模板的条件编译

java - 任何人都可以解释 C C++ Java 关于动态或静态语言

Android Studio 3.4 : Changing the value for a property with a final value has been deprecated. 这将失败并在 Gradle 6.0 中出现错误