1

我环顾四周,但我只能找到使用Android.mk和的解决方案Application.mk。我是否认为通过使用CMakeLists.txt我不需要这些文件中的任何一个?这是我第一次尝试使用 Android NDK。

我的基础是我CMakeLists.txtSuperpoweredExample,我已经打电话给我的图书馆nativelib。.cpp 文件似乎无法获取头文件中的引用。难道我做错了什么?它构建得很好,但它只是在编译时抛出这些错误。

CMakeLists.txt:

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.

add_library( # Sets the name of the library.
             nativelib

             # 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.
             G:/Git/NDKTest/app/src/main/jni/nativelib.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.
                       nativelib

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

//////////////////////////////////////////////////////////////    
//////////////////////////////////////////////////////////////
// Below here is copy and pasted from the SuperpoweredExample
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////

set(
    PATH_TO_SUPERPOWERED
    "C:/Users/j/Downloads/Superpowered/Superpowered"
)

message(${ANDROID_ABI})

file(GLOB CPP_FILES "*.cpp")

add_library(
    SuperpoweredExample
    SHARED
    ${CPP_FILES}
    ${PATH_TO_SUPERPOWERED}/AndroidIO/SuperpoweredAndroidAudioIO.cpp
)

include_directories(src/main/jni)
include_directories(${PATH_TO_SUPERPOWERED})

target_link_libraries(
    SuperpoweredExample
    log
    android
    OpenSLES
    ${PATH_TO_SUPERPOWERED}/libSuperpoweredAndroid${ANDROID_ABI}.a
    nativelib
)

堆栈跟踪:

Error:FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:externalNativeBuildDebug'.
> Build command failed.
Error while executing 'G:\sdk1\cmake\3.6.3155560\bin\cmake.exe' with arguments {--build G:\Git\NDKTest\app\.externalNativeBuild\cmake\debug\x86 --target nativelib}
[1/2] Building CXX object CMakeFiles/nativelib.dir/nativelib.cpp.o
[2/2] Linking CXX shared library G:\Git\NDKTest\app\build\intermediates\cmake\debug\obj\x86\libnativelib.so
FAILED: cmd.exe /C "cd . && G:\sdk1\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe  -target i686-none-linux-android -gcc-toolchain G:/sdk1/ndk-bundle/toolchains/x86-4.9/prebuilt/windows-x86_64 --sysroot=G:/sdk1/ndk-bundle/platforms/android-16/arch-x86 -fPIC -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -fno-exceptions -fno-rtti -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -fno-exceptions -fno-rtti -fsigned-char -Inull -O0 -fno-limit-debug-info -O0 -fno-limit-debug-info  -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnativelib.so -o G:\Git\NDKTest\app\build\intermediates\cmake\debug\obj\x86\libnativelib.so CMakeFiles/nativelib.dir/nativelib.cpp.o  -llog -lm "G:/sdk1/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/x86/libgnustl_static.a" && cd ."
G:\Git\NDKTest\app\src\main\jni/nativelib.cpp:16: error: undefined reference to 'SuperpoweredRoll::SuperpoweredRoll(unsigned int)'
  G:\Git\NDKTest\app\src\main\jni/nativelib.cpp:17: error: undefined reference to 'SuperpoweredFilter::SuperpoweredFilter(SuperpoweredFilterType, unsigned int)'
  G:\Git\NDKTest\app\src\main\jni/nativelib.cpp:18: error: undefined reference to 'SuperpoweredFlanger::SuperpoweredFlanger(unsigned int)'
  G:\Git\NDKTest\app\src\main\jni/nativelib.cpp:19: error: undefined reference to 'SuperpoweredRecorder::SuperpoweredRecorder(char const*, unsigned int, unsigned int)'
  G:\Git\NDKTest\app\src\main\jni/nativelib.cpp:21: error: undefined reference to 'SuperpoweredAndroidAudioIO::SuperpoweredAndroidAudioIO(int, int, bool, bool, bool (*)(void*, short*, int, int), void*, int, int, int)'
  G:\Git\NDKTest\app\src\main\jni/nativelib.cpp:28: error: undefined reference to 'SuperpoweredRecorder::stop()'
  G:\Git\NDKTest\app\src\main\jni/nativelib.cpp:30: error: undefined reference to 'SuperpoweredRecorder::start(char const*)'
  G:\Git\NDKTest\app\src\main\jni/nativelib.cpp:37: error: undefined reference to 'SuperpoweredRecorder::process(float*, float*, unsigned int)'
  G:\Git\NDKTest\app\src\main\jni/nativelib.cpp:38: error: undefined reference to 'SuperpoweredFloatToShortInt(float*, short*, unsigned int, unsigned int)'
  G:\Git\NDKTest\app\src\main\jni/nativelib.cpp:43: error: undefined reference to 'SuperpoweredAndroidAudioIO::~SuperpoweredAndroidAudioIO()'
  G:\Git\NDKTest\app\src\main\jni/nativelib.cpp:44: error: undefined reference to 'SuperpoweredRecorder::~SuperpoweredRecorder()'
  clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
  ninja: build stopped: subcommand failed.


* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

将名称从 native-lib 更改为 nativelib,同样的错误。

我有一种感觉,错误target_link_libraries()CMakeLists.txt. 我正在尝试将我的NativeLib代码与现有SuperpoweredExample代码链接起来。

4

1 回答 1

3

我认为链接您的“nativelib”发生在链接 Superpowered 示例之前,因此您的链接器找不到“nativelib”使用的 Superpowered 部分。

于 2016-12-02T17:35:37.420 回答