我正在尝试使用NDK在Android中使用本机代码(C ++)。尝试制作项目时出现构建失败。 c ++代码取自https://github.com/facebookresearch/fastText/tree/master/src。它已添加到以下位置C:\ Users \ xxx \ AndroidStudioProjects \ blah \ app \ src \ main \ cpp。
我的CMake文件的内容:
cmake_minimum_required(VERSION 2.8.9)
project(fasttext)
# The version number.
set (fasttext_VERSION_MAJOR 0)
set (fasttext_VERSION_MINOR 1)
include_directories(fasttext)
set(CMAKE_CXX_FLAGS " -pthread -std=c++11 -funroll-loops -O3 -march=native")
set(HEADER_FILES
src/main/cpp/args.h
src/main/cpp/densematrix.h
src/main/cpp/dictionary.h
src/main/cpp/fasttext.h
src/main/cpp/loss.h
src/main/cpp/matrix.h
src/main/cpp/meter.h
src/main/cpp/model.h
src/main/cpp/productquantizer.h
src/main/cpp/quantmatrix.h
src/main/cpp/real.h
src/main/cpp/utils.h
src/main/cpp/vector.h)
set(SOURCE_FILES
src/main/cpp/args.cc
src/main/cpp/densematrix.cc
src/main/cpp/dictionary.cc
src/main/cpp/fasttext.cc
src/main/cpp/loss.cc
src/main/cpp/main.cc
src/main/cpp/matrix.cc
src/main/cpp/meter.cc
src/main/cpp/model.cc
src/main/cpp/productquantizer.cc
src/main/cpp/quantmatrix.cc
src/main/cpp/utils.cc
src/main/cpp/vector.cc)
add_library(fasttext-shared SHARED ${SOURCE_FILES} ${HEADER_FILES})
add_library(fasttext-static STATIC ${SOURCE_FILES} ${HEADER_FILES})
add_library(fasttext-static_pic STATIC ${SOURCE_FILES} ${HEADER_FILES})
set_target_properties(fasttext-shared PROPERTIES OUTPUT_NAME fasttext)
set_target_properties(fasttext-static PROPERTIES OUTPUT_NAME fasttext)
set_target_properties(fasttext-static_pic PROPERTIES OUTPUT_NAME fasttext_pic POSITION_INDEPENDENT_CODE True)
add_executable(fasttext-bin src/main/cpp/main.cc)
target_link_libraries(fasttext-bin pthread fasttext-static)
set_target_properties(fasttext-bin PROPERTIES PUBLIC_HEADER
"${HEADER_FILES}" OUTPUT_NAME fasttext)
install (TARGETS fasttext-shared
LIBRARY DESTINATION lib)
install (TARGETS fasttext-static
ARCHIVE DESTINATION lib)
install (TARGETS fasttext-static_pic
ARCHIVE DESTINATION lib)
install (TARGETS fasttext-bin
RUNTIME DESTINATION bin
PUBLIC_HEADER DESTINATION include/fasttext)
失败消息:
Build command failed.
Error while executing process
C:\Users\xxx\AppData\Local\Android\Sdk\cmake\3.6.4111459\bin\cmake.exe
with arguments {--build C:\Users\xxx\AndroidStudioProjects\blah\app\.externalNativeBuild\cmake\debug\x86_64 --target fasttext-bin}
[1/16] Building CXX object CMakeFiles/fasttext-static.dir/src/main/cpp/model.cc.o
[2/16] Building CXX object CMakeFiles/fasttext-static.dir/src/main/cpp/fasttext.cc.o
[3/16] Building CXX object CMakeFiles/fasttext-static.dir/src/main/cpp/args.cc.o
[4/16] Building CXX object CMakeFiles/fasttext-static.dir/src/main/cpp/dictionary.cc.o
[5/16] Building CXX object CMakeFiles/fasttext-static.dir/src/main/cpp/densematrix.cc.o
[6/16] Building CXX object CMakeFiles/fasttext-static.dir/src/main/cpp/loss.cc.o
[7/16] Building CXX object CMakeFiles/fasttext-static.dir/src/main/cpp/matrix.cc.o
[8/16] Building CXX object CMakeFiles/fasttext-static.dir/src/main/cpp/meter.cc.o
[9/16] Building CXX object CMakeFiles/fasttext-static.dir/src/main/cpp/main.cc.o
[10/16] Building CXX object CMakeFiles/fasttext-static.dir/src/main/cpp/utils.cc.o
[11/16] Building CXX object CMakeFiles/fasttext-static.dir/src/main/cpp/vector.cc.o
[12/16] Building CXX object CMakeFiles/fasttext-static.dir/src/main/cpp/productquantizer.cc.o
[13/16] Building CXX object CMakeFiles/fasttext-static.dir/src/main/cpp/quantmatrix.cc.o C:\Users\xxx\AndroidStudioProjects\blah\app\src\main\cpp\fasttext.cc:323:16: warning: 'selectEmbeddings' is deprecated: selectEmbeddings is being deprecated. [-Wdeprecated-declarations]
auto idx = selectEmbeddings(qargs.cutoff);
^
C:\Users\xxx\AndroidStudioProjects\blah\app\src\main\cpp/utils.h:18:
49: note: expanded from macro 'FASTTEXT_DEPRECATED'
#define FASTTEXT_DEPRECATED(msg) __attribute__((__deprecated__(msg)))
C:\Users\xxx\AndroidStudioProjects\blah\app\src\main\cpp\fasttext.cc:551
:5: warning: 'precomputeWordVectors' is deprecated:
precomputeWordVectors is being deprecated. [-Wdeprecated-declarations]
precomputeWordVectors(*wordVectors_);
^
C:\Users\xxx\AndroidStudioProjects\blah\app\src\main\cpp/fasttext.h:180:
3: note: 'precomputeWordVectors' has been explicitly marked deprecated
here
FASTTEXT_DEPRECATED("precomputeWordVectors is being deprecated.")
^
C:\Users\xxx\AndroidStudioProjects\blah\app\src\main\cpp/utils.h:18:49:
note: expanded from macro 'FASTTEXT_DEPRECATED'
#define FASTTEXT_DEPRECATED(msg) __attribute__((__deprecated__(msg)))
2 warnings generated.
[14/16] Linking CXX static library libfasttext.a
[15/16] Building CXX object CMakeFiles/fasttext-
bin.dir/src/main/cpp/main.cc.o
[16/16] Linking CXX executable fasttext
FAILED: cmd.exe /C "cd . && C:\Users\xxx\AppData\Local\Android\Sdk\ndk-
bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --
target=x86_64-none-linux-android21 --gcc-
toolchain=C:/Users/xxx/AppData/Local/Android/Sdk/ndk-
bundle/toolchains/llvm/prebuilt/windows-x86_64 --
sysroot=C:/Users/xxx/AppData/Local/Android/Sdk/ndk-
bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot -pthread -
std=c++11 -funroll-loops -O3 -march=native -O0 -fno-limit-debug-info -
Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -static-
libstdc++ -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -
Wl,--no-undefined -Qunused-arguments -Wl,-z,noexecstack -Wl,--gc-sections
CMakeFiles/fasttext-bin.dir/src/main/cpp/main.cc.o -o fasttext -
lpthread libfasttext.a -latomic -lm && cd ."
C:/Users/xxx/AppData/Local/Android/Sdk/ndk-
bundle/toolchains/llvm/prebuilt/windows-x86_64/lib/gcc/x86_64-linux-
android/4.9.x/../../../../x86_64-linux-android/bin\ld: error: cannot find
-lpthread
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
提前了解为什么我会收到此错误的任何提示/建议。 谢谢!