简体   繁体   English

如何在CRO文件中调用CPP文件中的函数,反之亦然在ANDROID NDK中?

[英]How to call a function in CPP file from C File and vice versa in ANDROID NDK?

I am not able to call a function in cpp file from c file and also a function in c file from a cpp file in the ndk itself. 我无法从c文件调用cpp文件中的函数,也无法从ndk本身的cpp文件调用c文件中的函数。

I tried using extern "C" {} as well. 我也试过使用extern“C”{}。

Pasting the code i tried here for reference. 粘贴我在这里尝试的代码供参考。

CFileCallingCpp.c: 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: CFileCallingCpp.h:

#ifndef _CFILECALLINGCPP_H_
#define _CFILECALLINGCPP_H_

void printTheLogs();


#endif

CppFile.cpp: 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: CppFile.h:

#ifndef _CPPFILE_H_
#define _CPPFILE_H_

void printTheLogs1();


#endif

Errors i am getting: 我得到的错误:

    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$

Please let me know if anyone knows how to call a cpp code from the c code in ANDROID-NDK. 如果有人知道如何从ANDROID-NDK中的c代码调用cpp代码,请告诉我。

Regards, 问候,
SSuman185 SSuman185

If you call a function from a cpp file that is defined in ac file, you can just use 如果从ac文件中定义的cpp文件调用函数,则可以使用

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

// from a cpp file
c_func();

You can do the other way around, call a function imlpemented in a cpp file from ac file 你可以反过来,从ac文件中调用一个cpp文件中的函数

// 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();
.....

The extern "C" should be in the header file included by the C source, not in the C++ source file. extern "C"应该在C源包含的头文件中,而不是在C ++源文件中。 The same for the C header file included by the C++ source file. C ++源文件包含的C头文件也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM