Emb_user
Emb_user

Reputation: 249

How to call a C++ function from C code

I have a C++ class with its header CFileMapping.cpp and CFileMapping.h I want to call the c++ function CFileMapping::getInstance().writeMemory( ) in my C code.

Even when I wrapped my C++ code and added

#ifdef __cplusplus
extern "C"

in the header file to deal with the c++ code and added
extern "C" in the cpp file, I still can't call my function like this

CFileMapping::getInstance().writeMemory(). 

Could any one help me? I want to keep my c++ code and be able to call it when I want.

Upvotes: 2

Views: 199

Answers (1)

Andrew Komiagin
Andrew Komiagin

Reputation: 6556

You should create C wrapper for your C++ call:

extern "C" 
{
  void WriteMemFile() 
  {
    CFileMapping::getInstance().writeMemory( );
  }
}

// The C interface
void WriteMemFile();

IMPORTANT: The extern "C" specifies that the function uses the C naming conventions.

Upvotes: 4

Related Questions