Each translation units produce a .obj file. It means if you pass 3 files to the compiler (like cl main.c a.c b.c
), it will output 3 obj file.
If you have some code that you want to be inlined, the translation units need to have access to the source code of the function to be able to have the instruction in each .obj files, it's not sufficient to only have the function prototype.
So if you want a function to be (considered to be) inlined in several translation units, in the file you include (.h or .c it doesn't matter) you need the full function body.
Also remember that the inline
keyword is only a hint to the compiler and will most likely be ignored (I didn't include any in the examples).
Possible to inline:
/* possible_to_inline.h */
int function_to_inline( int x ) {
int result = x * x;
return x;
}
/* Translation unit 1 */
#include "possible_to_inline.h"
int main( void ) {
/* ... */
int result = function_to_inline( 3 );
/* ... */
}
/* Translation unit 2 */
#include "possible_to_inline.h"
void tu_1( void ) {
function_to_inline( 3 );
}
Impossible to inline
/* impossible_to_inline.h */
int function_to_inline( int x );
/* Translation unit 1 */
#include "impossible_to_inline.h"
int main( void ) {
/* ... */
int result = function_to_inline( 3 );
/* ... */
}
/* Translation unit 2 */
#include "impossible_to_inline.h"
int function_to_inline( int x ) {
int result = x * x;
return x;
}
void tu_1( void ) {
function_to_inline( 3 );
}