2016-09-23 15 views
2

私のアンドロイドプロジェクトにC++ファイルをインポートします。まず、アンドロイドスタジオでC++サポートのプロジェクトを作成しました。スケルトンのプロジェクト構造を提供します。 add_libraryメソッドを使ってCMakeLists.textファイルからファイルをロードしました。私は正常に私のC + +ファイルを読み込むことができ、プロジェクトビューで見ることができます。私は私のネイティブlib.cpp内mClient.cppからメソッドを呼び出すしようとしていたとき、私は「ハロー()」Android NDKプロジェクトで定義されていない参照エラー

エラーに

未定義の参照を得ました。 IDEの警告が表示されないため、ファイルが正常に読み込まれたと言えます。コンパイル時には機能しません。私もcmake経由でadd_executableでロードしようとしましたが、同じエラーが発生します。私の設定と設定に何が欠けているのか分かりますか?私はNDKとCMakeには新しいです。 ありがとうございました。

mClinet.cpp

#include "mClient.h" 

static int hello(){ 
    return 1; 
} 

mClient.h

#ifdef __cplusplus 
extern "C" { 
#endif 

static int hello(); 

#ifdef __cplusplus 
} 
#endif 
#endif 

ここに私のCMake.textファイルです。私の母国-lib.cppから

私は(ハロー呼び出したい
# Sets the minimum version of CMake required to build the native 
# library. You should either keep the default value or only pass a 
# value of 3.4.0 or lower. 

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 it for you. 
# Gradle automatically packages shared libraries with your APK. 

# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -UNDEBUG") 

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/mClient.cpp) 

# Searches for a specified prebuilt library and stores the path as a 
# variable. Because system libraries are included 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 the 
# build script, prebuilt third-party libraries, or system libraries. 

target_link_libraries(native-lib android log) 

#include <jni.h> 
#include <string> 
#include "mClient.h" 

extern "C" 
jstring 
Java_com_zawmyohtet_hellondk_MainActivity_stringFromJNI(
     JNIEnv* env, 
     jobject /* this */) { 

    int myInt = hello(); 

    std::string hello = "Hello from C++"; 
    return env->NewStringUTF(hello.c_str()); 
} 
+1

@Eichhörnchenextern "C"が名前のマングリングのために存在しなかった場合、JNIコールは機能しませんでした。これは完全に合法です。 –

答えて

0

staticを削除します。 staticは、その翻訳単位の外で機能にアクセスできないことを意味します。

+0

ありがとう、それは動作します。 –

関連する問題