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

我无法从c文件调用cpp文件中的函数,也无法从ndk本身的cpp文件调用c文件中的函数.

我也试过使用extern“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","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","aaaaaaaaaaaaaaaaaa");
    CCustomDebug::printErrorLog("EXAMPLE","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

我得到的错误

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$

如果有人知道如何从ANDROID-NDK中的c代码调用cpp代码,请告诉我.

问候,
SSuman185

解决方法

extern“C”应该在C源包含的头文件中,而不是在C源文件中. C源文件包含的C头文件也是如此.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...