2017-04-07 232 views
1

我正在学习使用android NDK并尝试创建本机C++类(.h和.cpp)。我遵循官方教程(https://developer.android.com/studio/projects/add-native-code.html)来实现这一目标。我设法创建了一个简单的C++类,并从java调用它,没有问题。如何在android studio中创建一个C++类(.h和.cpp)?

现在我想创建自己的C++类(让我们说一个HellowWorld类),只需一个无所作为的构造函数。要做到这一点,我右键单击我的cpp文件夹,其中包含我已经工作的JNI包装。

创建我的类并创建一个默认的构造函数,并从我的JNI函数调用,但在编译过程中坠毁:

Error:FAILURE: Build failed with an exception.

  • 出了什么问题: 执行失败的任务“:应用程序: externalNativeBuildDebug”。

    Build command failed. Error while executing 'C:\Users\lucien.moor\AppData\Local\Android\Sdk\cmake\3.6.3155560\bin\cmake.exe' with arguments {--build C:\Users\lucien.moor\Desktop\tmp\MyApplication2\app.externalNativeBuild\cmake\debug\mips64 --target native-lib} [1/2] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o [2/2] Linking CXX shared library ........\build\intermediates\cmake\debug\obj\mips64\libnative-lib.so FAILED: 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.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 function Java_he_1arc_myapplication2_MainActivity_stringFromJNI': C:\Users\lucien.moor\Desktop\tmp\MyApplication2\app\src\main\cpp/native-lib.cpp:10: undefined reference to HelloWorld::HelloWorld()' clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation) ninja: build stopped: subcommand failed.

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

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

这里是我的JNI本机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()); 
} 

这里是我的Helloworld.h:

#ifndef MYAPPLICATION2_HELLOWORLD_H 
#define MYAPPLICATION2_HELLOWORLD_H 


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

,这里是我的HelloWorld.cpp

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

当我打开这个文件时,android studio告诉我“这个文件不是该项目。请包括它在适当的build文件(的build.gradle,或的CMakeLists.txt等Android.mk)和同步的项目。”

所以,我要如何连接这些可爱的.h和.cpp在一起吗?

回答

4

我找到了解决办法!

的信息提示,我不得不添加我的文件INSI de CMakeLists.txt。不是必需的头文件,但.cpp文件已在的CMakeLists.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}) 
0
public: 
HelloWorld(); 

这至少应该是

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

它的工作原理,但这不是我想要实现的,我希望实现在CPP文件中。最终目标是使用已经编写的C++类(分割成.h/.cpp),我不想修改它们。我也认为在C++中做声明和实现并不是一个好习惯 –

+0

对不起,我以为它来自你的.cpp文件。那么请保持.h不变,并将其放在.cpp文件中。 – greenapps

+0

这就是我用HelloWorld :: HelloWorld(){}所做的,但编译器说它没有找到实现。这就像HelloWorld.cpp不是项目的一部分,编译器没有找到它 –