【问题标题】:__asm__("__isoc99_scanf") after function declaration函数声明后的 __asm__("__isoc99_scanf")
【发布时间】:2019-10-20 00:35:45
【问题描述】:

我在预处理的 C 代码中看到了以下代码。 asm 在函数声明之后做了什么?

extern int scanf (const char *__restrict __format, ...) __asm__ ("" "__isoc99_scanf");

显然,它使函数调用编译为“call __isoc99_scanf”而不是“call scanf”。这是 C/GCC 标准语法吗?

【问题讨论】:

  • 你在手册中查过这个吗?
  • 这绝对是特定于编译器的语法。 GCC uses 类似的语法。
  • @MartinRosenau:这 is 完全是 GNU C 语法,用于设置 asm / 链接的符号名称。 "" "foo" 只是毫无意义的字符串连接,可能是 C 预处理器的结果。
  • @MartinRosenau - 您提供的链接是 gcc 文档(4.4)的一个相当旧的版本。 newer version 说的差不多,但可能更清楚一点。

标签: c gcc assembly inline-assembly function-declaration


【解决方案1】:

我刚刚在stdio-common/isoc99_scanf.c 下的glibc 中找到了它的实现,如下所示。

/* Copyright (C) 1991-2019 Free Software Foundation, Inc.
   This file is part of the GNU C Library.
   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.
   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.
   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <http://www.gnu.org/licenses/>.  */
#include <stdarg.h>
#include <stdio.h>
#include <libioP.h>
/* Read formatted input from stdin according to the format string FORMAT.  */
int
__isoc99_scanf (const char *format, ...)
{
  va_list arg;
  int done;
  va_start (arg, format);
  done = __vfscanf_internal (stdin, format, arg, SCANF_ISOC99_A);
  va_end (arg);
  return done;
}

它的声明也存在于libio/stdio.h 下,如下所示。

 #if defined __USE_ISOC99 && !defined __USE_GNU \
     && (!defined __LDBL_COMPAT || !defined __REDIRECT) \
     && (defined __STRICT_ANSI__ || defined __USE_XOPEN2K)
 # ifdef __REDIRECT
 /* For strict ISO C99 or POSIX compliance disallow %as, %aS and %a[
    GNU extension which conflicts with valid %a followed by letter
    s, S or [.  */
 extern int __REDIRECT (fscanf, (FILE *__restrict __stream,
                                 __const char *__restrict __format, ...),
                        __isoc99_fscanf) __wur;
 extern int __REDIRECT (scanf, (__const char *__restrict __format, ...),
                        __isoc99_scanf) __wur;
 extern int __REDIRECT (sscanf, (__const char *__restrict __s,
                                 __const char *__restrict __format, ...),
                        __isoc99_sscanf) __THROW;
 # else
 extern int __isoc99_fscanf (FILE *__restrict __stream,
                             __const char *__restrict __format, ...) __wur;
 extern int __isoc99_scanf (__const char *__restrict __format, ...) __wur;
 extern int __isoc99_sscanf (__const char *__restrict __s,
                             __const char *__restrict __format, ...) __THROW;
 #  define fscanf __isoc99_fscanf
 #  define scanf __isoc99_scanf
 #  define sscanf __isoc99_sscanf
 # endif
 #endif

【讨论】:

    猜你喜欢
    • 2019-09-22
    • 2020-08-10
    • 2014-01-22
    • 2022-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    相关资源
    最近更新 更多