如何添加" jni / * .c / cpp"文件到CMakeLists.txt文件?

时间:2018-03-15 11:03:34

标签: c++ c syntax android-ndk include

我创建了一个包含c ++ ndk的新项目。因此它会自动创建一个CMakeLists.txt文件和一个cpp目录。但是当我创建一个名为jni的新目录并试图在其上放置test.cAndroid.mkApplication.mk文件时,我想在同步项目时收到此错误消息:

  

此文件不属于proejct.please包括它适用于   构建文件

Android.mk:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ndktest
LOCAL_SRC_FILES := test.c

Application.mk:

APP_ABI := all

test.c:

#include <jni.h>
#include <string.h>

extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_m_ndktest_MainActivity_testJNI(JNIEnv *env, jobject instance)
{
    return env->NewStringUTF(hello.c_str());

}

JNIEXPORT jstring JNICALL
Java_com_example_m_ndktest_MainActivity_testJNI2(JNIEnv *env, jobject instance)
{
    return (*env)->NewStringUTF(env, "hellooooo");

}

如何将.mk文件包含在CMakeLists.txt

CMakeLists.txt:

# 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)

# 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.
             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 )

# 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.
                       native-lib

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

enter image description here

  

***我还有一个问题,我在哪里可以找到关于我们应该在Android-ndk中编写的STRANGE语法的教程? (我的意思是test.c   我从默认native-lib.c

复制的语法

2 个答案:

答案 0 :(得分:2)

Android Studio具有外部本机构建的概念。对于这些外部本机构建,您有two个选项:

  • NDK-构建
  • CMake的

CMake似乎更现代,所以可能更具前瞻性。我建议使用那个。

这意味着您编写了CMakeLists.txt文件(通常位于jni/目录中。)

您不再需要任何.mk文件,它们由ndk-build外部本机构建使用,而不是CMake文件。

要指明您使用的外部本机构建系统,请使用应用程序的gradle文件。

android {
    defaultConfig {
        externalNativeBuild {
            cmake {
                cppFlags "..."
                arguments "..."
            }
        }
    }
}

在CMakeLists.txt文件中,您将描述如何构建本机.so库。这包括列出构建它所需的所有.c.cpp源文件。这是通过CMake的add_library()宏完成的。

总而言之,您的新项目定义将包含gradle文件和一个(或多个)CMakeLists.txt文件,但您不再需要任何.mk文件。

答案 1 :(得分:1)

  

如果我想用像tesseract这样的ndk-build构建一个库,我不应该在项目向导中填写复选框,包括对c ++的支持吗?

复选框不是问题,您只需编辑 build.gradle 脚本,并替换向导插入cmake的{​​{1}}。