0

As i know Windows operating system used the assembly language to interact with the hardwares. When they did this,they could use c,c++ or any other language to the rest work.

As i know C++ header files actually call the windows api for the implementaion.

So where are header files located? Are them installed by compilers ? or they come with the operating systems?

What keyword or code do the header files use to interact with the sutable api(for example std::cout on windows,calls a function in a dll file and in linux an other)?

For example is iostream.h different on linux from windows?

And how that how they find suitable libraries?

And my last question is that,how libraries interact with assembly code?(so assembly code interacts with hardware)

TIA.

Ambc
  • 19
  • Your knowledge seems wrong, or at least badly formulated (e.g. machine code is not assembly language, libraries are not mostly header files, etc). GIYF to learn more. Start with [library](http://en.wikipedia.org/wiki/Library_%28computing%29) wikipage. Read also [Levine's book: *Loaders and Linkers*](http://www.iecc.com/linker/) – Basile Starynkevitch Aug 24 '14 at 10:30
  • Thank you,Would you please tell me what is wrong? – Ambc Aug 24 '14 at 10:31
  • Read first the links I gave in comments. Also for Linux: read about [ELF](http://en.wikipedia.org/wiki/Executable_and_Linkable_Format) and [Drepper's paper: *How to write shared libraries*](http://www.akkadia.org/drepper/dsohowto.pdf) – Basile Starynkevitch Aug 24 '14 at 10:33
  • And most of the sentences in your question are at least slightly wrong. – Basile Starynkevitch Aug 24 '14 at 10:37
  • Read also about [compilers](http://en.wikipedia.org/wiki/Compiler) and [object files](http://en.wikipedia.org/wiki/Object_file). – Basile Starynkevitch Aug 24 '14 at 10:43
  • 1
    @BasileStarynkevitch:- These are good list of items for OP to get an idea! :) – Rahul Tripathi Aug 24 '14 at 10:43
  • "As i know C++ header files actually call the windows api for the implementaion." - This is an example for one of the wrong sentences. C++ header files and the Windows API are two entirely unrelated things. In fact, it's usually a good idea to keep the Windows API out of headers, if possible, and restrict it to implementation files. – Christian Hackl Aug 24 '14 at 11:41
  • P.S.: unless by "C++ header files", you refer to the C++ standard headers. – Christian Hackl Aug 24 '14 at 11:41

2 Answers2

1

The following passage isn't meant to be any sort of complete description of how libraries, compilation processes or system calls invoking works but rather a bird-eye view of what OP asked, thus lacks several details and important passages which will have to be studied in-depth by the OP himself

By "C++ library" I assume you're referring to the C++ standard library (although the considerations here are valid for any other library as well).

The C++ standard library isn't mandatorily present on any operating system by default, it usually comes shipped with a compiler installation or with a secondary package. That doesn't mean you can't execute C++ compiled routines, you just need headers and the library in order to compile your programs along with a compiler which supports it.

The C++ standard library is usually compiled platform-specific and you can't just copy the headers and lib files from one operating system to another one (you'll end up in tears).

Everytime you import the declarations from a header file with something like

#include <iostream>

you're rendering your program aware of the moltitude of data structures, functions and classes provided by the standard library. You can use them as you want as long as you provide the .lib file (in a Windows environment) where the code of the routines is usually defined (in Visual Studio this is usually referred as the Runtime Library with /MT /MD options) for linking.

Once you've linked your executable against those .lib files, you have a compiled executable which, opened in a disassembler, might have something like (for a simple hello world, snippet from here - not a windows environment)

mov     edx,len                             ;message length
mov     ecx,msg                             ;message to write
mov     ebx,1                               ;file descriptor (stdout)
mov     eax,4                               ;system call number (sys_write)
int     0x80                                ;call kernel

thus eventually every C++ function or routine provided by the standard library either implements an algorithm and/or eventually call some operating-system specific routines through System Calls. There are several design and implementation differences between the various operating systems (and even the boundaries of the system call points) in addition to a thousand of layers for security checking (not to mention ring3/ring0 switches) so I won't spend more words here about that.

Community
  • 1
  • 1
Marco A.
  • 43,032
  • 26
  • 132
  • 246
  • So you mean that C++ headers call C++ libraries! But how C++ libraries work? – Ambc Aug 24 '14 at 12:32
  • C++ libraries are code, compiled executable code. By writing "call function1()" in your main() function and compiling it you're nothing more but doing a jmp (or a call, there isn't a great deal of difference) to another part. – Marco A. Aug 24 '14 at 12:41
  • Let me explain my problem with another scenario: When you write std::cout in your code,what will happen? I **think** that first compiler will go and ask iostream.h that where is the implementaion? So how that implementaion was written?(I think using Windows API) – Ambc Aug 24 '14 at 12:48
  • iostream is just a header, it contains prototypes (and some inline perhaps) for the functions, it contains **declarations** that will instruct your code on how to use the **definitions** of the library (you'll be able to use them after linking). Who wrote the iostream header also wrote the cpp file that first used that header and compiled the cpp file to a .lib that is what you link against. Did I get your question correctly now? – Marco A. Aug 24 '14 at 12:53
0

You may try to install the Windows SDK and check the %PROGRAMFILES%\Microsoft SDKs\Windows directory.

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331