1

As far as I know, C(not C++) has a stable ABI on each OS, and mainstream compilers often stand with that. Many languages may link with code written in C, and e.g. SystemV AMD64 documents at page 10 that :

No attempt has been made to specify an ABI for languages other than C. However, it is assumed that many programming languages will wish to link with code written in C, so that the ABI specifications documented here apply there too.

So I tried to link C code compiled by msvc and by gcc in Windows:

// msvcF.c, compiled by msvc.
#include "msvcF.h"
#include <stdio.h>

void test()
{
    printf("compiled version from msvc!\n");
    return;
}
// msvcF.h
void test();
// gccF.c, compiled by gcc.
#include "msvcF.h"
#include <stdio.h>

int main()
{
    printf("Hello from gcc compiled code!\n");
    test();
    return 0;
}

And the command is as below:

cl /c msvcF.c # This is in VS2019 x86-64 env, msvc 19.29.
gcc -c gccF.c # gcc 8.1.0

When I tried to link gccF.o and msvcF.obj, if using gcc's linker as :

gcc -O gccF.o msvcF.obj

It prompts: Warning: corrupt .drectve at end of def file. And when I tried to use msvc's linker as :

link gccF.o msvcF.obj # This is in VS2019 x86-64 env, msvc 19.29.

It prompts: gccF.o: error LNK2019: unresolved external symbol '__main' referenced in function 'main'.

I also tried to add -mabi=ms in the gcc command, but it doesn't work.

So I'm quite confused; does it mean C ABI is not regulated or determined by the OS or mainstream compilers don't try to obey that? I mean C++ ABI is determined by both the compiler and OS, so if C is also so, what's the point of saying the latter is compitable?

o_oTurtle
  • 1,091
  • 3
  • 12
  • 2
    the object file format has nothing to do with the ABI – phuclv Aug 18 '22 at 03:14
  • Object file is the state before linking. The ABI applies in the next step, when the code is linked and becomes machine code. In general, C programmers need not worry their pretty heads about ABI except when it comes to stating the calling convention explicitly - for example stdcall vs cdecl. – Lundin Aug 18 '22 at 11:06
  • @Lundin Sorry for my questions, but now I'm totally messed up. I've read [N4028](https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4028.pdf) by Herb Sutter saying that ABI is to let code be able to somehow link together. – o_oTurtle Aug 18 '22 at 11:59
  • 1
    Well sure, two separate linkers meeting the same ABI must generate the same code, when it comes to things like calling convention. But that isn't related to object files. – Lundin Aug 18 '22 at 12:33
  • @Lundin So you mean that OS ABI doesn't guarantee compilers to generate inter-linkable object files, but to guarantee if they are inter-linkable, the machine code can run on the OS? But then what's the merit of C ABI over C++ ABI considering both of them have to use inter-linkable object files? – o_oTurtle Aug 18 '22 at 13:34
  • Isn't the problem here simply the fact that the linker cannot link two object files from two very different compilers (MS compiler and gcc)? – Jabberwocky Aug 18 '22 at 13:39
  • ABI is mainly related to a specific compiler and ultimately the CPU. x86 PC being a special snowflake since it historically had many different kinds of calling conventions, see this: https://en.wikipedia.org/wiki/X86_calling_conventions. There's nothing called "C ABI" or "C++ ABI"... if object files from C can be linked by your C++ linker it very likely is because they are part of the same compiler tool chain. – Lundin Aug 18 '22 at 13:44
  • @Lundin I'd like to extend my gratitude for your patience to answer my stupid questions. So what does [this](https://stackoverflow.com/a/4490134/15582103) mean by saying "interoperable" in "Most C compilers on Windows use this as their default calling convention, and thus are interoperable" and so-called C/C++ ABI? – o_oTurtle Aug 18 '22 at 14:27
  • @o_oTurtle In the x86 Windows would, the stdcall and cdecl calling conventions are the common ones. That answer just says that a C or C++ compiler using cdecl might have compatibility problems calling the Windows API. This would be because most the Windows API is pre-linked in the form of Dynamic Load Libraries (DLL) and in the stdcall format. So whenever importing a DLL or creating one, you'll have to be careful about specifying the correct calling convention - most of the time stdcall. But this also means that you can call DLLs created in wildly different languages. – Lundin Aug 18 '22 at 14:43
  • Though when you type `#include ` this is already handled for you by the compiler. So in the Windows world, pretty much the only time you have to actually worry about calling convention is when creating/importing custom DLLs. – Lundin Aug 18 '22 at 14:45

0 Answers0