2017-04-07 230 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不是項目的一部分,編譯器沒有找到它 –