[英]creating a c wrapper for C++ library
Is wrapping a c++ library to C via opaque pointers gives a stable ABI interface ? 通过不透明指针将c ++库包装到C可以提供稳定的ABI接口吗? I am clear about ABI interface and why c++ is not having a stable one.
我很清楚ABI接口以及为什么c ++没有稳定的接口。 This has to do with name mangling and lot other stuff.
这与名称修改和许多其他东西有关。 I know C is very stable in that part.
我知道C在那部分非常稳定。 It is also easy to wrap a C library into various other languages compared to C++.
与C ++相比,将C库包装成各种其他语言也很容易。 These two are driving force to make ac API for my library.
这两个是为我的库制作ac API的驱动力。
When wrapping a C++ library to C the underlying code is still C++. 将C ++库包装到C时,底层代码仍然是C ++。 I my case it is c++ with boost shared ptr and other dependencies.
我的情况是它是带有boost共享ptr和其他依赖项的c ++。
So since the underlying code is in C++, how the ABI stability is achieved. 因此,由于底层代码是在C ++中,所以如何实现ABI稳定性。 Or in other words, there is still c++ stuff compiled in the shared library (.so, .dll etc..)
或者换句话说,仍然在共享库中编译c ++东西(.so,.dll等...)
I would like to know how it works. 我想知道它是如何工作的。 Perhaps someone can provide me with an example that very well explains this stuff.
也许有人可以为我提供一个很好地解释这些东西的例子。
Yes, you can create a stable C-Inteface for a C++ implementation. 是的,您可以为C ++实现创建稳定的C-Inteface。 Of course, the C-Interface only offers C-features.
当然,C-Interface仅提供C功能。 Still it can be handy to use C++ for the actual implementation.
使用C ++实际实现仍然很方便。 Here an example where you have a single C++ implementation with function templates and you offer for some variants a C-Interface:
这里有一个示例,其中您有一个带有函数模板的C ++实现,并且您为某些变体提供了一个C接口:
// Internal C++ implementation
template <typename T>
void
foo(T &a, const T &b)
{
// do something
}
// C Interface
extern "C" {
void
sfoo(float *a, const float *b)
{
foo(*a, *b);
}
void
dfoo(double *a, const double *b)
{
foo(*a, *b);
}
} // extern "C"
So basically the compiler will generate different implementations for T=float
and T=double
: 所以基本上编译器会为
T=float
和T=double
生成不同的实现:
sfoo
for float, dfoo
for double, ... (You also can do this using the pre-processor in C. But it is not that nice to read and maintain.) sfoo
浮法, dfoo
双,...(你也可以做到这一点使用预处理器C.但它是不是很好阅读和维护。) Yes it would be the stable C ABI of the C compiler for your platform; 是的,它将是您的平台的C编译器的稳定C ABI; if done properly, of course.
如果做得好,当然。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.