【发布时间】:2017-10-05 13:52:26
【问题描述】:
有人可以向我解释如何在 ndk 项目中正确链接用 C++ 编写的外部库吗?
我正在尝试使用本视频中描述的 SDL 库:https://www.youtube.com/watch?v=BSBISI0sCqo&t=306s,但是当我尝试在终端中执行其中一个命令时出现问题:
F:/Programming/Android/Indie/app/src/main/cpp> C:/Users/Danijel/AppData/Local/Android/sdk/ndk-bundle/ndk-build NDK_LIBS_OUT=../jniLibs
此调用导致错误:
Android NDK:未设置 APP_PLATFORM。默认为支持的最低版本 android- 14. Android NDK:您的 APP_BUILD_SCRIPT 指向一个未知文件:F:/Programming/Android/Indie/app/src/main/cpp/jni/Android.mk C:/Users/Danijel/AppData/Local/Android/sdk/ndk-bundle/build//../build/core/add-appl ication.mk:101: *** Android NDK: 中止......停下来。
所以,我知道实际路径包含 cpp 文件夹(我无法将其重命名为 jni),而不是 jni,但是当我尝试在 gradle 中设置路径时也会发生同样的事情。
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := app
LOCAL_CFLAGS := -Wall -Wextra
LOCAL_SRC_FILES := BoardManager.cpp GameEnvironment.cpp GameObject.cpp Player.cpp Renderer.cpp ShaderManager.cpp Utility.cpp TextureManager.cpp
LOCAL_LDLIBS := -lGLESv1_CM -ldl -llog
include $(BUILD_SHARED_LIBRARY)
应用程序.mk:
APP_PLATFORM=android-14
APP_ABI := armeabi-v7a
APP_STL := gnustl_static
CMakeLists.txt:
# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.
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 it for you.
# Gradle automatically packages shared libraries with your APK.
if (${ANDROID_PLATFORM_LEVEL} LESS 12)
message(FATAL_ERROR "OpenGL 2 is not supported before API level 11 (currently using ${ANDROID_PLATFORM_LEVEL}).")
return()
elseif (${ANDROID_PLATFORM_LEVEL} LESS 18)
add_definitions("-DDYNAMIC_ES3")
set(OPENGL_LIB GLESv2)
else ()
set(OPENGL_LIB GLESv3)
endif (${ANDROID_PLATFORM_LEVEL} LESS 11)
include_directories(src/main/cpp/)
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).
# Associated headers in the same location as their source
# file are automatically included.
src/main/cpp/native-lib.cpp
src/main/cpp/BoardManager.cpp
src/main/cpp/GameEnvironment.cpp
src/main/cpp/GameObject.cpp
src/main/cpp/Player.cpp
src/main/cpp/Renderer.cpp
src/main/cpp/ShaderManager.cpp
src/main/cpp/Utility.cpp
src/main/cpp/TextureManager.cpp )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because system libraries are included 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 the
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
${OPENGL_LIB}
EGL
# Links the target library to the log library
# included in the NDK.
${log-lib} )
【问题讨论】:
标签: android c++ android-ndk libraries