【问题标题】:Migrating from ndk-build to CMake - error with build.ninja从 ndk-build 迁移到 CMake - build.ninja 出错
【发布时间】:2018-04-08 06:36:12
【问题描述】:

我一直在尝试从我的 ndk-build 配置转移到 CMake,以便在我的 android 库中构建不同的共享库。我的Android.mk 文件是:

    LOCAL_PATH := $(call my-dir)

######################################################
####   Building internal evaluation and dsp library
######################################################
include $(CLEAR_VARS)

LOCAL_MODULE    := pitchYIN
LOCAL_SRC_FILES := yin.c yinOld.c DSPAlgorithms.c singingEvaluation.c myutils.c
LOCAL_LDLIBS := -llog
LOCAL_CFLAGS := -DVERBOSE=0

include $(BUILD_SHARED_LIBRARY)

######################################################
####   Building soundtouch library
######################################################
include $(CLEAR_VARS)

### Update this local path wrt system ###
MY_SOUNDTOUCH_SRC_DIR := <path-to-the-src-dir>

LOCAL_MODULE    := soundtouch
LOCAL_SRC_FILES := soundtouch-jni.cpp $(MY_SOUNDTOUCH_SRC_DIR)/SoundTouch/AAFilter.cpp  $(MY_SOUNDTOUCH_SRC_DIR)/SoundTouch/FIFOSampleBuffer.cpp \
                    $(MY_SOUNDTOUCH_SRC_DIR)/SoundTouch/FIRFilter.cpp $(MY_SOUNDTOUCH_SRC_DIR)/SoundTouch/cpu_detect_x86.cpp \
                    $(MY_SOUNDTOUCH_SRC_DIR)/SoundTouch/sse_optimized.cpp $(MY_SOUNDTOUCH_SRC_DIR)/SoundStretch/WavFile.cpp \
                    $(MY_SOUNDTOUCH_SRC_DIR)/SoundTouch/RateTransposer.cpp $(MY_SOUNDTOUCH_SRC_DIR)/SoundTouch/SoundTouch.cpp \
                    $(MY_SOUNDTOUCH_SRC_DIR)/SoundTouch/InterpolateCubic.cpp $(MY_SOUNDTOUCH_SRC_DIR)/SoundTouch/InterpolateLinear.cpp \
                    $(MY_SOUNDTOUCH_SRC_DIR)/SoundTouch/InterpolateShannon.cpp $(MY_SOUNDTOUCH_SRC_DIR)/SoundTouch/TDStretch.cpp \
                    $(MY_SOUNDTOUCH_SRC_DIR)/SoundTouch/BPMDetect.cpp $(MY_SOUNDTOUCH_SRC_DIR)/SoundTouch/PeakFinder.cpp

# Including the header files for sound touch ..
LOCAL_C_INCLUDES := $(MY_SOUNDTOUCH_SRC_DIR)
LOCAL_C_INCLUDES += $(MY_SOUNDTOUCH_SRC_DIR)/../includes

# for native audio
LOCAL_LDLIBS += -lgcc
# --whole-archive -lgcc
# for logging
LOCAL_LDLIBS += -llog
# for native asset manager
#LOCAL_LDLIBS += -landroid

# Custom Flags:
# -fvisibility=hidden : don't export all symbols
LOCAL_CFLAGS += -fvisibility=hidden -I $(MY_SOUNDTOUCH_SRC_DIR)/../include -fdata-sections -ffunction-sections

# OpenMP mode : enable these flags to enable using OpenMP for parallel computation
#LOCAL_CFLAGS += -fopenmp
#LOCAL_LDFLAGS += -fopenmp


# Use ARM instruction set instead of Thumb for improved calculation performance in ARM CPUs
LOCAL_ARM_MODE := arm

include $(BUILD_SHARED_LIBRARY)

这里我正在尝试构建两个共享库pitchYINsoundtouch。我现在正试图转向使用CMake 的更独立于平台的方法。这是我尝试写的等价的CMakeLists.txt

    # Sets the minimum version of CMake required to build your native library.
# This ensures that a certain set of CMake features is available to
# your build.

cmake_minimum_required(VERSION 3.4.1)

################Loading other libraries############################
find_library( # Defines the name of the path variable that stores the
              # location of the NDK library.
              log-lib

              # Specifies the name of the NDK library that
              # CMake needs to locate.
              log )

find_library( # Defines the name of the path variable that stores the
              # location of the NDK library.
              gcc-lib

              # Specifies the name of the NDK library that
              # CMake needs to locate.
              gcc )
######################################################################################
######### Adding pitchYIN library to the module
######################################################################################
set( jni_src_DIR ./src/main/jni )

add_library( # Specifies the name of the library.
             pitchYIN

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             ${jni_src_DIR}/yin.c
             ${jni_src_DIR}/yinOld.c
             ${jni_src_DIR}/DSPAlgorithms.c
             ${jni_src_DIR}/singingEvaluation.c
             ${jni_src_DIR}/myutils.c )

# Specifies a path to native header files.
include_directories(${jni_src_DIR})

# Links your native library against one or more other native libraries.
target_link_libraries( # Specifies the target library.
                       pitchYIN

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


######################################################################################
######### Adding soundtouch library to the module
######################################################################################

set( soundtouch_src_DIR <path-to-src> )

add_library( # Specifies the name of the library.
             soundtouch

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             ${jni_src_DIR}/soundtouch-jni.cpp
             ${soundtouch_src_DIR}/SoundTouch/AAFilter.cpp
             ${soundtouch_src_DIR}/SoundTouch/FIFOSampleBuffer.cpp
             ${soundtouch_src_DIR}/SoundTouch/FIRFilter.cpp
             ${soundtouch_src_DIR}/SoundTouch/cpu_detect_x86.cpp
             ${soundtouch_src_DIR}/SoundTouch/sse_optimized.cpp
             ${soundtouch_src_DIR}/SoundStretch/WavFile.cpp
             ${soundtouch_src_DIR}/SoundTouch/RateTransposer.cpp
             ${soundtouch_src_DIR}/SoundTouch/SoundTouch.cpp
             ${soundtouch_src_DIR}/SoundTouch/InterpolateCubic.cpp
             ${soundtouch_src_DIR}/SoundTouch/InterpolateLinear.cpp
             ${soundtouch_src_DIR}/SoundTouch/InterpolateShannon.cpp
             ${soundtouch_src_DIR}/SoundTouch/TDStretch.cpp
             ${soundtouch_src_DIR}/SoundTouch/BPMDetect.cpp
             ${soundtouch_src_DIR}/SoundTouch/PeakFinder.cpp
             )

# Specifies a path to native header files.
include_directories(${soundtouch_src_DIR}
                    ${soundtouch_src_DIR}/../includes)

# Custom Flags:
# -fvisibility=hidden : don't export all symbols
set(gcc_coverage_compile_flags "-fvisibility=hidden -I ${soundtouch_src_DIR}/../include -fdata-sections -ffunction-sections")
add_definitions($(gcc_coverage_compile_flags))

# OpenMP mode : enable these flags to enable using OpenMP for parallel computation
add_definitions("-fopenmp")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")

# Use ARM instruction set instead of Thumb for improved calculation performance in ARM CPUs
# Equivalent to LOCAL_ARM_MODE := arm in Android.mk
# set(CMAKE_ANDROID_ARM_MODE ON)

# Links your native library against one or more other native libraries.
target_link_libraries( # Specifies the target library.
                       soundtouch

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

另外,我的 gradle 文件中有一些 cmake 参数,例如:

.........
defaultConfig {
    /*sourceSets.main {
        jni.srcDirs = [] //disable automatic ndk-build call
        jniLibs.srcDir 'src/main/libs' //set .so files location to libs
    }*/
    externalNativeBuild {
        cmake {
            arguments "-DANDROID_TOOLCHAIN=clang", "-DANDROID_ARM_MODE=arm"
        }
    }
}

....
externalNativeBuild {
    /*ndkBuild {
        path 'src/main/jni/Android.mk'
    }*/

    cmake {
        // Provides a relative path to your CMake build script.
        path "CMakeLists.txt"
    }
}
.........

添加arguments "-DANDROID_TOOLCHAIN=clang", "-DANDROID_ARM_MODE=arm" 是因为LOCAL_ARM_MODE := arm 在我的Android.mk 文件中用于soundtouch 库。当我现在尝试构建它时,我收到以下错误:

Build command failed.
Error while executing process /Users/swapnilgupta/Library/Android/sdk/cmake/3.6.4111459/bin/cmake with arguments {--build /Users/swapnilgupta/work/musicmuni/riyaz/android_lib/audioiolib/.externalNativeBuild/cmake/debug/armeabi-v7a --target clean}
ninja: error: build.ninja:57: bad $-escape (literal $ must be written as $$)

build.ninja 的第 57 行是:

FLAGS = -isystem /Users/swapnilgupta/Library/Android/sdk/ndk-bundle/sysroot/usr/include/aarch64-linux-android -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security   -fopenmp -O2 -DNDEBUG  -fPIC   $(gcc_coverage_compile_flags) -fopenmp

我试图通过网络阅读有关此内容的一些信息,但无法弄清楚在我的情况下是什么原因造成的。请帮忙:)

编辑 1

我已经尝试过 Alex 的回答中提到的事情。它确实帮助我解决了一些问题,但我仍然收到以下链接错误:

Build command failed.
Error while executing process /Users/swapnilgupta/Library/Android/sdk/cmake/3.6.4111459/bin/cmake with arguments {--build /Users/swapnilgupta/work/musicmuni/riyaz/android_lib/audioiolib/.externalNativeBuild/cmake/debug/arm64-v8a --target soundtouch}
[1/1] Linking CXX shared library ../../../../build/intermediates/cmake/debug/obj/arm64-v8a/libsoundtouch.so
FAILED: : && /Users/swapnilgupta/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++  --target=aarch64-none-linux-android --gcc-toolchain=/Users/swapnilgupta/Library/Android/sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64 --sysroot=/Users/swapnilgupta/Library/Android/sdk/ndk-bundle/sysroot -fPIC -isystem /Users/swapnilgupta/Library/Android/sdk/ndk-bundle/sysroot/usr/include/aarch64-linux-android -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security   -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a --sysroot /Users/swapnilgupta/Library/Android/sdk/ndk-bundle/platforms/android-21/arch-arm64 -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,libsoundtouch.so -o ../../../../build/intermediates/cmake/debug/obj/arm64-v8a/libsoundtouch.so CMakeFiles/soundtouch.dir/src/main/jni/soundtouch-jni.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundTouch/AAFilter.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundTouch/FIFOSampleBuffer.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundTouch/FIRFilter.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundTouch/cpu_detect_x86.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundTouch/sse_optimized.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundStretch/WavFile.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundTouch/RateTransposer.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundTouch/SoundTouch.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundTouch/InterpolateCubic.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundTouch/InterpolateLinear.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundTouch/InterpolateShannon.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundTouch/TDStretch.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundTouch/BPMDetect.cpp.o CMakeFiles/soundtouch.dir/Users/swapnilgupta/work/musicmuni/soundtouch/source/SoundTouch/PeakFinder.cpp.o  -llog -lomp -latomic -lm "/Users/swapnilgupta/Library/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/arm64-v8a/libgnustl_static.a" && :
CMakeFiles/soundtouch.dir/src/main/jni/soundtouch-jni.cpp.o: In function `_init_threading(bool)':
/Users/swapnilgupta/work/musicmuni/riyaz/android_lib/audioiolib/src/main/jni/soundtouch-jni.cpp:69: undefined reference to `gomp_tls_key'
/Users/swapnilgupta/work/musicmuni/riyaz/android_lib/audioiolib/src/main/jni/soundtouch-jni.cpp:69: undefined reference to `gomp_tls_key'
/Users/swapnilgupta/work/musicmuni/riyaz/android_lib/audioiolib/src/main/jni/soundtouch-jni.cpp:74: undefined reference to `gomp_tls_key'
/Users/swapnilgupta/work/musicmuni/riyaz/android_lib/audioiolib/src/main/jni/soundtouch-jni.cpp:74: undefined reference to `gomp_tls_key'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

该错误与变量gomp_tls_key 相关,该变量是上述文件中的extern 变量。

【问题讨论】:

  • 你能把生成的 build.ninja 文件的第 57 行贴出来吗?
  • @AlexCohn 更新了! :)
  • 关于 gomp_tls_key,请阅读:stackoverflow.com/a/28579559/192373
  • 好的.. 但仅供参考 extern pthread_key_t gomp_tls_key; 用于声明此变量,pthread_key_t 的类型为 int
  • 我不确定这是否与 extern 关键字 extern 的正确用户有关。似乎在下面的代码中已经处理了非ui线程调用问题。我收到此错误的代码可以在pastebin.com/phf96Nx9 找到

标签: android c++ cmake android-ndk ndk-build


【解决方案1】:
add_definitions($(gcc_coverage_compile_flags)) → add_definitions(${gcc_coverage_compile_flags}) 

其实我不知道你为什么需要这个set();如果你直接,你不会失去任何东西

add_definitions("-fvisibility=hidden -I ${soundtouch_src_DIR}/../include -fdata-sections -ffunction-sections")

另外,使用 -fopenmp 你不需要添加这个标志两次;这条线

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fopenmp")

可以安全移除。

最后,请注意您不需要查找 liblog

target_link_libraries( # Specifies the target library.
                   soundtouch
                   log omp)

借助 NDK CMake 工具链脚本,可以正常工作。

【讨论】:

  • 用您提到的更改更新了代码。现在得到这个构建错误:pastebin.com/mxQSbgGj。仅供参考:我必须包含#include &lt;cstring&gt;,因为构建过程无法找到memcpy()
  • 你错过了 libomp,不是吗?
  • 哦是的..我忘记了...但即使包括在内,我也遇到了同样的问题!
  • 我花了一些时间,用我现在面临的错误更新了问题。
  • 如果我没记错的话,这个解决方法是为 NDK r10 发明的,不再与当前的 NDK 相关。前段时间,我suggested 简单地使用了prebuilt SoundTouch library。自己编译不会有任何收获。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-05
  • 2019-03-20
  • 2018-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-08
相关资源
最近更新 更多