使用带有CMake的外部C ++库时出错

时间:2018-06-05 07:35:40

标签: 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'
}

1 个答案:

答案 0 :(得分:1)

使用gnustl_ *似乎能够构建:

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

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