如何在C ++中构建运行时版本不可知的DLL? [英] How do I build a runtime version agnostic DLL in C++?

查看:213
本文介绍了如何在C ++中构建运行时版本不可知的DLL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的产品是一个C ++库,在Windows上,作为dll发布。它很少使用c-runtime(基本的iostream和它的),所以我敢肯定所有最新版本的CRT将是罚款。

My product is a C++ library, which, on Windows, is distributed as a dll. It makes very little use of the c-runtime (basic iostream and that's it), so I'm sure that all recent versions of the CRT will be fine.

我的客户端应该使用我的dll构建他的应用程序,我不想强​​加他任何特定的运行时版本。我想我的DLL绑定到我的客户端的应用程序使用的任何运行时库版本(我可以假设他将使用动态链接为他的CRT)。毕竟,这不是动态链接是什么?这是可能的吗?

Since my client is supposed to build his application using my dll, I don't want to impose upon him any specific runtime version. I'd like my dll to bind to whatever runtime library version my client's app is using (and I can assume that he'll use dynamic linking for his CRT). After all, isn't that what dynamic linking is all about? Is that possible?

编辑:链接dll和静态运行库不会工作,因为然后静态运行时(从DLL)和动态运行时从客户端的应用程序)将混合,这是坏的。

linking the dll against the static runtime libs won't work either, because then the static runtime (from the dll) and the dynamic runtime (from the client's application) will be mixed, which is bad.

编辑:我主要问的是如何告诉运行时加载器链接我的dll反对CRT应用程序是否链接?有什么清单,也许?
一般来说,我的问题是如何构建一个很好的dll,这是由客户端使用它们是自己的应用程序?

What I'm mainly asking is how do I tell the runtime loader to link my dll against whatever CRT the application is linked with? Something with the manifest, perhaps? More generally, my question is how to build a nicely-behaving dll, that's to be used by clients building they're own applications?

编辑:谢谢到答案中的建议,我已将所有对std类的引用转移到我的标题中的内联函数中,并将我的dll与静态运行时库链接。

Thanks to the advice in the answers, I've transferred all references to std classes into inlined functions in my headers, and linked my dll with the static runtime libraries. It now seems to work even in applications linked with different CRT versions.

推荐答案

没有什么真正的方法可以确保你的DLL与多个运行时 - 在它们之间更改的任何类型都可能导致不兼容。例如,对象的大小可以更改,或其中的成员的位置。 C ++中有很少的空间可用于这种事情。

There's no real way to ensure your DLL works with multiple runtimes -- any of the types that change between them can lead to incompatibilities. For instance, the size of an object can change, or the location of members in them. There is very little room in C++ for this kind of thing.

最好的做法是静态链接到运行时,并确保导出的API只限于类型在你的控制下 - 没有传递 std :: string 到函数,没有stdlib类型作为成员,不要在另一个DLL中的 delete 。不要为同一对象混合内联函数和导出函数(包括构造函数/析构函数),因为成员顺序和填充可能在编译器之间更改。 pimpl成语在这里可能会有帮助。

The best thing you can do is statically link to the runtime and ensure the exported API is limited to types strictly under your control -- no passing std::string to a function, no stdlib types as members, and don't new in one DLL and delete in another. Don't mix inline and exported functions (including constructors/destructors) for the same object, because member order and padding might change between compilers. The pimpl idiom might help here.

这篇关于如何在C ++中构建运行时版本不可知的DLL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆