2011-11-24 14 views
6

no soy capaz de llamar a una función en el archivo cpp de archivo c y también una función en el archivo c desde un archivo CPP en el propio NDK.Cómo llamar a una función en un archivo CPP desde un archivo C y viceversa en ANDROID NDK?

He intentado utilizar extern "C" {} también.

pega el código he intentado aquí por referencia.

CFileCallingCpp.c:

#include "CFileCallingCpp.h" 
//#include "custom_debug.h" 
#include "CppFile.h" 





void tempFunc() { 

} 

void printTheLogs() { 
    //Its not possible to make use of the CPP class in c file 
// CCustomDebug cls; 
// cls.printErrorLog("This is the error log %d %s", 54321, "aaaaaaaaaaaaaaaaaa"); 
// cls.printErrorLog("EXAMPLE", "This is the error log %d %s", 54321, "aaaaaaaaaaaaaaaaaa"); 
    printTheLogs1(); 
// tempFunc(); 
} 

CFileCallingCpp.h:

#ifndef _CFILECALLINGCPP_H_ 
#define _CFILECALLINGCPP_H_ 

void printTheLogs(); 


#endif 

CppFile.cpp:

#include "CppFile.h" 
#include "custom_debug.h" 
#include "CFileCallingCpp.h" 

void printTheLogs1() { 
    CCustomDebug::printErrorLog("This is the error log %d %s", 54321, "aaaaaaaaaaaaaaaaaa"); 
    CCustomDebug::printErrorLog("EXAMPLE", "This is the error log %d %s", 54321, "aaaaaaaaaaaaaaaaaa"); 
} 

#if defined(__cplusplus) 
extern "C" { 
#endif 

void callCFileFunc() { 
    printTheLogs(); 
// printTheLogs1(); 
} 


#if defined(__cplusplus) 
} 
#endif 

CppFile.h:

#ifndef _CPPFILE_H_ 
#define _CPPFILE_H_ 

void printTheLogs1(); 


#endif 

errores que estoy recibiendo:

sh-4.1$ /cygdrive/c/Android/android-ndk/ndk-build 
    SharedLibrary : libJNIExInterface.so 
    D:/EclipseWorkspace/NativeExample/obj/local/armeabi/objs-debug/JNIExInterface/CppFile.o: In function `callCFileFunc': 
    D:/EclipseWorkspace/NativeExample/jni/CppFile.cpp:15: undefined reference to `printTheLogs()' 
    D:/EclipseWorkspace/NativeExample/obj/local/armeabi/objs-debug/JNIExInterface/CFileCallingCpp.o: In function `printTheLogs': 
    D:/EclipseWorkspace/NativeExample/jni/CFileCallingCpp.c:18: undefined reference to `printTheLogs1' 
    collect2: ld returned 1 exit status 
    make: *** [/cygdrive/d/EclipseWorkspace/NativeExample/obj/local/armeabi/libJNIExInterface.so] Error 1 
    sh-4.1$ 

Por favor, hágamelo saber si alguien sabe cómo llamar a un código de CPP a partir del código C en ANDROID-NDK.

Saludos,
SSuman185

Respuesta

2

El extern "C" deben estar en el archivo de cabecera incluida por la fuente de C, no en el archivo fuente en C++. Lo mismo para el archivo de cabecera C incluido en el archivo fuente de C++.

+0

Gracias, está funcionando bien :) – Suman

8

Si se llama a una función de un archivo cpp que se define en el archivo de corriente alterna, que sólo puede utilizar

extern "C" void c_func(); // definition 

// from a cpp file 
c_func(); 

Puede hacerlo al revés, llamar a una función imlpemented en un fichero cpp de archivo ac

// implemnatation in cpp file 
extern "C" void cpp_func() 
{ 
    // c++ code allowed here 
} 

// declaration in .h file 
#ifdef _CPLUSPLUS 
extern "C" void cpp_func(); 
#else 
extern void cpp_func(); 
#endif 

// from the c file 
.... 
cpp_func(); 
..... 
Cuestiones relacionadas