适用于 Linux 和 Windows 的 C++ 跨平台动态库 [英] C++ cross-platform dynamic libraries for Linux and Windows

查看:93
本文介绍了适用于 Linux 和 Windows 的 C++ 跨平台动态库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一些跨平台的库代码.

I am wanting to write some cross-platform library code.

我正在创建一个静态和动态库,大部分开发都是在 Linux 中完成的,我已经在 Linux 中生成了静态和共享库,但现在想要以以下形式生成静态和动态库的 Windows 版本.lib.dll 使用相同的源代码.

I am creating a library both static and dynamic with most of the development done in Linux, I have got the static and shared library generated in Linux but now wanted to generate a Windows version of a static and dynamic library in the form of .lib and .dll using the same source code.

这可能吗?我有点担心,因为我注意到使用 _dllspec 或源代码中的类似内容生成所需的 Windows .dll 文件.

Is this possible? I'm a bit worried because I noticed generating Windows .dll files required using _dllspec or something similiar in your source code.

我正在寻求最佳和最快的解决方案,以便在 Windows 上编译我的代码.我不需要在Linux下进行编译;我很高兴直接在 Windows 下进行.此外,我使用了两个外部库,它们是 Boost 和 Xerces XML,我已经在 Windows 和 Linux 系统上安装了它们,所以希望它们不会成为问题.

I am seeking the best and quickest solution to getting my code compiled on Windows. I don't need to do the compiling under Linux; I am happy to do it directly under Windows. Also I am using two external libraries which are Boost and Xerces XML which I have installed on both my Windows and Linux system so hopefully they shouldn't be a problem.

我真正想要的是拥有一个可以在 Linux 和 Windows 下编译的单一源代码副本,以生成特定于每个平台的库.我真的不在乎我是否必须编辑我的代码以支持 Windows 或 Linux,只要我有一个源代码副本即可.

What I really want is to have a single source code copy that can be compiled under both Linux and Windows to generate libraries specific to each platform. I don't really care if I have to edit my code in favour of Windows or Linux as long as I can have a single source code copy.

推荐答案

总的来说,有两个问题需要你关注:

In general, there are two issues you need to be concerned with:

  1. 要求,在 Windows 上,您的 DLL 显式导出应该对外界可见的符号(通过 __declspec(dllexport)
  2. 能够维护构建系统(理想情况下,不必维护单独的 makefile 和 Microsoft Visual C++ 项目/解决方案)

首先,您需要了解 __declspec(dllexport).在仅限 Windows 的项目中,通常这是按照我在 的回答中描述的方式实现的这个问题.您可以通过确保已定义导出符号(例如 MY_PROJECT_API)但在为 Linux 构建时扩展为空来进一步扩展此步骤.这样,您可以根据 Windows 需要将导出符号添加到代码中,而不会影响 linux 构建.

For the first, you will need to learn about __declspec(dllexport). On Windows only projects, typically this is implemented in the way I describe in my answer to this question. You can extend this a step further by making sure that your export symbol (such as MY_PROJECT_API) is defined but expands to nothing when building for Linux. This way, you can add the export symbols to your code as needed for Windows without affecting the linux build.

第二,你可以研究某种跨平台的构建系统.

For the second, you can investigate some kind of cross-platform build system.

如果您对 GNU 工具集感到满意,您可能需要研究 libtool(也许与 automake 和 autoconf 结合使用).这些工具通过 CygwinMinGW/MSYS.MinGW 还为您提供了交叉编译选项,即在运行 Linux 的同时构建您的原生 Windows 二进制文件.我发现有助于导航 Autotools(包括 libtool)的两个资源是 "Autobook" (特别是关于 DLL 和 Libtool) 和 Alexandre Duret-Lutz 的 PowerPoint 幻灯片.

If you're comfortable with the GNU toolset, you may want to investigate libtool (perhaps in conjunction with automake and autoconf). The tools are natively supported on Linux and supported on Windows through either Cygwin or MinGW/MSYS. MinGW also gives you the option of cross-compiling, that is, building your native Windows binaries while running Linux. Two resources I've found helpful in navigating the Autotools (including libtool) are the "Autobook" (specifically the section on DLLs and Libtool) and Alexandre Duret-Lutz's PowerPoint slides.

正如其他人提到的,CMake 也是一种选择,但我自己不能说.

As others have mentioned, CMake is also an option, but I can't speak for it myself.

这篇关于适用于 Linux 和 Windows 的 C++ 跨平台动态库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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