skip to Main Content

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


  1. 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 -On 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.

    Login or Signup to reply.
  2. Where am I going wrong?

    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 remove extern, as functions are implicitly extern. Just __attribute__((__gnu_inline__)) int myprintf, no extern no inline.

    inline has very super special meaning in C. Do not use it. You might be interested in researching it.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search