简体   繁体   English

将STL库与Gradle CMake项目链接

[英]Link STL library with Gradle CMake project

I have simple Gradle project and trying to add native library building it using CMake 我有一个简单的Gradle项目,并尝试使用CMake添加本机库

Root CMakeLists.txt 根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 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 . 在本机库中,我使用STL库的一部分,例如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. 根据我的理解,可以使用其他C ++库解决该问题。

Currently you are specifying the following within the build.gradle file: 当前,您正在build.gradle文件中指定以下内容:

arguments "-DANDROID_STL=system"

Which, as stated within the following documentation , is: 如以下文档所述 ,它是:

system: The minimal system C++ runtime library and the default runtime 系统:最小的系统C ++运行时库和默认运行时

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. 您可能需要将其更改为另一个C ++库,但是我不确定它是哪一个,可能是gnu选项之一。 Better yet, it could be solved by removing the statement entirely. 更好的是,可以通过完全删除该语句来解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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