lmo*_*oor 1 c++ android android-ndk android-studio-2.3
我正在学习使用Android NDK并尝试创建本机c ++类(.h和.cpp)。我按照官方教程(https://developer.android.com/studio/projects/add-native-code.html)来实现。我设法创建了一个简单的c ++类,并从java调用它,没问题。
现在,我想用一个不执行任何操作的构造函数来创建自己的c ++类(比如HellowWorld类)。为此,我右键单击包含我已经在使用的JNI包装器的cpp文件夹。
我创建了我的类并创建了一个默认的构造函数,并从我的JNI函数调用它,但是在编译过程中它崩溃了:
错误:失败:生成失败,出现异常。
出了什么问题:任务':app:externalNativeBuildDebug'的执行失败。
生成命令失败。使用参数{--build C:\ Users \ lucien.moor \ Desktop \时执行'C:\ Users \ lucien.moor \ AppData \ Local \ Android \ Sdk \ cmake \ 3.6.3155560 \ bin \ cmake.exe'时出错tmp \ MyApplication2 \ app.externalNativeBuild \ cmake \ debug \ mips64 --target native-lib} [1/2]构建CXX对象CMakeFiles / native-lib.dir / src / main / cpp / native-lib.cpp.o [ 2/2]链接CXX共享库........ \ build \ intermediates \ cmake \ debug \ obj \ mips64 \ libnative-lib.so失败:cmd.exe / C“ cd” && C:\ Users \ lucien.moor \ AppData \ Local \ Android \ sdk \ ndk-bundle \ toolchains \ llvm \ prebuilt \ windows-x86_64 \ bin \ clang ++。exe --target = mips64el-none-linux-android --gcc-toolchain = C: /Users/lucien.moor/AppData/Local/Android/sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/windows-x86_64 --sysroot = C:/ Users / lucien。
Java_he_1arc_myapplication2_MainActivity_stringFromJNI': C:\Users\lucien.moor\Desktop\tmp\MyApplication2\app\src\main\cpp/native-lib.cpp:10: undefined reference toHelloWorld :: HelloWorld()'clang ++。exe:错误:链接器命令失败,退出代码为1(使用-v查看调用)ninja:构建停止:子命令失败。
尝试:使用--stacktrace选项运行以获取堆栈跟踪。使用--info或--debug选项运行以获取更多日志输出。
我认为在链接.h和.cpp文件时出现问题。当我尝试内联实现构造函数时,它可以正常工作。它只是找不到.cpp实现。
这是我的JNI native-lib.cpp文件:
#include <jni.h>
#include <string>
#include "HelloWorld.h"
extern "C"
jstring
Java_he_1arc_myapplication2_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
HelloWorld t;
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
Run Code Online (Sandbox Code Playgroud)
这是我的Helloworld.h:
#ifndef MYAPPLICATION2_HELLOWORLD_H
#define MYAPPLICATION2_HELLOWORLD_H
class HelloWorld {
public:
HelloWorld();
};
#endif //MYAPPLICATION2_HELLOWORLD_H
Run Code Online (Sandbox Code Playgroud)
这是我的HelloWorld.cpp
#include "HelloWorld.h"
HelloWorld::HelloWorld() { }
Run Code Online (Sandbox Code Playgroud)
当我打开此文件时,android studio告诉我“此文件不属于项目。请将该文件包含在适当的构建文件(build.gradle,CMakeLists.txt或Android.mk等)中,然后同步该项目。”
那么,如何将这些可爱的.h和.cpp链接在一起?
我找到了解决方案!
根据消息提示,我必须将文件添加到CMakeLists.txt中。不需要头文件,但必须在CMakeLists.txt中添加.cpp文件
为了允许链接程序找到实现文件,我必须添加
src/main/cpp/HelloWorld.cpp in my CMakeLists.txt
Run Code Online (Sandbox Code Playgroud)
这是完整的CMakeLists文件:
cmake_minimum_required(VERSION 3.4.1)
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).
# Associated headers in the same location as their source
# file are automatically included.
src/main/cpp/native-lib.cpp
src/main/cpp/HelloWorld.cpp)
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 )
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
Run Code Online (Sandbox Code Playgroud)