K
Koen
Hi!
I have a question about building and then using libraries containing
C++ code.
Let's say I have some C++ code and a .cpp file with 1 function that
uses some other C++ code / classes etc... Also, any possible exception
is handled within the function itself.
In code:
MyModule.h
----------
#ifndef MYMODULE_H
#define MYMODULE_H
extern int Test(float inParam1,float inParam2,float* outResult);
#endif // #ifndef MYMODULE_H
MyModule.cpp
------------
#include "MyModule.h"
#include "MyClasses.h" // contains MyClassA and MyClassB
int Test(float inParam1,float inParam2,float* outResult)
{
int theResult = 0;
try
{
MyClassA a;
a.Setup(inParam1);
MyClassB b;
b.Setup(inParam2);
*outResult = a.Process(b);
}
catch (...)
{
theResult = -1;
}
return theResult;
}
Now, I would like to build a library that is callable from C++ AND
from C that exposes the functionality of that Test function (I just
gave an example with 1 single function, but in practice there are
more).
Currently, when I build the library, it is compiled using the C++
compiler (of course, since I really use C++ classes and so on), and I
can use the library from a C++ program (.cpp file with main), as
should...
But I can't seem to find out how to make that function accessible to a
C program (.c file with main). I get an error like "unresolved
external symbol _Test", and I know that it probably has something to
do with the name mangling in C++ being different from C...
Considering the fact that the *interface* of the library does not
contain any C++ specific things, and handles all possible exceptions
internally, it should be possible to use it in C too, right?
Can someone please explain me how to do that?
Thanks in advance!
Koen
PS
If someone wants the test code, I can post/send them on request.
I have a question about building and then using libraries containing
C++ code.
Let's say I have some C++ code and a .cpp file with 1 function that
uses some other C++ code / classes etc... Also, any possible exception
is handled within the function itself.
In code:
MyModule.h
----------
#ifndef MYMODULE_H
#define MYMODULE_H
extern int Test(float inParam1,float inParam2,float* outResult);
#endif // #ifndef MYMODULE_H
MyModule.cpp
------------
#include "MyModule.h"
#include "MyClasses.h" // contains MyClassA and MyClassB
int Test(float inParam1,float inParam2,float* outResult)
{
int theResult = 0;
try
{
MyClassA a;
a.Setup(inParam1);
MyClassB b;
b.Setup(inParam2);
*outResult = a.Process(b);
}
catch (...)
{
theResult = -1;
}
return theResult;
}
Now, I would like to build a library that is callable from C++ AND
from C that exposes the functionality of that Test function (I just
gave an example with 1 single function, but in practice there are
more).
Currently, when I build the library, it is compiled using the C++
compiler (of course, since I really use C++ classes and so on), and I
can use the library from a C++ program (.cpp file with main), as
should...
But I can't seem to find out how to make that function accessible to a
C program (.c file with main). I get an error like "unresolved
external symbol _Test", and I know that it probably has something to
do with the name mangling in C++ being different from C...
Considering the fact that the *interface* of the library does not
contain any C++ specific things, and handles all possible exceptions
internally, it should be possible to use it in C too, right?
Can someone please explain me how to do that?
Thanks in advance!
Koen
PS
If someone wants the test code, I can post/send them on request.