在Windows上分发(本机C ++)库 [英] Distributing (native C++) libraries on windows

查看:72
本文介绍了在Windows上分发(本机C ++)库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想与第三方共享我用本机C ++编写的库。我的意思是说,我想与一个不希望与之共享源代码的人共享一个库,该库提供了我开发的功能。我们可以在这里假设相同的CPU体系结构。

I would like to share a library I've written in native C++ with a third party. By that I mean I'd like to share a library providing functionality I've developed with someone who is using windows, whom I do not wish to share source code with. We can assume the same CPU architecture here.

我只曾经直接共享源代码,而且大多数都是在linux上共享的。因此,确保我可以提供尽可能通用的二进制文件的标准过程对我来说是一个谜。而且我发现缺少在线文档。

I've only ever shared source code directly, and mostly on linux. So the standard process of ensuring I can provide a binary that is as versatile as possible is a mystery to me. And I find documentation online to be lacking.

我目前正在共享.lib文件,但是这似乎暗示着这些只有在编译器将其链接后才能起作用

I am currently working on sharing .lib files, however it does seem to imply that these will only work if the compiler linking it is the same compiler I used to generate that lib file.

更常见的方法似乎是动态链接路径-提供一个.dll。我不是非常喜欢这样做的开销,因为似乎我的函数和类签名看起来像这样 __ declspec(dllexport)void __cdecl Function1(void); 。当我打算保持跨平台兼容性时,可以想象我的源代码越来越多地陷入了条件编译的纠结之中。此外,除非我使用运行时链接路线,否则我将不得不提供一个导入库.lib,这使我回到与以前相同的问题。

The more common method does seem to be the dynamic linking route - providing a .dll. I'm not terrifically fond of the overhead of doing so, as it seems that my function and class signatures would look something like this __declspec(dllexport) void __cdecl Function1(void);. As I am aiming to maintain cross-platform compatibility, I can imagine my source growing into a tangle of conditional compilation. Furthermore, unless I go the runtime linking route, I'll have to provide an import library .lib, which brings me back to the same problem as before.

在Windows上分发库的指南? DLL的运行时链接是唯一可行的方法吗?而且可靠吗? (会想到运行时库冲突的问题。)

Is there a guide to distributing libraries on Windows? Is runtime linking of DLLs the only realistic way? And is it dependable? (The issue of conflicting runtime libraries comes to mind).

推荐答案

导出将起作用的C ++类的最简单方法在不同的编译器和标准库实现中可靠地使用cppcomponents https://github.com/jbandela/cppcomponents

The easiest way to "export" a C++ class that will work reliably across different compiler and standard library implementations is to use cppcomponents https://github.com/jbandela/cppcomponents

该库仅是标头,因此不需要构建。它可以在Linux和Windows上运行,并且不需要任何导入库。但是,它确实需要功能强大的C ++ 11编译器(gcc 4.7.2,clang 3.2,Visual Studio 2013都可以运行)。

The library is header only, so no building is required. It works on both Linux and Windows, and does not require any import libraries. It does however, require a capable C++11 compiler (gcc 4.7.2, clang 3.2, Visual Studio 2013 all work).


  • 它支持std :: string,vector,tuple以及其他标准类型。

  • 它支持异常。

  • 它还支持std :: function像可以在不同编译器中使用的对象。

  • 还内置了对事件的支持,这些事件应该使观察者模式更易于实现。

  • It supports std::string, vector, tuple as well as other standard types.
  • It supports exceptions.
  • It also supports std::function like objects that can be used across different compilers.
  • There is also built in support for events that should make your observer pattern easier to implement.

该库是根据Boost许可发布的,因此可以免费用于商业和开源。

The library is released under the Boost License so it is free for both commercial and opensource use.

例如使用它,看看我对类似问题
https://stackoverflow.com/a/19592553/的stackoverflow答案1858436

For an example of using it, take a look at my stackoverflow answer to a similar question https://stackoverflow.com/a/19592553/1858436

将其用于您的库将允许使用Visual C ++(调试和发行版以及将来的版本)中的单个二进制文件,以及海湾合作委员会。否则,您将不得不支持调试版本,发行版本和GCC版本,并且当出现新版本时,必须为该版本进行重建。

Using this for your library would allow a single binary to be used from Visual C++ (both debug and release, and future versions), as well as GCC. Otherwise, you will be stuck having to support a debug version, a release version, and a GCC version, and when a new version comes out have to rebuild for that version.

如果您有具体的小例子,我可以为您提供帮助。

If you have a specific small example of what you want to do, I can help you with that.

这篇关于在Windows上分发(本机C ++)库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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