简体   繁体   English

交叉编译库通信

[英]Cross-compiler library communication

I need to develop a C++ front-end GUI using MSVC that needs to communicate with the bank-end library that is compiled with C++ Builder. 我需要使用MSVC开发一个C ++前端GUI,它需要与使用C ++ Builder编译的bank-end库进行通信。

How can we define our interfaces so that we don't run into CRT library problems? 我们如何定义接口以便不会遇到CRT库问题?

For example, I believe we will be unable to safely pass STL containers back and forth. 例如,我相信我们将无法安全地来回传递STL容器。 Is that true? 真的吗?

I know I can pass POD types safely, but I am hoping that I can use some more sophisticated data structures as well. 我知道我可以安全地传递POD类型,但我希望我也可以使用一些更复杂的数据结构。

You might find this article interesting Binary-compatible C++ Interfaces . 您可能会发现本文有趣的二进制兼容C ++接口 The lesson in general is, never pass STL container, boost or anything of the like. 一般的教训是,永远不要通过STL容器,提升或任何类似的东西。 Like the two other answers your best bet is to stick with PODs and functions with a calling convention specified. 与其他两个答案一样,您最好的选择是坚持使用指定的调用约定的POD和函数。

Since implementations of the STL vary from compiler to compiler, it is not safe to pass STL classes. 由于STL的实现因编译器而异,因此传递STL类是不安全的。 You can then either require the user to a specific implementation of the STL (and probably a specific version as well), or simply not use the STL between libraries. 然后,您可以要求用户使用STL的特定实现(也可能是特定版本),或者只是不使用库之间的STL。

Further more stick with the calling conventions where the behaviour can be considered cross compiler frieindly. 进一步坚持使用调用约定,其中行为可以被认为是交叉编译器。 For instance __cdecl and __stdcall will be handled equally on most compilers, while the __fastcall calling convention will be a problem, especially if you wish to use the code in C++ Builder. 例如, __cdecl__stdcall将在大多数编译器上同等处理,而__fastcall调用约定将是一个问题,特别是如果您希望在C ++ Builder中使用代码。

As the article " Binary-compatible C++ Interface " mentions you can use interface as well, as long as you remember a few basic principles. 正如文章“ 二进制兼容的C ++接口 ”所提到的,你也可以使用接口,只要你记住一些基本原则。

  1. Always make interfaces pure virtual classes (that is no implementations). 始终使接口成为纯虚拟类(这不是实现)。
  2. Make sure to use a proper calling convention for the member functions in the interface (the article mentions __stdcall for Windows. 确保对接口中的成员函数使用正确的调用约定(文章提到了Windows的__stdcall
  3. Keep the memory clean up at the same side of the DLL boundary. 保持内存清理在DLL边界的同一侧。
  4. And quite a few other things, like don't use exceptions, don't overload functions in the interface (compilers treat this differently), etc. Find them all at the bottom of the article. 还有很多其他的东西,比如不使用异常,不要在界面中重载函数(编译器对待它的方式不同)等等。在文章的底部找到它们。

You might want to read more about the Component Object Model (COM) if you choose to go with the C++ interfaces, to get an idea about how and why this will be able to work across compilers. 如果您选择使用C ++接口,您可能想要阅读有关组件对象模型(COM)的更多信息,以了解这将如何以及为何能够跨编译器工作。

You should be able to pass data that you can safely pass via a C interface, in other words, PODs. 您应该能够传递可以通过C接口安全传递的数据,换句话说就是POD。 Everything over and above PODs that is being passed by regular C or C++ function calls will run into issues with differing object layouts and different implementations of the runtime libraries. 通过常规C或C ++函数调用传递的POD之上的所有内容都将遇到不同对象布局和运行时库的不同实现的问题。

You probably will be able to pass structs of PODs if you are very careful about how you lay them out in memory and ensure that both compilers are using the same data packing etc. Over and above C structs, you pretty much have a creek/paddle problem. 如果你非常小心如何将它们放在内存中并确保两个编译器都使用相同的数据打包等,你可能会传递POD的结构。除了C结构之外,你几乎有一个小溪/桨问题。

For passing more sophisticated data types, I would look into object component technologies like COM, CORBA or other technologies that allow you to make remote or cross-process function calls. 为了传递更复杂的数据类型,我将研究对象组件技术,如COM,CORBA或其他允许您进行远程或跨进程函数调用的技术。 These would solve the problem of marshalling the data between compilers and processes and thus solve your 'pod-only' problem. 这些将解决编译器和进程之间的数据编组问题,从而解决您的“仅限pod”问题。

Or you could write the front end using C++-Builder and save yourself a lot of grief and headaches. 或者你可以使用C ++ - Builder编写前端,并为自己省去很多悲伤和头痛。

I ran into problems when passing STL-Containers even when using the same STL-Implementation, but having set different levels of debug information etc. Therefore Passing PODs will be OK. 我在传递STL-Containers时遇到问题,即使使用相同的STL-Implementation,但设置了不同级别的调试信息等。因此,传递POD将是正常的。 C++ Containers will almost certainly result in problems. C ++容器几乎肯定会导致问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM