【发布时间】:2018-06-05 07:35:39
【问题描述】:
我正在尝试在我的 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'
}
【问题讨论】:
-
您显示的错误表明您以某种方式没有与 C++ 标准库链接。如果您进行详细构建(将 CMake 变量
VERBOSE设置为TRUE)并从头开始重新构建,您应该能够准确地看到用于构建的命令和标志。使用它来尝试找出问题所在。还要看一下 CMake 生成输出,它说什么?它想使用什么编译器? -
@Someprogrammerdude 你好,我对使用 CMake 还是很陌生。我将 set(CMAKE_VERBOSE_MAKEFILE ON) 放在 CMakeLists.txt 中,然后执行 'cmake .'在 AS 终端。由于字符限制,我将其放入此文件中。 Link。请帮助:") 我被困了很长时间。
-
从表面上看,导入的库没有正确构建。也许您没有使用相同的 NDK 来构建 libgsl.a 和其他。
-
@AlexCohn 嗨,如何确定哪个 NDK 用于构建 libgsl.a 和其他。确定后该怎么办?
-
最好的方法是自己重建这些库,使用最新的 NDK。
标签: android c++ android-studio cmake android-ndk