简体   繁体   English

库定义在哪里?

[英]Where are Library definitions located?

Could someone say me where I can found the source file where are the definitions of the standard libraries? 有人可以说我在哪里可以找到源文件,标准库的定义在哪里?
For example, where is the file string.c that contains the definitions of the function prototyped in string.h ? 例如,包含string.h原型函数的定义的文件string.c在哪里? And above all, it exists? 最重要的是,它存在吗?

its all in compilled state, some of maybe optimized by asm. 它全部处于编译状态,有些可能已通过asm优化。 You need find sources of your compiler to see definitions 您需要找到编译器的源才能查看定义

For GCC, which is open source, you can download the sources for the libstdc++ library from their mirror sites here . 对于GCC,这是开源的,你可以从他们的镜像站点下载的的libstdc ++库源在这里 Included in the download is the source for the std library. 下载中包括了std库的源代码。 Bear in mind that different vendors will have different implementations, so the link provided is merely how the developers of GCC decided to implement the standard library 请记住,不同的供应商将有不同的实现,因此提供的链接仅仅是GCC的开发人员决定实现标准库的方式

You're probably not going to like this. 您可能不会喜欢这样。

The C++ Standard does not say specifically where anything in the Standard Libraries are actually implemented. C ++标准并未具体说明标准库中实际执行的位置。 It says where things are declared , but only to the degree that it names the file(s) you must #include in order to bring the names in. For example, the Standard says that: 它说明了声明的位置 ,但仅以它为文件命名的程度,您必须#include才能引入名称。例如,标准说:

std::string

is a typedef for basic_string<...> , and in order to bring that typedef in to your program, you must #include <string> . basic_string<...>typedef ,并且为了将该typedef引入程序,您必须#include <string> It doesn't actually say that basic_string or string are defined in <string> however, and it doesn't say where, on your hard drive <string> is even located. 它实际上并没有说basic_stringstring是在<string>中定义的,并且也没有说<string>在硬盘上的位置。 In fact, it's often not in <string> in the real world. 实际上,在现实世界中通常不在<string>中。 In my implementation, (MSVC10) string is defined in a different file, <xstring> , and it looks like this: 在我的实现中,(MSVC10) string是在另一个文件<xstring> ,它看起来像这样:

typedef basic_string<char, char_traits<char>, allocator<char> >
    string;

Useful, huh? 有用吧?

There's another aspect. 还有另一方面。 A lot of the stuff in the Standard Library is template stuff, like string , so because of the way templates work in C++ these facilities must be so-called "include libraries." 标准库中的许多内容都是模板内容,例如string ,因此,由于模板在C ++中的工作方式,这些功能必须称为“包含库”。 But not everything in the Standard Library is made up of templates. 但并不是标准库中的所有内容都由模板组成。

Consider sprintf . 考虑sprintf The Standard says that this declaration is provided by #include <cstdio> but that, like string isn't even where it's declared. 标准说,该声明是由#include <cstdio>提供的,但是,即使string在声明的地方也一样。 And sprintf isn't a template thing. sprintf不是模板。 the implementation is in what's often called the CRT -- the C Runtime Library. 该实现在通常称为CRT的C运行库中。 This is a collection of DLLs and LIBs (in MSVC10, anyway) that your program links to to run code like sprintf . 这是DLL和LIB的集合(无论如何在MSVC10中),您的程序链接到这些DLL和LIB来运行sprintf代码。

Now the bad news is those components that are in the CRT are generally shipped without source code. 现在的坏消息是,CRT中的那些组件通常出厂时都没有源代码。 You don't know where sprintf is implemented and you can't look at the source code. 您不知道sprintf的实现位置,也看不到源代码。 You're left with little alternative in these cases except get a job with MicroSoft so you can take a look at the source code. 在这些情况下,除了找到MicroSoft的工作之外,您别无选择,因此您可以看一下源代码。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM