Android NDK.Build command failed. Undefined reference. clang++: error: linker command failed with exit code 1
Android NDK.Build command failed. Undefined reference. clang++: error: linker command failed with exit code 1
当我尝试使用 add_library(mycpp-lib ...)
和 add_library(native-lib ...)
制作单独的库时出现此错误。当我使用单个 add_library()
构建时,我没有收到错误。
注意:两个库(libmycpp-lib.so和libnative-lib.so)都生成成功了。
这是我遇到的错误:
Build command failed.
Error while executing process /Users/vk/Library/Android/sdk/cmake/3.10.2.4988404/bin/ninja with arguments {-C /Users/vk/Development/MyNativeApp1/app/.cxx/cmake/debug/arm64-v8a mycpp-lib native-lib}
ninja: Entering directory `/Users/vk/Development/MyNativeApp1/app/.cxx/cmake/debug/arm64-v8a'
[1/2] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o
[2/2] Linking CXX shared library ../../../../build/intermediates/cmake/debug/obj/arm64-v8a/libnative-lib.so
FAILED: ../../../../build/intermediates/cmake/debug/obj/arm64-v8a/libnative-lib.so
: && /Users/vk/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ --target=aarch64-none-linux-android21 --gcc-toolchain=/Users/vk/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64 --sysroot=/Users/vk/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security -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 -shared -Wl,-soname,libnative-lib.so -o ../../../../build/intermediates/cmake/debug/obj/arm64-v8a/libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -llog -latomic -lm && :
CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o: In function `Java_vikas_example_com_mynativeapp1_MainActivity_stringFromJNI':
/Users/vk/Development/MyNativeApp1/app/.cxx/cmake/debug/arm64-v8a/../../../../src/main/cpp/native-lib.cpp:14: undefined reference to `addtwo(int, int)'
/Users/vk/Development/MyNativeApp1/app/.cxx/cmake/debug/arm64-v8a/../../../../src/main/cpp/native-lib.cpp:16: undefined reference to `array_pointer(int*, int)'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
这是我的 CMakeLists.txt:
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_VERBOSE_MAKEFILE on)
aux_source_directory(src/main/cpp/cpp_basic/airthmatic CPP_SRC)
aux_source_directory(src/main/cpp/cpp_basic CPP_BASIC_SRC)
include_directories(src/main/cpp/cpp_basic/airthmatic)
include_directories(src/main/cpp/cpp_basic)
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp
)
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)
add_library(mycpp-lib
SHARED
${CPP_SRC}
${CPP_BASIC_SRC})
target_link_libraries( # Specifies the target library.
native-lib
${mycpp-lib}
${log-lib}
)
mycpp-lib
目标名称不是用 set()
命令定义的变量,因此用 ${mycpp-lib}
扩展它会产生一个空字符串。使用 target_link_libraries()
时,您可以在链接先前定义的目标时简单地输入 目标名称 (不带 ${}
):
target_link_libraries( # Specifies the target library.
native-lib
mycpp-lib
${log-lib}
)
注意,对于 log-lib
,您 需要 ${}
来扩展由 find_library()
调用定义的缓存变量。
当我尝试使用 add_library(mycpp-lib ...)
和 add_library(native-lib ...)
制作单独的库时出现此错误。当我使用单个 add_library()
构建时,我没有收到错误。
注意:两个库(libmycpp-lib.so和libnative-lib.so)都生成成功了。
这是我遇到的错误:
Build command failed.
Error while executing process /Users/vk/Library/Android/sdk/cmake/3.10.2.4988404/bin/ninja with arguments {-C /Users/vk/Development/MyNativeApp1/app/.cxx/cmake/debug/arm64-v8a mycpp-lib native-lib}
ninja: Entering directory `/Users/vk/Development/MyNativeApp1/app/.cxx/cmake/debug/arm64-v8a'
[1/2] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o
[2/2] Linking CXX shared library ../../../../build/intermediates/cmake/debug/obj/arm64-v8a/libnative-lib.so
FAILED: ../../../../build/intermediates/cmake/debug/obj/arm64-v8a/libnative-lib.so
: && /Users/vk/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang++ --target=aarch64-none-linux-android21 --gcc-toolchain=/Users/vk/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64 --sysroot=/Users/vk/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security -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 -shared -Wl,-soname,libnative-lib.so -o ../../../../build/intermediates/cmake/debug/obj/arm64-v8a/libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -llog -latomic -lm && :
CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o: In function `Java_vikas_example_com_mynativeapp1_MainActivity_stringFromJNI':
/Users/vk/Development/MyNativeApp1/app/.cxx/cmake/debug/arm64-v8a/../../../../src/main/cpp/native-lib.cpp:14: undefined reference to `addtwo(int, int)'
/Users/vk/Development/MyNativeApp1/app/.cxx/cmake/debug/arm64-v8a/../../../../src/main/cpp/native-lib.cpp:16: undefined reference to `array_pointer(int*, int)'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
这是我的 CMakeLists.txt:
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_VERBOSE_MAKEFILE on)
aux_source_directory(src/main/cpp/cpp_basic/airthmatic CPP_SRC)
aux_source_directory(src/main/cpp/cpp_basic CPP_BASIC_SRC)
include_directories(src/main/cpp/cpp_basic/airthmatic)
include_directories(src/main/cpp/cpp_basic)
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp
)
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)
add_library(mycpp-lib
SHARED
${CPP_SRC}
${CPP_BASIC_SRC})
target_link_libraries( # Specifies the target library.
native-lib
${mycpp-lib}
${log-lib}
)
mycpp-lib
目标名称不是用 set()
命令定义的变量,因此用 ${mycpp-lib}
扩展它会产生一个空字符串。使用 target_link_libraries()
时,您可以在链接先前定义的目标时简单地输入 目标名称 (不带 ${}
):
target_link_libraries( # Specifies the target library.
native-lib
mycpp-lib
${log-lib}
)
注意,对于 log-lib
,您 需要 ${}
来扩展由 find_library()
调用定义的缓存变量。