2

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.

belwood
  • 3,320
  • 11
  • 38
  • 45
Emb_user
  • 249
  • 1
  • 2
  • 7

1 Answers1

4

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.

Andrew Komiagin
  • 6,446
  • 1
  • 13
  • 23
  • Thanks for your response. I am having an error in the .cpp file `FileMapping.cpp:(.text+0x1014): undefined reference to std::cout ` . Even if i added the using namespace std; after extern "C" – Emb_user Apr 14 '15 at 13:54
  • you need to compile it using C++ compiler, not C compiler and then you'll be able to call this library function from C code – Andrew Komiagin Apr 14 '15 at 14:00
  • @Emb_user Read carefully the post http://stackoverflow.com/questions/1041866/in-c-source-what-is-the-effect-of-extern-c – VolAnd Apr 14 '15 at 14:03
  • I fixed my problem. My program works now .Thanks a lot . – Emb_user Apr 14 '15 at 14:05