C ++跨平台动态库; Linux和Windows [英] C++ Cross Platform Dynamic Libraries; Linux and Windows

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

问题描述

我需要一些帮助来编写跨平台代码;不是一个应用程序,而是一个库。

I need some help on writing cross-platform code; not an application, but a library.

我创建一个静态和动态库,大部分开发在Linux中完成,我有静态和共享库生成在Linux中,但现在想要以 .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.

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

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系统,所以希望他们不应该是一个问题。

If not then can anyone advice me 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. 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)。
这些工具在Linux上原生支持,并通过 Cygwin MinGW / MSYS 。 MinGW还提供了交叉编译的选项,即在运行Linux时构建本机Windows二进制文件。我发现在导航Autotools(包括libtool)中有用的两个资源是Autobook(特别是 DLL和Libtool )和

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.

正如其他人提到的,

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

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

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