I have a dll whose source code is written in C++ . I would like to create the static version of that library and I would like to be able to link this static library in a another library which is written in pure C. The compiler is MinGW on Windows. How should I compile the first C++ static library so to make it usable by a C library?
相关问题
- Sorting 3 numbers without branching [closed]
- Multiple sockets for clients to connect to
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- How to know full paths to DLL's from .csproj f
You don't, you do the opposite. You make the C library work in your C++ program. C++ encompasses C, not the other way around (superset C++, subset C). So what you do, is you can write a C++ program (using C++ or C syntax), and compile this program with a c++ compiler. You can then use C and C++ libraries.
You add a C API to your C++ library. In the header file you add
You do the same in your C api C++ file "but you can leave out the
#ifdef
s because you know you will be compiling them using your C++ compiler.You C api C++ file then can access the C++ API but your library is guaranteed to export using the C style Application Binary interface (no name mangling). The library can then be used from a C application.
The header file will be good to go both in C and C++ projects
If you plan on calling member functions of an instance of C++ class you will need to provide forwarding functions. You will also need to provide at least one function to return a pointer to an instance of the class you want to access. There are to significant reasons for this. 1) Constructors are special and do not have names so they cannot be called directly and 2) member functions take an implicit
this
parameter which can be passed on the stack or in a register.For instance let's say you have a class called
SomeClass
Now, you want to add a type safe way of creating and accessing an instance of that class. You can create a C interface that provides the creation function and forwarding functions. You might start by adding an additional header file to provide the C interface for
SomeClass
Now you need to provide an implementation of the C interface. This is where all the magic happens that allows you to call C++ member functions from C.
I recommend using
reinterpret_cast
over C style casts to prevent the accidental removal ofconst
qualifiers.To access the C++ library from C you can now do something like the example below
Note: Simply putting the C++ library in a DLL and calling
GetProcAddress
for free functions and static member functions will be difficult as you will need to take into account name mangling.