Interoperability of libraries created by different compiler brands

Source: Internet
Author: User
Http://www.mingw.org/wiki/MixingCompilersPosted July 22nd, 2009 by Keith
  • Gcc
  • Interoperability
  • Libraries
  • Mixing Compilers
  • Msvc
Introduction

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:
Extends 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
DeclaredExtern "C"
. If you do otherwise, you will probably get linker errors because different compilers mangle C ++ names differently.

Why different compilers may not interoperate

Sometimes people wonder why compiler writers don't just use the same
Name-mangling scheme. That might make linking succeed, but wocould 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 province. Here's a partial
List :--

  • Http://ou800doc.caldera.com/SDK_porting/binary_cplusplus_compat.html
  • One compiler offers 3200 different Abis according to this page: http://www.boost.org/libs/config/config.htm#source
  • According to stroustrup (ARM, 7.2.1c, page 122 ):
If two C ++ implementations for the same system use different calling sequences,
Or in other ways are not link compatible, it wocould be unwise to use identical encodings
Of type signatures.

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
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 :--

  • The simple name mangling issue which it may be possible to circumvent with an explicit. Def file.
  • Different Structure alignment issues which need the Correct Compiler Options (-MMS-bitfields ,...).
  • A fundamental conflict of underlying exception and memory models :--
    • A new/delete or malloc/free in a msvc dll will not co-operate
      A cygwin newlib new/delete or malloc/free. One cannot free space which
      Was allocated in a function using a different new/malloc at all.
    • An exception raised by an msvc dll will not be caught by a cygwin executable, and vice versa.
    • The slow GNU sjlj exception model, (used in GCC-3.x and earlier ),
      Is compatible with the msvc ++ model, but the new dwarf2 model, (which
      Will be used by GCC-4.x), will be incompatible.
Circumventing the issues

The article at http://aegisknight.org/cppinterface.html
Describes an advanced technique which may help you to work around
Name-mangling and linking problems; Be sure to follow its guidelines
Carefully, if you try it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.