I'm on an Ubuntu x86_64 system.
I have this source file main.c that uses the fx function defined in the shared library libfx.so :
int fx( int a, int b);
int main( void ){
int x = fx( 50, 30 );
return 0;
}
Here is the definition of fx in the file fx.c used to create the shared library :
int fx( int a, int b){
return a + b;
}
Questions :
- Is it correct to say that we can modify the function definition internally as we like without causing incompatibilities ? For example we could do :
int fx( int a, int b){
return a - b;
}
or
int fx( int a, int b){
if ( a > 10 ){
return a + b;
}else{
return a - b;
}
}
Is it correct to say that incompatibilities occur if we modify any of the following :
return type
function name
numbers of paramters
type of paramaters
Do these principles also apply to single object files ? For example an object file called A that uses a function defined in B
Not entirely. It is correct that modifying the function definition will not cause any problems in passing arguments or receiving return values. However, changing the behavior of a function may change the resulting behavior of the calling function.
The questions you ask about appear to be in a specific limited context and may only be asking about compatibility of the calling interface specifically and not intended to include behavior compatibility.
Changing the return type often causes interface incompatibility. There are some limited circumstances in which it does not, such as changing to a compatible type.
Changing the function name often causes interface incompatibility. There are some limited circumstances in which it does not, such as changing the trailing characters of a name longer than the significant portion for external names. (The C standard requires at least 31 significant characters.)
Changing the number of parameters often causes interface incompatibility. There are some limited circumstances in which it does not, such as reducing the number of parameters in platforms in which the calling routine is responsible for stack management related to passing parameters. (In other words, in some platforms, the calling routine may pass extra parameters which will be ignored by the called routine.)
Changing the type of parameters often causes interface incompatibility. There are some limited circumstances in which it does not, such as changing to a compatible type.
Yes.