Advertisement Jump to content
Sign in to follow this  
ram090

Where is the source for cout

This topic is 1875 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to get a full idea of where the compiler finds/processes the code for C++ statements/keywords etc.  For example take the statement 'cout', we have to include iostream for this. So looking at iostream we see a declaration for std::cout, one or two lines of code,  after a bit of research I see we also have a file called ios_base which is 'the master file for all the I/O stream classes'. Not that I exactly know what's going on here. Anyway, I take it that the fundamental code for 'cout' is in a dll or cpp file  somewhere, and at linking time the linker accesses this code in the dll and lunks? it's functions/methods etc to the compiled EXE?

Where is the point where iostream or ios_base tells the compiler that the code for cout in stored in xxx.dll file etc. All the header file does is declare a prototype for cout, so where is the source for this prototype.

I guess what I am asking is what it the process of linking If you have a function in a dll such as 'cout' how does the linker actually use/add this function to the compiled EXE. It's not a copy/paste process like a header file is to the source file. What is the process of communicating with dlls.

Another way of looking at it, I could write a class that adds two numbers then compile this into a dll and call this add function via LadyLibrary, I am assuming that keywords like cout have a function/class somewhere in some dll.  Or how does it all work.

Share this post


Link to post
Share on other sites
Advertisement
Clang's C++ compiler provides libc++[.so|.dll], GCC-C++ provides libstdc++ (I think), and other compilers provide their own standard C++ Library.

When using the C++ compiler, it links in the core c++ library implicitly. You can easily test this in GCC by compiling the .cxx files into object files (with -c) and then using ld as the linker rather than g++. This will then throw a fit and spam the console with linker error messages since the standard library is not linked (ld is a C linker when used with no parameters).

As for tracking down the exact declaration of std::cout... Good luck, C++ can be a nightmare to follow when overly used but reading standard library source code is something much worse ;).
Note that in many cases, the header files also have different implementations depending on the compiler, vendor or even standard library used. For example, OpenBSD has clang 3.2 and so does FreeBSD, however OpenBSD's clang uses a different C++ standard library (so doesnt support the full C++11 standard). Edited by Karsten_

Share this post


Link to post
Share on other sites

You can have an idea how std::cout looks like in the Standard library here: http://en.cppreference.com/w/cpp/io/basic_ostream

 

The C++ standard and the C++ Standard Library mostly define only the interface and the behaviour for the final user. The source code of Standard library, especially the IOS libraries, could be a pain to read, since it's common the use of non-standard proprietary and OS-dependent library in the implementation.

 

Therefore, since most implementation of the C++ Standard Library and most of C++ core feature are "Implementation-Defined Behaviour", you must look a the library of your compiler. Note also that implementation could vary a lot from different version of the compiler (As an example, the VC++ implementation of the C++ standard library of the last three versions of visual studio has changed a lot).

Edited by Alessio1989

Share this post


Link to post
Share on other sites

A DLL (a dynamic link object, known more generically as a DSO, or dynamic sharable object) is a specially formatted binary file.  At some designated position in the file is a dictionary of names and address offsets within the file of associated linkable objects that can be used by the runtime link-loader to resolve unresolved symbols.

 

So, when your program is loaded, or perhaps it calls LoadLibrary() or dlopen() (as appropriate), the runtime link-loader loads in a DSO and finds that it has some unresolved symbols (say, for example, std::cout).  It then looks through the already-loaded DSO dictionaries and replaces the unresolved symbol entry with that of the known object.  When loading the dictionary, it reads it from the DSO into an addess in memory, and uses that memory address in its own dictionary for future symbol resolution.

 

When you're linking your own program, you need to pass the names of the libraries that will be used at load time to resolve external symbols.  By including <iostream> and referencing std::cout, the compiler will emit an unresolved symbol into the .OBJ (or.o) file.  When the linker builds the .EXE, you need to specify MSVCPP.LIB (or -lstdc++, or -lc++, or whatever) on the command line... the compiler driver (visual studio, or gcc, or clang, or whatever) will often do this implicitly for standard libraries.  The EXE will end up with a dictonary that says something along the lines of "unresolved symbol std::cout from libstdc++.so.6" as a placeholder, to be resolved at program load time.

 

Even more exciting: and EXE is a DSO, and so is a DLL.  When your EXE loads DLLS and resolves symbols, each DLL may also further load other DLLS each with resolving and unresolved symbols.

 

If you're looking for the C++ source for std::cout itself, it's most likely in a header file you can read.  It will make calls into the C runtime library, which may not be available to you, depending on your target platform.  GNU/Linux is Free Software so you can see it all, OS X uses the Open Source BSD runtime, and the others (Microsoft Windows, Android/Linux, most consoles) are closed secret proprietary source with no user-servicable parts inside.

Share this post


Link to post
Share on other sites

 ... Android/Linux, ...) are closed secret proprietary source with no user-servicable parts inside.

I thought the Android c++ standard library was open-source.

 

https://github.com/android/platform_bionic

 

It is BSD licensed so there is no reason why Google cannot make it closed source but until then we can read away ;)

Share this post


Link to post
Share on other sites

(Microsoft Windows, Android/Linux, most consoles) are closed secret proprietary source with no user-servicable parts inside.

Well... Both PS3 and PS4 seem to use a FreeBSD based OS. Coincidentally, there was one time when a Sony employee posted a commit for FreeBSD, very low level stuff for a PowerPC CPU. It's interesting since AMD doesn't supports their hardware on FreeBSD AFAIK, so they're probably rolling some custom drivers too.

 

Not that most of us would ever get to see the sources of their FreeBSD fork tho...

Edited by TheChubu

Share this post


Link to post
Share on other sites
It's interesting since AMD doesn't supports their hardware on FreeBSD AFAIK, so they're probably rolling some custom drivers too.

Yep, thats right, though interestingly since we have had no help in the form of binary drivers from AMD, the open-source radeon driver is actually pretty usable for 3D (almost on par with that in Linux). However, since Nvidia provides binary blobs for its hardware, the open-source "nv" is utter shite (nowhere near the usefulness of Linux's Nouveau).

 

Moral of the story, AMD has had to be cruel to be kind ;)

 

All I know is that if I was that skilled developer putting all that effort into developing a highly complex driver, it must be soul destroying to know that in ~5 years when the new console comes along, all your effort will be completely lost. I bet they would love to open-source their work and prove to the world that they are one of the rare breed of programmers, smart enough to be able to create something like that!

 

Edit: Thinking about it. I bet all AMD did was release the specs for their hardware to Sony and they did it all in house. Afterall, AMD's UNIX drivers are extremely low quality (no 2D acceleration for a start) which would be an issue for the PS4.

Edited by Karsten_

Share this post


Link to post
Share on other sites

There's plenty of full articles on this topic available.  You're likely to find better answers on StackOverflow or just Googling a bit than asking on a game development site for generic C++ questions like this in the future.

 

http://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work

http://www.lurklurk.org/linkers/linkers.html

http://www.lifengadget.com/lifengadget/compiling-linking-cplusplus/

Share this post


Link to post
Share on other sites
As others have pointed out, the standard library builds the io classes using a hierarchy of base classes so they all have interchangeable parts.

The cout object is an instance of an ostream object that has been specialized and bound to whatever the standard output device is. That in turn is a specialization of the basic_ostream, which is a specialization of the basic_ios, which is a specialization of ios_base. Each level of the hierarchy adds functionality. If you want to cover it in depth you will need to quite a lot of reading; there is a book 'The C++ Standard Library' by Josuttis that covers it, but be warned that it is rather technical. It also covers an older version of the C++ standard, but the content of the io stream system is largely unchanged.


The source code depends on your compiler. For Visual C++, it is located under "C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\crt\src" if you installed it, adjusting for whatever version number you have. I believe it is included in the default install options.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!