在 MinGW 与 MSVS 下的 Windos 导出/导入符号;CMake 的 WINDOWS_EXPORT_ALL_SYMBOLS 被忽略 [英] Windos export/import symbols under MinGW vs MSVS; CMake's WINDOWS_EXPORT_ALL_SYMBOLS ignored

查看:31
本文介绍了在 MinGW 与 MSVS 下的 Windos 导出/导入符号;CMake 的 WINDOWS_EXPORT_ALL_SYMBOLS 被忽略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要使用 Visual Studio 构建 C 库,请使用 CMake 命令

To build a C library with Visual Studio, the CMake command

set(WINDOWS_EXPORT_ALL_SYMBOLS ON)

使我免于在函数声明前添加 __declspec(dllexport)__declspec(dllimport);只有全局变量需要显式导入/导出符号.

saves me from adding __declspec(dllexport) or __declspec(dllimport) in front of function declarations; explicit import/export symbols are only required for global variables.

在 MinGW 下(阅读:MinGW 或其推荐的替代品 Mingw-w64)这不起作用.将应用程序(也用 MinGW 构建)链接到我的库失败,直到我在每个函数前面粘贴了导入/导出符号.而长答案 https://stackoverflow.com/a/32284832/1017348 表明相反:不需要导入/在 MinGW 下导出符号.这个答案对吗?那么如何摆脱对导入/导出符号的需求?

Under MinGW (read: either MinGW or its recommendable replacement Mingw-w64) this does not work. Linking applications (also built with MinGW) to my library failed until I had pasted import/export symbols in front of each function. Whereas the long answer https://stackoverflow.com/a/32284832/1017348 suggests the contrary: no need for import/export symbols under MinGW. Is that answer right? How then to get rid of the need for import/export symbols?

推荐答案

我刚刚遇到了同样的问题.在浏览 CMake 源代码后,对我有用的修复程序还添加了:

I just encountered the same problem. After poking through CMake source code, the fix that worked for me was to also add:

set( CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS 1 )

CMake 说:

此属性仅适用于 Windows 上的 MS 兼容工具.

This property is implemented only for MS-compatible tools on Windows.

CMake 通过在 /Modules/Platform 中的每个平台"文件中设置 CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS 来启用此功能,他们知道支持它.但是,CMake 不会将 MinGW 建模为平台".相反,您只需选择Windows Makefile"、Windows Ninja"等,然后手动设置 C/C++/Fortran 编译器以指向 MinGW gcc 编译器.理想情况下,CMake 应该识别操作系统是 Windows 并且编译器是 gcc 并为我们设置它,但现在我们可以通过自己设置来帮助它.

CMake enables this capability by setting CMAKE_SUPPORT_WINDOWS_EXPORT_ALL_SYMBOLS in each "Platform" file in <cmake install>/Modules/Platform that they know supports it. However, CMake doesn't model MinGW as a "Platform". Instead you just pick "Windows Makefile", "Windows Ninja", etc. and manually set the C/C++/Fortran compilers to point to the MinGW gcc compilers. Ideally CMake should recognize when the OS is Windows and the compiler is gcc and set this for us, but for now we can help it by setting it ourselves.

顺便说一句,CMake 使用隐藏的 cmake -E __create_def 实现了此功能. 命令.我以前想过添加自定义规则来运行该命令.尽管它以 __ 开头,但它仅供内部使用,并且可能会从一个版本更改为下一个版本.

Incidentally CMake implements this feature with a hidden cmake -E __create_def <output-def> <input-list-of-obj-files> command. I previously thought of adding a custom rule to run that command. Though as it starts with __, it's meant for internal use and might change from one release to the next.

这篇关于在 MinGW 与 MSVS 下的 Windos 导出/导入符号;CMake 的 WINDOWS_EXPORT_ALL_SYMBOLS 被忽略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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