出于纯粹的教育目的,我尝试使用“构造函数调用内置函数”编写示例(https://gcc.gnu.org/onlinedocs/gcc-13.2.0/gcc/Constructing-Calls.html#index-_005f_005fbuiltin_005fva_005farg_005fpack)
我写了一个示例,其中包含文档中出现的内容:
#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;
}
但我无法建造
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
gcc 版本如上所示
我哪里错了?