[英]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)
在這里,我正在嘗試構建兩個共享庫pitchYIN
和soundtouch
。 我現在正嘗試轉向使用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.