如何在 android studio 中创建 c++ 类(.h 和 .cpp)?

How to create a c++ class (.h and .cpp) in android studio?

本文关键字:cpp c++ android studio 创建      更新时间:2023-10-16

我正在学习使用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\AppData\Local\Android\Sdk\cmake\3.6.3155560\bin\cmake.exe' 时出错 {--build C:\Users\lucien.moor\Desktop\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 共享库 .....\构建\中间体\cmake\debug\obj\mips64\libnative-lib.so 失败: cmd.exe/C "cd .&& C:\Users\lucien.moor\AppData\Local\Android\sdkdk-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.moor/AppData/Local/Android/sdk/ndk-bundle/platforms/android-21/arch-mips64 -fPIC -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -wa,--noexecstack -wformat -werror=format-security -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -wa,--noexecstack -Wformat -Werror=format-security -O0 -fno-limit-debug-info -O0 -fno-limit-debug-info -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -wl,-z,noexecstack -Qunused-arguments -wl,-z,relro -wl,-z,now -wl,--build-id -wl,--warn-shared-textrel -wl,--fatal-warnings -wl,--no-undefined -wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -wl,-z,now -shared -wl,-soname,libnative-lib.so -o ........\build\intermediates\cmake\debug\obj\mips64\libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -llog -lm "C:/Users/lucien.moor/AppData/Local/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/mips64/libgnustl_static.a" && cd 。 CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o: In functionJava_he_1arc_myapplication2_MainActivity_stringFromJNI': C:Userslucien.moorDesktoptmpMyApplication2appsrcmaincpp/native-lib.cpp:10: undefined reference toHelloWorld::HelloWorld()' clang++.exe:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) 忍者:构建停止:子命令失败。

  • 尝试: 使用 --stacktrace 选项运行以获取堆栈跟踪。使用 --info 或 --debug 选项运行以获取更多日志输出。

我认为链接 .h 和 .cpp 文件时出现问题。当我尝试内联实现我的构造函数时,它工作正常。它只是找不到.cpp实现。

这是我的 JNI 原生库.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());
}

这是我的Helloworld.h:

#ifndef MYAPPLICATION2_HELLOWORLD_H
#define MYAPPLICATION2_HELLOWORLD_H

class HelloWorld {
public:
HelloWorld();
};
#endif //MYAPPLICATION2_HELLOWORLD_H

这是我的《你好世界.cpp

#include "HelloWorld.h"
HelloWorld::HelloWorld() { }

当我打开这个文件时,Android Studio告诉我"这个文件不是项目的一部分。请将其包含在适当的构建文件(build.gradle,CMakeLists.txt或 Android.mk 等)中并同步项目。

那么,如何将这些可爱的.h和.cpp链接在一起呢?

我找到了解决方案!

正如消息所暗示的那样,我不得不将我的文件添加到CMakeLists.txt中。头文件不是必需的.cpp但必须在 CMakeList 中添加该文件.txt

为了让链接器找到实现文件,我不得不添加

src/main/cpp/HelloWorld.cpp in my CMakeLists.txt

以下是完整的 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} )
public:
HelloWorld();

那至少应该是

public:
HelloWorld()
{
//......
}