c/c++ 函数的源代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1127328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:54:53  来源:igfitidea点击:

source code of c/c++ functions

c++c

提问by mipadi

I wanted to have a look at the implementation of different C/C++ functions (like strcpy, stcmp, strstr). This will help me in knowing good coding practices in c/c++. Could you let me know where can I find it?

我想看看不同 C/C++ 函数(如 strcpy、stcmp、strstr)的实现。这将帮助我了解 c/c++ 中的良好编码实践。你能告诉我在哪里可以找到它吗?

Thanks.

谢谢。

回答by mipadi

You could check out a copy of glibc, which will have the source code for all C functions in the C standard library. That should be a good starting place.

您可以查看glibc的副本,其中包含 C 标准库中所有 C 函数的源代码。那应该是一个很好的起点。

回答by jalf

It's worth pointing out that the source code for the C standard library does not necessarily highlight good coding practices. As the standard library, it has a special status where, for example, it can rely on a lot of unspecified or non-portable behavior simply because they know the exact compiler with which it is used.

值得指出的是,C 标准库的源代码不一定突出良好的编码实践。作为标准库,它有一个特殊的地位,例如,它可以依赖许多未指定或不可移植的行为,因为它们知道使用它的确切编译器。

And of course, this only tells you about C coding practices.

当然,这只是告诉您有关 C 编码实践的信息。

That has nothing to do with C++ coding practices, which are very different. Don't treat them as one language. There is no such thing as C/C++. C is a language with its own coding practices and common idioms, and C++ is a separate, independent language which also has its own practices and idioms.

这与 C++ 编码实践无关,它们非常不同。不要将它们视为一种语言。没有 C/C++ 这样的东西。C 是一种语言,有自己的编码实践和常用习惯用法,而 C++ 是一种独立的、独立的语言,也有自己的实践和习惯用法。

Good C code is rarely good C++ code. strcpyand other C library functions are certainlynot good C++ code.

好的 C 代码很少是好的 C++ 代码。strcpy而其他C库函数肯定不是好的C++代码。

回答by Michael Burr

Most compilers provide source code for the library - however that source code is usually rather more complex and convoluted than you might expect.

大多数编译器为库提供源代码 - 然而,源代码通常比您想象的要复杂和复杂。

A good resource for this is P.J. Plauger's book, "The Standard C Library", which can be had pretty cheaply if you go for a used one. The code presented is not always straight-forward, but Plauger explains it quite well (and gives the reasons why it can't always be straight-forward and still follow the standard).

这方面的一个很好的资源是PJ Plauger 的书“标准 C 库”,如果您选择使用过的书,可以很便宜地获得它。所提供的代码并不总是直截了当,但 Plauger 很好地解释了它(并给出了为什么它不能总是直截了当并且仍然遵循标准的原因)。

回答by Pete

Many C standard library functions have source code listings & discussions in The C Programming Language by Kernighan & Ritchie. The discussions are a helpful way of learning more about the specifics of the C language & how functions in the standard library work under the hood.

许多 C 标准库函数在Kernighan & Ritchie 的 The C Programming Language 中有源代码列表和讨论。这些讨论是学习更多关于 C 语言的细节以及标准库中的函数如何在幕后工作的有用方式。

回答by IRBMe

Look at an implementation of the libcstandard C library. To see how a real, popular C library is implemented, try looking at the glibc code. You can access the code using git:

查看libc标准 C 库的实现。要了解真正流行的 C 库是如何实现的,请尝试查看 glibc 代码。您可以使用 git 访问代码:

git clone git://sourceware.org/git/glibc.git

git 克隆 git://sourceware.org/git/glibc.git

As for C++, you can find the glibc++ standard library on one of these mirrors:

至于 C++,您可以在以下镜像之一上找到 glibc++ 标准库:

http://gcc.gnu.org/mirrors.html

http://gcc.gnu.org/mirrors.html

You can also check out uLibc, which will probably be simpler than the GNU library:

您还可以查看 uLibc,它可能比 GNU 库更简单:

http://git.uclibc.org/uClibc/tree/

http://git.uclibc.org/uClibc/tree/

To give you a flavour, here's the strncpyimplementation from uLibc:

为了给你一个味道,这里是uLibc的strncpy实现:

Wchar *Wstrcpy(Wchar * __restrict s1, const Wchar * __restrict s2)
{
    register Wchar *s = s1;

#ifdef __BCC__
    do {
        *s = *s2++;
    } while (*s++ != 0);
#else
    while ( (*s++ = *s2++) != 0 );
#endif

    return s1;
}

Here is strcmpand strstr

这是strcmpstrstr

回答by IRBMe

"The Standarc C Library" is a specification. You want the sources for an implementation of the specification. Your C compiler may or not provide such sources - one that does is GCC.

“标准 C 库”是一个规范。您需要规范实现的来源。您的 C 编译器可能会或不会提供这样的源代码 - 一个是GCC

回答by Michael van der Westhuizen

You could also have a look at the OpenBSD source tree. Specifically, you want the string subdirectory of libc.

您还可以查看OpenBSD 源代码树。具体来说,您需要libc 的 string 子目录

回答by Tim Hoolihan

回答by Sinan ünür

About 10 years ago, I read The Standard C Library by Plauger. Thoroughly recommend it.

大约 10 年前,我阅读了 Plauger 的 The Standard C Library。彻底推荐它。

回答by Eric Petroelje

Here you go

干得好

The implementation will vary somewhat from OS to OS, but the GNU/Linux implementation is probably going to be the easiest one to find out there.

不同操作系统的实现会有所不同,但 GNU/Linux 实现可能是最容易找到的。