Is it possible to link object files made by one compiler with those made by another?

To be more specific, suppose both compilers are on the same platform (OS + instruction set). However, one of the object files was made from compiler dependent code. On the other hand, the code is object oriented and respects encapsulation.

I need this for some kind of framework that I create. The target platform is any system where GCC and the Java virtual machine are. Indeed, the structure will be compiled on every platform. The compiler that uses the framework user depends on it.

+7
source share
4 answers

You should be able to link them while they use the same object file format and are aimed at the same set of machine instructions. For example, let's say you have two C compilers, each of which has its own language extensions. You will compile two different files, one with compiler A, the other with compiler B. Each source file uses the language extensions of the corresponding compiler. As long as both compilers are targeted to the same platform and architecture, for example, the i386 instruction installed on Linux, you should be able to link files to one executable file.

See this list of object file formats on the wiki .

It may also interest you:

UNIX tools for learning object files

EDIT

According to this article, C ++ Standard Library ABI , there is an industry standard C ++ ABI, and you should be able to link file objects of any compiler that complies with this standard. Here you can see this standard:

Itanium C ++ ABI

This document was developed jointly by an informal industry coalition consisting of (in alphabetical order) CodeSourcery, Compaq, EDG, HP, IBM, Intel, Red Hat and SGI ...

In this document, we indicate the Binary application interface for C ++ programs, that is, object code interfaces between the C ++ user code and the embedded system and the library. This includes a memory layout for C ++ data objects, including both predefined and user-defined data types, as well as an internal compiler-generated objects, such as virtual tables. It also includes the function calling interfaces, exception handling interfaces, global naming, and various object code conventions.

As long as you target the same set of commands, the object file format, and use the standard C ++ ABI (which is the default in gcc / g ++), you should be fine, assuming, of course, that standard C ++ ABIs are actually standard and properly implemented by most modern C ++ compilers that run on Linux (which seems to be platform targeting).

EDIT 2

You should take a look at this SO post:

GCC vs. MS C ++ compiler for supporting binary binary API compatibility

It seems that Microsoft does not adhere to any standard standard (Itanium or otherwise) regarding its C ++ ABI, so if you compile gcc for Windows, this is likely to be a problem.

You probably also want to look at these two articles:

Policies / issues with binary compatibility with C ++

Some thoughts on binary compatibility

You can limit your users to compilers that support Itanium ABI, but it depends on your target audience.

+9
source

It depends on the compilers. Some of them use the same ABIs and thus generate objects that can be related to each other, some not, and objects cannot be connected. Usually - I don't know the exception - when compilers use an incompatible ABI, they also use an incompatible name, and the communication phase does not work.

In fact, it takes a lot of effort and effort to link objects created using different compilers. There was a time when often there was no possibility between two different versions of gcc.

There is much more to ABI than the name mangling:

  • precise layout of objects (including padding, vtable format and RTTI information format, ...)

  • method of exception exclusion

  • way to return results

  • method of passing parameters (in the register or not, which registers where this )

  • which store registers that are not used for the result / parameter (caller, callee, ...)

  • a way to handle patterns (e.g. static data elements)

  • standard version of the library is used

  • ...

To give an idea of ​​the complexity of ABI, here is a document that details the ABI used in Itanium. IIRC, this supplement is compared to a similar document describing C ABI. It is used (for machine-independent parts) gcc on other objects.

+7
source

Yes.

After compilation, the object file contains only well-defined object code that does not rely on the compiler, although the source file uses compiler-specific code.

Make sure you use the same object format, but you already know that the same set of commands is used, so don't worry about that.

+2
source

The simple answer is no. You can link two object files together only if they are compatible with binary data. (It is correctly stated: it may or may not be possible to link them together, but even if they contact, the resulting program will not work.) This means that they pass parameters in the same way (for all types of parameters), pass any hidden arguments in the same way (for example , this ), lay out all data structures in the same way (including things that you don't see, for example, vtable), and that all classes and objects used by two object files have the same definition.

This works for C, usually because most, if not all platforms, specify a binary API for C. It almost never works for C ++, because almost no platform sets a binary API for C ++ - one of the notable exceptions is Itanium, and this specification is either incomplete or not respected. In practice, in fact, not only you should use the same compiler, but you must compile the code with the same options: both g ++ and VC ++, for example, have two different incompatible implementations for things like std::vector and std::string , selected according to the compiler options.

0
source

All Articles