c++ - 为什么依赖的应用程序要使用与动态库相同的编译器?

标签 c++ qt linkage abi

最近我不得不编译 Qt,说明中清楚地提到我的应用程序应该使用与我编译 Qt 时使用的相同的编译器进行编译。 现在我不明白这是为什么,想知道这是特定于 Qt 还是通用 C++ 的东西?

最佳答案

简介

Object files and static libraries created with different compilers, or even with significantly different releases of the same compiler, often cannot be linked together. This issue is not specific to MinGW: many other compilers are mutually incompatible. Build everything from source with the same version of the same compiler if you can.

Dll's are slightly different. Sometimes you can link a DLL built with one compiler to an application compiled with another. This works well if the DLL is written in C, even if the application is written in C++. For example, MinGW C++ programs commonly link to the C runtime library provided with Windows. DLLs that are written in C++ work too, as long as you communicate with them only through a C interface declared with extern "C". If you do otherwise, you will probably get linker errors because different compilers mangle C++ names differently.

为什么不同的编译器可能无法互操作

Sometimes people wonder why compiler writers don't just use the same name-mangling scheme. That might make linking succeed, but would most likely give you a program that crashes when you call into the DLL. Real link compatibility requires a common Application Binary Interface, and name-mangling is just one consideration among many. Here's a partial list:--

如果同一系统的两个 C++ 实现使用不同的调用序列,或者以其他方式链接不兼容,则使用相同的类型签名编码是不明智的。

Implementors have traditionally used deliberately different name-mangling schemes, figuring it's better to 'just say no' at link time than to make some simple code work and let the issues emerge at run time.

Even though GNU g++ can link MSVC C++ libraries now, and can produce MSVC++ compatible libraries/DLLs, this does not mean that they will be able to work at run-time due to the dynamic nature of C++. Some possible reasons for this are:--

  • 可以使用显式 .def 文件规避的简单名称修改问题。
  • 不同的结构对齐问题需要正确的编译器选项(-mms-bitfields,...)。
  • 底层异常和内存模型的根本冲突:--

MSVC DLL 中的 new/delete 或 malloc/free 不会与 Cygwin newlib new/delete 或 malloc/free 合作。根本无法释放在使用不同的 new/malloc 的函数中分配的空间。

Cygwin 可执行文件不会捕获 MSVC DLL 引发的异常,反之亦然。

缓慢的 GNU SJLJ 异常模型(在 GCC-3.x 和更早版本中使用)与 MSVC++ 模型兼容,但新的 DWARF2 模型(将由 GCC-4.x 使用)将是不兼容。

可以找到规范来源here .

关于c++ - 为什么依赖的应用程序要使用与动态库相同的编译器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23577663/

相关文章:

grails - Grails XOM链接错误-SAXParseException

c - `char *array[size]` 和 `extern char **array` 的错误链接?

c++ - 保存AVL树中节点下的叶子数

c++ - 在没有源代码的情况下粘合 C 和 C++ 目标文件

C++ Boost - 没有找到接受类型为 'boost::filesystem::path' 的右手操作数的运算符

c++ - 从 Windows 上的 Visual Studio 2013 到 Ubuntu 交叉编译 C++ OpenCV

mysql - 使用 MySQL 问题的 Qt 静态构建

c++ - 使用 C 样式链接从函数返回类模板安全吗?

c++ - 如何在不阻塞的情况下启动Qt事件进程?

c++ - Qt:数据库连接不会打开