I have a large program in C that I'd like to use certain C++ objects with such as maps. I followed this post on how to call C++ from C and my two C++ look like this.
///map.cc
#include "map.h"
void* createMap()
{
std::map<int,int> *m = new std::map<int,int>();
return static_cast<void*>(m);
}
void putInMap(int key, int value, void* opaque_map_ptr){
std::map<int,int> *m = static_cast<std::map<int,int>*>(opaque_map_ptr);
m->insert(std::pair<int,int>(key,value));
}
//map.h
#ifdef __cplusplus
#define EXTERNC extern "C"
#else
#define EXTERNC
#endif
EXTERNC void* createMap();
EXTERNC void putInMap(int key, int value, void* opaque_map_ptr);
What I'm not clear about is if I do the following call in my C program, how can I be sure my Map exists and how would I make it global? Do I just make the corresponding pointer global?
#include "map.h"
//How do I let everyone use this map
void* ptr = createMap();