繁体   English   中英

从ndk-build迁移到CMake-build.ninja错误

[英]Migrating from ndk-build to CMake - error with build.ninja

我一直在尝试从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"原因是因为soundtouch库的Android.mk文件中的LOCAL_ARM_MODE := arm 当我现在尝试构建它时,出现以下错误:

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

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

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变量。

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工具链脚本,因此可以正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM