简体   繁体   中英

Link STL library with Gradle CMake project

I have simple Gradle project and trying to add native library building it using CMake

Root CMakeLists.txt

project(mine_lib)
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)

set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/bin")

configure_file("${PROJECT_SOURCE_DIR}/src/buildinfo/buildinfo.hpp.in" "${PROJECT_BINARY_DIR}/buildinfo.hpp")

set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${warnings}")
set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} ${warnings}")

add_subdirectory(${PROJECT_SOURCE_DIR}/src)

include_directories(${PROJECT_SOURCE_DIR}/src/data)
include_directories("${PROJECT_BINARY_DIR}")

add_library(mine_lib SHARED "${PROJECT_SOURCE_DIR}/src/jni/jniwrapper.c")

build.gradle

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        applicationId "..."
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

        defaultConfig {
            externalNativeBuild {
                cmake {
                    arguments "-DANDROID_STL=system"
                    cppFlags "-fexceptions"
                }
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    externalNativeBuild {
        cmake {
            path "src/main/mine_lib/CMakeLists.txt"
        }
    }

    sourceSets {
        main {
            jniLibs.srcDirs 'src/main/mine_lib/src/'
        }
    }
}

In my native library I use part of STL library, for example cstdint . And for now I'm getting compilation error which looks like that:

Error:FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:externalNativeBuildDebug'.
> Build command failed.
  Error while executing process /AndroidSDK/android-sdk-macosx/cmake/3.6.4111459/bin/cmake with arguments {--build /Users/user/IdeaProjects/Sandbox/MyApp/app/.externalNativeBuild/cmake/debug/mips64 --target mine_lib}
  [1/2] Building C object CMakeFiles/mine_lib.dir/src/jni/jniwrapper.c.o
  FAILED: /AndroidSDK/android-sdk-macosx/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/bin/clang  --target=mips64el-none-linux-android --gcc-toolchain=/AndroidSDK/android-sdk-macosx/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/darwin-x86_64 --sysroot=/AndroidSDK/android-sdk-macosx/ndk-bundle/sysroot -Dheif_EXPORTS -I/Users/user/IdeaProjects/Sandbox/HEIFGallery/app/src/main/mine_lib/src/reader -I. -isystem /AndroidSDK/android-sdk-macosx/ndk-bundle/sysroot/usr/include/mips64el-linux-android -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fintegrated-as -Wa,--noexecstack -Wformat -Werror=format-security   -O0 -fno-limit-debug-info  -fPIC -MD -MT CMakeFiles/mine_lib.dir/src/jni/jniwrapper.c.o -MF CMakeFiles/mine_lib.dir/src/jni/jniwrapper.c.o.d -o CMakeFiles/mine_lib.dir/src/jni/jniwrapper.c.o   -c /Users/user/IdeaProjects/Sandbox/MyApp/app/src/main/mine_lib/src/jni/jniwrapper.c
  In file included from /Users/user/IdeaProjects/Sandbox/MyApp/app/src/main/mine_lib/src/jni/jniwrapper.c:2:
  /Users/user/IdeaProjects/Sandbox/MyApp/app/src/main/mine_lib/src/data/dataparser.hpp:16:10: fatal error: 'cstdint' file not found
  #include <cstdint>
           ^~~~~~~~~
  1 error generated.
  ninja: build stopped: subcommand failed.


* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED in 0s

What is the problem really is and how can I solve it?

Based on my understanding the problem could be solved by using a different C++ Library.

Currently you are specifying the following within the build.gradle file:

arguments "-DANDROID_STL=system"

Which, as stated within the following documentation , is:

system: The minimal system C++ runtime library and the default runtime

You'll probably need to change that to another C++ Library but I am not sure which one it would be, could be one of the gnu options. Better yet, it could be solved by removing the statement entirely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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