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?