包装一个 c++ c 中的库? (不要使用“extern c”)
是否可以将 C++ 库包装到 C 中?
我怎样才能做到这一点?
有现成的工具吗?
(需要访问现有的 C++ 库,但只能使用 C)
is it possible to wrap a c++ library into c?
how could i do this?
are there any existing tools?
(need to get access to a existing c++ library but only with C)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以用 C 语言编写面向对象的代码,因此如果它是面向对象的 C++ 库,则可以将其包装在 C 接口中。然而,这样做可能非常乏味,特别是当您需要支持继承、虚函数等东西时。
如果 C++ 库采用通用编程(模板),它可能会变得非常棘手(您需要提供模板的所有需要的实例),并且很快就会达到不值得这样做的地步。
假设它是面向对象的,下面是如何在 C 中进行面向对象的基本草图:
C++ 类:
C 接口:
根据您的要求,您可以修改它。例如,如果这将是私有 C++ API 的公共 C 接口,则将真实指针作为句柄分发可能会使其容易受到攻击。在这种情况下,您将分发本质上是整数的句柄,将指针存储在句柄到指针的映射中,并通过查找替换强制转换。
让函数返回字符串和其他动态大小的资源也可能变得相当复杂。您需要 C 调用方提供缓冲区,但它无法事先知道大小。然后,某些 API(如 WIn32 API 的一部分)允许调用者使用长度为 0 的缓冲区调用此类函数,在这种情况下,它们返回所需缓冲区的长度。然而,这样做可能会使通过 API 的调用效率极其低下。 (如果算法执行后才知道所需缓冲区的长度,则需要执行两次。)
我过去做过的一件事是向内部存储的字符串分发句柄(类似于上面代码中的句柄),并提供一个 API 来询问所需的缓冲区大小,检索提供缓冲区的字符串,并销毁句柄(删除内部存储的字符串)。
这是一个真正可以使用的 PITA,但 C 语言也是如此。
You can write object-oriented code in C, so if it's an object-oriented C++ library, it's possible to wrap it in a C interface. However, doing so can be very tedious, especially if you need to support inheritance, virtual functions and such stuff.
If the C++ library employs Generic Programming (templates), it might get really hairy (you'd need to provide all needed instances of a template) and quickly approaches the point where it's just not worth doing it.
Assuming it's OO, here's a basic sketch of how you can do OO in C:
C++ class:
C interface:
Depending on your requirements, you could amend that. For example, if this is going to be a public C interface to a private C++ API, handing out real pointers as handles might make it vulnerable. In that case you would hand out handles that are, essentially, integers, store the pointers in a handle-to-pointer map, and replace the cast by a lookup.
Having functions returning strings and other dynamically sized resources can also become quite elaborate. You would need the C caller provide the buffer, but it can't know the size before-hand. Some APIs (like parts of the WIn32 API) then allow the caller to call such a function with a buffer of the length 0, in which case they return the length of the buffer required. Doing so, however, can make calling through the API horribly inefficient. (If you only know the length of the required buffer after the algorithm executed, it needs to be executed twice.)
One thing I've done in the past is to hand out handles (similar to the handle in the above code) to internally stored strings and provide an API to ask for the required buffer size, retrieve the string providing the buffer, and destroy the handle (which deletes the internally stored string).
That's a real PITA to use, but such is C.
编写一个执行
extern c
的 C++ 包装器,用 C++ 编译它,然后调用您的包装器。Write a c++ wrapper that does an
extern c
, compile that with c++, and call your wrapper.extern C 只能帮助您在 dll 中拥有您所看到的名称。
您可以使用
dumpbin /EXPORTS your.dll
看看带有 extern C 或不带有 extern C 的名称会发生什么情况。
http://msdn.microsoft.com/en- us/library/c1h23y6c(v=vs.71).aspx
要回答您的问题...这取决于...但您不太可能在没有包装的情况下使用它。如果这个 C++ 库仅使用简单的函数和类型,您就可以使用它。如果这个 C++ 库使用复杂的类结构 - 可能您将无法在不包装的情况下从 C 使用它。这是因为类的内部可能会根据许多条件以一种或另一种方式构建(使用虚拟表或抽象进行推理。或者在示例中,复杂的 C++ 库可能有自己的对象创建机制,因此您必须以它的方式使用它是设计好的,否则你会得到不可预测的行为)。
所以,我认为,你必须为圆顶包裹做好准备。
这里有一篇关于包装 C++ 类的好文章。作者在这篇文章中介绍了如何将 C++ 类包装到 C#,但他第一步使用的是 C。
http://www.codeproject.com/KB/cs/marshalCPPclass.aspx
extern C only helps you to have a names in dll like you see them.
You can use
dumpbin /EXPORTS your.dll
to see what happens with names with extern C or without it.
http://msdn.microsoft.com/en-us/library/c1h23y6c(v=vs.71).aspx
To answer your question... It depends... But it is highly unlikely that you can use it without wrappings. If this C++ library uses just a simple functions and types you can just use it. If this C++ library uses a complex classes structure - probably you will be unable to use it from C without wrapping. It is because the internal of classes may be structured one way or another depending on many conditions (using inference with virtual tables or abstracting. Or in example complex C++ library may have its own object creation mechanisms so you HAVE to use it in the way it is designed or you will get unpredictable behavior).
So, I think, you have to prepare yourself for doing dome wrappings.
And here is a good article about wrapping C++ classes. It the article the Author tells about wrapping C++ classes to C# but he uses C at first step.
http://www.codeproject.com/KB/cs/marshalCPPclass.aspx
如果编写了可以用C编译器编译的C++库,只需稍加编辑(例如将
bool
更改为int
,将false
更改为0
和true
到1
等),那么就可以完成。但并非所有 C++ 代码都可以用 C 包装。模板是 C++ 中无法包装或几乎不可能包装的功能之一。
If the C++ library is written which can be compiled with C compiler with slight editting (such as changing
bool
toint
,false
to0
andtrue
to1
etc), then that can be done.But not all C++ code can be wrapped in C. Template is one feature in C++ that cannot be wrapped, or its nearly impossible.
将其包装在调用该 dll 的 C++ cpp 中,并在您创建的文件中使用“extern C”。
Wrap it in C++ cpp that calls that dll, and "extern C" in that file you made.