【发布时间】:2018-03-15 11:03:33
【问题描述】:
我创建了一个包含 c++ ndk 的新项目。所以它自动创建了一个CMakeLists.txt 文件和一个cpp 目录。但是当我创建了一个名为jni 的新目录并尝试将test.c、Android.mk 和Application.mk 文件放在上面时,当我想同步我的项目时收到此错误消息:
这个文件不是项目的一部分。请把它包括在适当的地方 构建文件
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ndktest
LOCAL_SRC_FILES := test.c
应用程序.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");
}
我应该如何在CMakeLists.txt 中包含我的.mk 文件?
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} )
***我还有一个问题,在哪里可以找到关于我们应该在 Android-ndk 中编写的 STRANGE 语法的教程? (我的意思是test.c 我从默认
native-lib.c复制的语法)
【问题讨论】:
-
“我在哪里可以找到关于我们应该在 Android-ndk 中编写的 STRANGE 语法的教程?” 你指的是哪种语法?函数命名约定?这是in the official documentation描述的。
-
“我应该如何在 CMakeLists.txt 中包含我的 .mk 文件?” 你没有。您选择 one 的 CMake 或 ndk-build 并使用它;不是他们两个。
-
@Michael:你说如果我想用 ndk-build 构建一个库,比如
tesseract,我应该在项目向导中填写复选框including support for c++?
标签: c++ c syntax android-ndk include