0

I have created a C++ API that currently will only work on a particular version of Visual Studio (2008). Basically, I have a C++ DLL (and other libraries that this DLL depends on) all compiled with Visual Studio 2008. This will be a big problem for our users since they will be running newer versions of Visual Studio. I need to figure out a way to create a platform independent C++ API. I heard that one way to do this is to wrap all of the interface methods in C. Is this the right way to address this problem? How would I go about wrapping the C++ code in C such that the users are not bound to a particular version of Visual Studio on their end (and also be able to support a Linux environment as well)?

Andrew
  • 1,581
  • 3
  • 18
  • 31

2 Answers2

1

Since the abi can change between the different version of visual studio, you will need to flatten your class. As you said you wrap your class in a C Interface then you will be able to use any compiler you want. To do so you can use extern "C". It will enforce the use of C Linkage for your methods.

Quick example of extern "C"

extern "C"
{
    void myExternCFunction(int* outParam);
}

//Later in the code
void myExternCFunction(int* outParam)
{
    *outParam = 42;
}

Heres a stackoverflow post about extern "C"
IsoCpp link about extern "C"

If you don't know what the abi is well, long story short, it describe how parameters will be sent to the method, how the method will be called...

Here's an article about abi

Community
  • 1
  • 1
Pumkko
  • 1,523
  • 15
  • 19
0

The DLL itself should only export C-style functions, not C++ classes. Then any compiler can use the DLL. If you want to also provide a C++ API, create a .h header file that defines and implements the C++ class(es) you want, and have the class(es) internally use the DLL functions as needed. Then any C++ compiler can use the C++ API (as long as you stay away from any compiler-specific extensions, unless you #ifdef them).

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770