【问题标题】:How to call a function in CPP file from C File and vice versa in ANDROID NDK?如何在 ANDROID NDK 中从 C 文件调用 CPP 文件中的函数,反之亦然?
【发布时间】:2011-11-24 10:25:06
【问题描述】:

我无法从 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", 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

我遇到的错误:

    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

【问题讨论】:

    标签: android c++ c android-ndk


    【解决方案1】:

    如果你从c文件中定义的cpp文件调用函数,你可以直接使用

    extern "C" void c_func(); // definition
    
    // from a cpp file
    c_func();
    

    你也可以反过来,从 c 文件调用 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();
    .....
    

    【讨论】:

      【解决方案2】:

      extern "C" 应该在 C 源代码包含的头文件中,而不是在 C++ 源文件中。 C++源文件中包含的C头文件也是如此。

      【讨论】:

        猜你喜欢
        • 2017-09-12
        • 1970-01-01
        • 2021-04-07
        • 1970-01-01
        • 2013-03-27
        • 1970-01-01
        • 1970-01-01
        • 2018-06-24
        • 2013-10-01
        相关资源
        最近更新 更多