JNI “undefined reference” to c++ method

耗尽温柔 提交于 2020-05-17 05:48:07

问题


I'm trying to create poker app with JNI in android studio, I want to save one instance of c++ class (TexasHoldem) and invoke it's methods only. in the texasJNI.java I wrote:

public class jniTexasHoldem {
  private long texasHoldm;
  jniTexasHoldem() {
      ConstructNativeTexas();
  }


  // Used to load the 'native-lib' library on application startup.
  static {
      System.loadLibrary("native-lib");
  }

  /**
   * A native method that is implemented by the 'native-lib' native library,
   * which is packaged with this application.
   */
  private native void ConstructNativeTexas();

  public native String getCardXML();

  public native String stringFromJNI();
}

in mainActivity:

ublic class MainActivity extends AppCompatActivity {
  private jniTexasHoldem m_TexasHoldem = new jniTexasHoldem();
  ...
  String cardXml = m_TexasHoldem.getCardXML();
}

and in native-lib:

extern "C"
JNIEXPORT jstring JNICALL
Java_android_myapplication_jniTexasHoldem_stringFromJNI(
    JNIEnv* env,
    jobject /* this */) {
    std::string hello = "Hello from C++";
    return env->NewStringUTF(hello.c_str());
}

extern "C"
JNIEXPORT jlong JNICALL
Java_android_myapplication_jniTexasHoldem_ConstructNativeTexas(JNIEnv *env, jobject thiz) {
  TexasHoldm* texasHoldmObj = new TexasHoldm();

  return (jlong)texasHoldmObj;
}

extern "C"
JNIEXPORT jstring JNICALL
Java_android_myapplication_jniTexasHoldem_getCardXML(JNIEnv *env, jobject thiz) {
  jclass c = env->GetObjectClass(thiz);
  // J is the type signature for long:
  jfieldID fid_handle = env->GetFieldID(c, "texasHoldm", "J");
  TexasHoldm * nativeObject = (TexasHoldm *) env->GetLongField(thiz, fid_handle);

  return (env)->NewStringUTF(nativeObject->getCard().c_str());
}

I expect to get the string of {card}.png which auto generated from getCard method but it does not compile due to undefined reference to 'TexasHoldm::getCard()'

my build.gradle:

externalNativeBuild {
    cmake {
        path "src/main/cpp/CMakeLists.txt"
        version "3.10.2"
    }
}
ndkVersion "21.1.6352462"

and in CmakeList I wrote:

target_link_libraries( # Specifies the target library.
                   native-lib
                   TexasHoldm.h
                   TexasHoldm.cpp
                   PokerTable.h
                   PokerTable.cpp
                   Player.h
                   Player.cpp
                   Hand.h
                   Hand.cpp
                   Card.h
                   Card.cpp


                   # Links the target library to the log library
                   # included in the NDK.
                   ${log-lib} )

回答1:


there were dependencies between all file so I cleaned CMakeLists file as it was at the beginning (the basic auto-generated file with native-lib only) I added the ndkVersion "21.1.6352462" line and added all external .cpp files inside add_library part inside the CMakeLists file.

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).
             native-lib.cpp **extra .cpp files here**)

so it compiles it all in one external lib.



来源:https://stackoverflow.com/questions/61563685/jni-undefined-reference-to-c-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!