Hey everyone,
I'm trying to compile a C++ library (VRPN) with a couple of Java wrappers to be used in an Android app. Using the ndk-build command, I get an "undefined reference" error for the first line of my C++ interface file which references a constructor for an object in the library. I am fairly sure my code is correct - the call matches the method header and I have both the header and source files to verify that it is implemented. I think the problem is with my Android.mk file, which currently only references my single class. So the code appears to compile but won't link once it fails to find object code for the rest of the library. I'm pretty new to C++, so I don't know a lot about makefiles, but none of the "standard" makefiles I've seen match up to those used by the Android NDK, which apparently hide a lot of details. If I understand correctly, in a "standard" makefile, I simply name the output file and which object files it needs, followed by the object files and their source and header files, etc etc. I don't know how to force ndk-build to create object code for and link the rest of the library.
Here's my current Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := jni_vrpn_button
LOCAL_SRC_FILES := jni_vrpn_button.cpp
include $(BUILD_SHARED_LIBRARY)
jni_vrpn_button.h:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class jni_VrpnButton */
#ifndef _Included_jni_VrpnButton
#define _Included_jni_VrpnButton
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: jni_VrpnButton
* Method: vrpn_Button_initialize
* Signature: (Ljava/lang/String;J)J
*/
JNIEXPORT jlong JNICALL Java_jni_VrpnButton_vrpn_1Button_1initialize
(JNIEnv *, jobject, jstring, jlong);
/*
* Class: jni_VrpnButton
* Method: vrpn_button_mainloop
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_jni_VrpnButton_vrpn_1button_1mainloop
(JNIEnv *, jobject, jlong);
#ifdef __cplusplus
}
#endif
#endif
jni_vrpn_button.cpp:
#include <unistd.h>
#include <sys/types.h>
#include <sys/time.h>
#include <jni_vrpn_button.h>
#include <vrpn_Button.h>
#include <vrpn_Connection.h>
/*
* Class: jni_VrpnButton
* Method: vrpn_Button_initialize
* Signature: (Ljava/lang/String;J)J
*/
JNIEXPORT jlong JNICALL Java_jni_VrpnButton_vrpn_1Button_1initialize
(JNIEnv * env, jobject, jstring n, jlong conn)
{
const char* name = strdup(env->GetStringUTFChars(n, 0));
vrpn_Button_Example_Server * serv = new vrpn_Button_Example_Server(name, (vrpn_Connection *) conn, 1, 1.0);
return (long) serv;
}
/*
* Class: jni_VrpnButton
* Method: vrpn_button_mainloop
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_jni_VrpnButton_vrpn_1button_1mainloop
(JNIEnv *, jobject, jlong ptr)
{
((vrpn_Button_Example_Server *) ptr)->mainloop();
}
Ideas?
Edit: forgot to post the error:
$ $CRYSTAX/ndk-build SharedLibrary : libjni_vrpn_button.so /cygdrive/c/Development/android-ndk-r4-windows-crystax-4/android-ndk-r4-crystax/sources/string/obj/l ocal/armeabi/objs/jni_vrpn_button/jni_vrpn_button.o: In function
Java_jni_VrpnButton_vrpn_1Button_1 initialize': /cygdrive/c/Development/android-ndk-r4-windows-crystax-4/android-ndk-r4-crystax/sources/string/jni/j ni_vrpn_button.cpp:18: undefined reference to
vrpn_Button_Example_Server::vrpn_Button_Example_Serve r(char const*, vrpn_Connection*, int, double)' collect2: ld returned 1 exit status make: * [/cygdrive/c/Development/android-ndk-r4-windows-crystax-4/android-ndk-r4-crystax/sources/s tring/obj/local/armeabi/libjni_vrpn_button.so] Error 1