user2284140
user2284140

Reputation: 197

Calling C++ function from a C file in Android

I am doing development in Android using C/C++. I have a C++ function, that needs to be called from a C file. Here is what I am doing right now, but I get the error

undefined reference to __check_expiry

The C++ function is defined in "a.h" and implemented in "a.cpp". I am including "a.h" in"b.c" file and calling the method "__check_expiry" from "b.c" file.

a.h :

#ifdef __cplusplus
extern "C" {
#endif

static int __check_expiry(void);

#ifdef __cplusplus
} 
#endif

a.cpp:

extern "C" {

static int __check_expiry() {

    vz::lock_guard<std::mutex> guard(mutex_check, lock_hooker_check);

    JNIEnv *env = NULL;
    static bool __is_attached_1 = false;

    if ((env = __getEnv(&__is_attached_1)) == NULL) {
        //log_info("getEnv fail\r\n");
    }
    assert(!__is_attached_1);

    int obj = env->CallStaticIntMethod((jclass) g_class, g_methodID);
    log_info("Returned value from JAVA %d", obj);

    __releaseEnv(__is_attached_1);

    guard.~lock_guard();

    return obj;
}

}

b.c:

#ifdef __cplusplus
extern "C" {
#endif

#include "a.h"

#ifdef __cplusplus
}
#endif

static int tunnel_to()
{
    int value = __check_expiry();
}

Upvotes: 0

Views: 593

Answers (1)

Dan Albert
Dan Albert

Reputation: 10509

Static functions are not exported. Remove the static qualifier.

(Unrelated: as long as you #include "a.h" from a.cpp, you don't need the extern "C" in your source file.)

Upvotes: 2

Related Questions