Where can I find source of string.cpp

2020-07-25 06:43发布

问题:

I want to access source file of string.h. I mean the file which have definitions of all functions available in string.h.

For example strcpy() is a function in string.h; where can I get its definition, as string.h only gives prototypes of functions?

回答1:

You didn't specify a developer tool - this answer is for Visual Studio 2008 on Windows. CRT sources can be found under:

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

if it's installed at the default location. For other versions of Visual Studio, just replace the 9.0 part with 10.0 (VS 2010) or 11.0 (VS 2012).

You won't find a single string.c file - several functions are implemented in their own .c file (and some of them are implemented in assembly).

The source location and availability will vary for each tool / operating system.



回答2:

The source implementation for the C standard library will depend on what environment and compiler you are using. If you are programming on Linux you probably use glibc, which is open source and can be freely downloaded here.

Here is its implementation of strcpy, by the way:

/* 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:

Why don't you try a copy of glibc it has source for all c functions OR you can grab a copy of P.J. Plauger's book, "The Standard C Library.



回答4:

Depends on your Operating System and compiler.

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

The default include directories are different for every Operating System,

Have a look at this: http://gcc.gnu.org/onlinedocs/cpp/Search-Path.html



回答5:

Probably it's problematic to find definitions but have a look at this: http://www.cplusplus.com/reference/cstring/?kw=string.h