【问题标题】:Where can I find source of string.cpp我在哪里可以找到 string.cpp 的来源
【发布时间】:2013-06-20 05:16:14
【问题描述】:

我想访问string.h 的源文件。我的意思是在string.h 中定义了所有可用函数的文件。

例如strcpy()string.h中的一个函数;我在哪里可以得到它的定义,string.h 只给出函数的原型?

【问题讨论】:

  • 这是链接实现的一个要点。不过,编译器的实现可能是开源的。我不确定标准库如何适应它。
  • 尝试查看gcc来源。

标签: c++ header-files user-defined-functions


【解决方案1】:

您没有指定开发人员工具 - 此答案适用于 Windows 上的 Visual Studio 2008。 CRT 源可以在以下位置找到:

C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\crt\src\

如果它安装在默认位置。对于其他版本的 Visual Studio,只需将 9.0 部分替换为 10.0 (VS 2010) 或 11.0 (VS 2012)。

您不会找到单个 string.c 文件 - 几个函数在它们自己的 .c 文件中实现(其中一些是在汇编中实现的)。

源位置和可用性因工具/操作系统而异。

【讨论】:

    【解决方案2】:

    C 标准库的源代码实现取决于您使用的环境和编译器。如果你在 Linux 上编程,你可能会使用 glibc,它是开源的,可以免费下载here

    顺便说一下,这里是它的 strcpy 实现:

    /* Copyright (C) 1991, 1997, 2000, 2003 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 <stddef.h>
    #include <string.h>
    #include <memcopy.h>
    #include <bp-checks.h>
    
    #undef strcpy
    
    /* Copy SRC to DEST.  */
    char *
    strcpy (dest, src)
         char *dest;
         const char *src;
    {
      char c;
      char *__unbounded s = (char *__unbounded) CHECK_BOUNDS_LOW (src);
      const ptrdiff_t off = CHECK_BOUNDS_LOW (dest) - s - 1;
      size_t n;
    
      do
        {
          c = *s++;
          s[off] = c;
        }
      while (c != '\0');
    
      n = s - src;
      (void) CHECK_BOUNDS_HIGH (src + n);
      (void) CHECK_BOUNDS_HIGH (dest + n);
    
      return dest;
    }
    libc_hidden_builtin_def (strcpy)
    

    【讨论】:

      【解决方案3】:

      你为什么不试试 glibc 的副本,它有所有 c 函数的源代码,或者你可以获取 P.J. Plauger's book, "The Standard C Library 的副本。

      【讨论】:

      【解决方案4】:

      查找定义可能有问题,但请看一下: http://www.cplusplus.com/reference/cstring/?kw=string.h

      【讨论】:

        【解决方案5】:

        取决于您的操作系统和编译器。

        #include<...> //Will look up default include directory
        #include "..." //Will look up specified path in between the "..."
        

        每个操作系统的默认包含目录都不同,

        看看这个: http://gcc.gnu.org/onlinedocs/cpp/Search-Path.html

        【讨论】:

        • 那是针对头文件的。我认为 OP 实际上是在寻找标准库函数的实现。
        猜你喜欢
        • 2013-11-01
        • 1970-01-01
        • 2021-12-09
        • 2011-08-31
        • 1970-01-01
        • 1970-01-01
        • 2019-06-03
        • 2013-11-24
        • 1970-01-01
        相关资源
        最近更新 更多