2013-06-20 36 views
3

我想訪問string.h的源文件。我的意思是具有string.h中所有可用功能定義的文件。我在哪裏可以找到string.cpp的源碼

例如strcpy()string.h中的功能;我在哪裏可以得到它的定義,因爲string.h只給出了函數的原型?

+0

這是一種連接實施的一大點。不過,編譯器的實現可能是開源的。我不確定標準庫如何適應這一點。 – chris

+0

嘗試查看'gcc'源代碼。 – devnull

回答

5

您沒有指定開發人員工具 - 此答案適用於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文件中實現(其中一些在彙編中實現)。

每個工具/操作系統的源位置和可用性會有所不同。

0

取決於您的操作系統和編譯器。

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

默認include目錄對於每個操作系統不同,

看一看這樣的: http://gcc.gnu.org/onlinedocs/cpp/Search-Path.html

+3

雖然是用於頭文件。我認爲OP實際上是在尋找標準庫函數的實現。 – hugomg

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) 
相關問題