编译报错如下:
Build command failed.
Error while executing 'D:\SDK\cmake\3.6.3155560\bin\cmake.exe' with arguments {-HD:\MyTest\MyCpp\app -BD:\MyTest\MyCpp\app\.externalNativeBuild\cmake\debug\armeabi-v7a -GAndroid Gradle - Ninja -DANDROID_ABI=armeabi-v7a -DANDROID_NDK=D:\SDK\android-ndk-r12b -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=D:\MyTest\MyCpp\app\build\intermediates\cmake\debug\obj\armeabi-v7a -DCMAKE_BUILD_TYPE=Debug -DCMAKE_MAKE_PROGRAM=D:\SDK\cmake\3.6.3155560\bin\ninja.exe -DCMAKE_TOOLCHAIN_FILE=D:\SDK\cmake\3.6.3155560\android.toolchain.cmake -DANDROID_NATIVE_API_LEVEL=19 -DCMAKE_C_FLAGS=-D_EXAMPLE_C_FLAG1 -D_EXAMPLE_C_FLAG2 -DCMAKE_CXX_FLAGS=-D__STDC_FORMAT_MACROS -DCMAKE_VERBOSE_MAKEFILE=TRUE}
-- Check for working C compiler: D:/SDK/android-ndk-r12b/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe
-- Check for working C compiler: D:/SDK/android-ndk-r12b/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: D:/SDK/android-ndk-r12b/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe
-- Check for working CXX compiler: D:/SDK/android-ndk-r12b/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring incomplete, errors occurred!
See also "D:/MyTest/MyCpp/app/.externalNativeBuild/cmake/debug/armeabi-v7a/CMakeFiles/CMakeOutput.log".
CMake Error at CMakeLists.txt:12 (add_library):
  The target name "test" is reserved or not valid for certain CMake features,
  such as generator expressions, and may result in undefined behavior.
CMake Error at CMakeLists.txt:43 (target_link_libraries):
  Cannot specify link libraries for target "test" which is not built by this
  project.
Error:executing external native build for cmake D:\MyTest\MyCpp\app\CMakeLists.txt
Build command failed.
Error while executing 'D:\SDK\cmake\3.6.3155560\bin\cmake.exe' with arguments {-HD:\MyTest\MyCpp\app -BD:\MyTest\MyCpp\app\.externalNativeBuild\cmake\release\armeabi-v7a -GAndroid Gradle - Ninja -DANDROID_ABI=armeabi-v7a -DANDROID_NDK=D:\SDK\android-ndk-r12b -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=D:\MyTest\MyCpp\app\build\intermediates\cmake\release\obj\armeabi-v7a -DCMAKE_BUILD_TYPE=Release -DCMAKE_MAKE_PROGRAM=D:\SDK\cmake\3.6.3155560\bin\ninja.exe -DCMAKE_TOOLCHAIN_FILE=D:\SDK\cmake\3.6.3155560\android.toolchain.cmake -DANDROID_NATIVE_API_LEVEL=19 -DCMAKE_C_FLAGS=-D_EXAMPLE_C_FLAG1 -D_EXAMPLE_C_FLAG2 -DCMAKE_CXX_FLAGS=-D__STDC_FORMAT_MACROS -DCMAKE_VERBOSE_MAKEFILE=TRUE}
-- Check for working C compiler: D:/SDK/android-ndk-r12b/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe
-- Check for working C compiler: D:/SDK/android-ndk-r12b/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: D:/SDK/android-ndk-r12b/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe
-- Check for working CXX compiler: D:/SDK/android-ndk-r12b/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring incomplete, errors occurred!
See also "D:/MyTest/MyCpp/app/.externalNativeBuild/cmake/release/armeabi-v7a/CMakeFiles/CMakeOutput.log".
CMake Error at CMakeLists.txt:12 (add_library):
  The target name "test" is reserved or not valid for certain CMake features,
  such as generator expressions, and may result in undefined behavior.
CMake Error at CMakeLists.txt:43 (target_link_libraries):
  Cannot specify link libraries for target "test" which is not built by this
  project.
Error:executing external native build for cmake D:\MyTest\MyCpp\app\CMakeLists.txt

解决方案 »

  1.   

    问题解决了,研究了一晚上其实是我的cmakelist.txt中so库的名称不符合规范,android自己建立的c++支持项目中库名是native-lib,
    我直接写的库名是test,必须将库名写成test-lib,然后编译就可以了
      

  2.   

    博主我现在也遇见这个问题请问怎么去解决 谢谢
    # For more information about using CMake with Android Studio, read the
    # documentation: https://d.android.com/studio/projects/add-native-code.html# Sets the minimum version of CMake required to build the native library.cmake_minimum_required(VERSION 3.4.1)# set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI})# Creates and names a library, sets it as either STATIC
    # or SHARED, and provides the relative paths to its source code.
    # You can define multiple libraries, and CMake builds them for you.
    # Gradle automatically packages shared libraries with your APK.add_library( # Sets the name of the library.
                 SerialPort             # Sets the library as a shared library.
                 SHARED             # Provides a relative path to your source file(s).
                 src/main/jni/SerialPort.c )# Searches for a specified prebuilt library and stores the path as a
    # variable. Because CMake includes system libraries in the search path by
    # default, you only need to specify the name of the public NDK library
    # you want to add. CMake verifies that the library exists before
    # completing its build.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 )# Specifies libraries CMake should link to your target library. You
    # can link multiple libraries, such as libraries you define in this
    # build script, prebuilt third-party libraries, or system libraries.target_link_libraries( # Specifies the target library.
                           SerialPort                       # Links the target library to the log library
                           # included in the NDK.
                           ${log-lib} )