For purely educational purposes, I am trying to write examples with the "Constructing Function Calls builtins" (https://gcc.gnu.org/onlinedocs/gcc-13.2.0/gcc/Constructing-Calls.html#index-_005f_005fbuiltin_005fva_005farg_005fpack)
I have written an example that incorporates what appears in the documentation:
#include <stdio.h>
extern int myprintf(FILE *f, const char *format, ...);
extern inline __attribute__((__gnu_inline__)) int myprintf(FILE *f, const char *format, ...)
{
int r = fprintf(f, "myprintf: ");
if (r < 0)
return r;
int s = fprintf(f, format, __builtin_va_arg_pack());
if (s < 0)
return s;
return r + s;
}
int main()
{
myprintf(stdout, "ciao %d\n", 10);
return 0;
}
but I cannot build
max@jarvis:~/test$ gcc --version
gcc (Ubuntu 13.2.0-4ubuntu3) 13.2.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
max@jarvis:~/test$ gcc -std=gnu2x -Wall test1.c -o test1
/usr/bin/ld: /tmp/ccgD1BYT.o: in function `main':
test1.c:(.text+0x27): undefined reference to `myprintf'
collect2: error: ld returned 1 exit status
the gcc version is shown above
Where am I going wrong?
gcc
's default optimisation level is -O0
(none). Inlining is not active without
optimisation, so:
$ gcc -std=gnu2x -Wall test1.c -o test1
emits a regular external call to myprintf
, which has no definition.
GCC Manual: 3.11 Options That Control Optimization:
-fno-inline
Do not expand any functions inline apart from those marked with the always_inline attribute. This is the default when not optimizing.
[my emphasis]
Compile with:
$ gcc -O1 -std=gnu2x -Wall test1.c -o test1
or higher -O
n and the program will link and run as you expect.
You can compare the -O0
assembly, where there is no trace of myprintf
besides an external call,
with the -O1
assembly, where its inlined definition is apparent.
Refer further to sect. 3.11 if you wish to learn which specific inlining options are enabled at each optimisation level.