If I compiled a library that includes an undefined behavior function guaranteed to work on a certain compiler, is it portable to other compilers?
I thought that the library has already generated assembly, so, when other programs call UB function, the function assembly well-defined to a certain compiler would be executed.
What am I getting wrong here?
CodePudding user response:
Do not look to the C standard for all your answers.
The C standard does not define the behavior when object modules compiled by different compilers are linked together. The jurisdiction of the C standard is solely single C implementations whose creators choose to conform to the C standard.
Linking together different object modules is covered by an application binary interface (ABI). If you compile two functions with two compilers that both conform to the same ABI and link them with a linker that conforms to the ABI, they generally should work together. There are additional details to consider, such as how various things in the language(s) bind to corresponding things in the ABI. For example, one compiler might map long
to some 32-bit integer type in the ABI while the other compiler might map long
to some 64-bit integer type, and this would of course interfere with the functions working together unless corresponding adjustments were made.