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 %dn", 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?
2
Answers
gcc
‘s default optimisation level is-O0
(none). Inlining is not active withoutoptimisation, so:
emits a regular external call to
myprintf
, which has no definition.GCC Manual: 3.11 Options That Control Optimization:
[my emphasis]
Compile with:
or higher
-O
n and the program will link and run as you expect.Refer further to sect. 3.11 if you wish to learn which specific inlining options
are enabled at each optimisation level.
Your function is
extern inline
, but you did not provide an external definition for your function. Your code is invalid.Just remove
inline
. You can also removeextern
, as functions are implicitlyextern
. Just__attribute__((__gnu_inline__)) int myprintf
, noextern
noinline
.inline
has very super special meaning in C. Do not use it. You might be interested in researching it.