Search code examples
c++stdheader-files

Does std:: library of c++ have .cpp files?


This may be a naive question since I'm a c++ beginner.

In a typical cpp project, we have cpp files and their header files. So based on my understanding, usually cpp files have definitions, while respective header files have declarations. During compiling, we have to include very cpp files in the project since header files do not specify the location of the definition of the declared object. Finally, linker links declaration and definition. This allows the communication between different translation units.

However, when it comes to std:: library, we do include header files but never compile its cpp files. For example, we write #include in main.cpp, but when compiling, the command does not have anything like g++ vector.cpp main.cpp ...

I even tried to search cpp files in the std library folder in my system, but all I can find is various header files.

I do know the definition and declaration can be included in a single header file, I don't think this is how std:: is wrote. Otherwise, it will result in multiple copies of definition when included in different cpp files.

Please help me to understand how this works? Can I do something like this with my own project?


Solution

  • Yes, there is compiled code in the standard library. You don't have to compile it because the library comes already compiled with your compiler. And you don't explicitly link to it, because the compiler knows about it. So you don't see it mentioned anywhere when you do normal builds. If you set your compiler to some form of verbose mode you can see that the name of the standard library gets passed to the linker, which pulls the code it needs from the library.