C++ 跨平台动态库;Linux 和 Windows

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1235165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:15:56  来源:igfitidea点击:

C++ Cross Platform Dynamic Libraries; Linux and Windows

c++cross-platformshared-libraries

提问by iQ.

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

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

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 .liband .dllusing the same source code.

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

Is this possible? I'm a bit worried because I noticed generating Windows .dllfiles required using _dllspecor something similiar in your source code.

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

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.

如果没有,那么任何人都可以建议我在 Windows 上编译我的代码的最佳和最快的解决方案。我不需要在 Linux 下进行编译,我很乐意直接在 Windows 下进行编译。此外,我使用了两个外部库,它们是 boost 和 Xerces XML,我已经在 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.

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

采纳答案by Nick Meyer

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

一般来说,您需要关注两个问题:

  1. The requirement that, on Windows, your DLL explicitly exports symbols that should be visible to the outside world (via __declspec(dllexport), and
  2. Being able to maintain the build system (ideally, not having to maintain a separate makefile and Microsoft Visual C++ Project/Solution)
  1. 要求,在 Windows 上,您的 DLL 显式导出应该对外界可见的符号(通过__declspec(dllexport), 和
  2. 能够维护构建系统(理想情况下,不必维护单独的 makefile 和 Microsoft Visual C++ 项目/解决方案)

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.

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

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

其次,您可以研究某种跨平台构建系统。

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 Cygwinor 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.

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

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

正如其他人所提到的,CMake也是一种选择,但我自己也说不上来。

回答by Rob K

You can pretty easily do it with #ifdef's. On Windows _WIN32 should be defined by the compiler (even for 64 bit), so code like

你可以很容易地用#ifdef's 来做到这一点。在 Windows 上 _WIN32 应该由编译器定义(即使是 64 位),所以代码如下

#ifdef _WIN32
#  define EXPORTIT __declspec( dllexport )
#else
#  define EXPORTIT
#endif

EXPORTIT int somefunction();

should work OK for you.

应该适合你。

回答by Cherif

Maybe it's better if you Add extern "C" !!!,

如果添加 extern "C" 可能会更好!!!,

/* file CMakeLists.txt */

/* 文件 CMakeLists.txt */

 SET (LIB_TYPE SHARED)
 ADD_LIBRARY(MyLibrary ${LIB_TYPE} MyLibrary.h)

/* file MyLibrary.h */

/* 文件 MyLibrary.h */

#if defined(_WIN32) || defined(__WIN32__)
#  if defined(MyLibrary_EXPORTS) // add by CMake 
#    define  MYLIB_EXPORT extern "C" __declspec(dllexport)
#  else
#    define  MYLIB_EXPORT extern "C" __declspec(dllimport)
#  endif // MyLibrary_EXPORTS
#elif defined(linux) || defined(__linux)
# define MYLIB_EXPORT
#endif

MYLIB_EXPORT inline int Function(int a) {
    return a;
}