开发者

relationship between programming language and OS APIs

there are methods in program language in c++ like

cout<<"hello world" .

when compiled does it i开发者_运维问答nvoke a system call to perform the actual work OR is it compiled directly into binary code and get executed by the kernal ?

if it use the OS API , different platforms using different OS API, how can the language be the same?


ok in the simplest terms I can think of.

A language compiles down to executable code that you wrote and this is linked with libraries of code written for the platform in question. Library code can be standardised by defining an API. For each platform a differnt standard library is generally needed. For most platforms a lower level api (defined by the OS) is also available. The language standard libraries then have the choice of access the OS APIs or directly replicating functionality. You can bet that in most cases the OS API will be used - but this is not the sole option as I've pointed out.

Ok lets take your example:

cout << "hello world";

lets say you compile this. In essence your compile will essentially turn this into a call that sends a string to a piece of code that will either make a call to OS api (say called WriteStringToConsole) or it may make calls to lower level components such as to video card to display pixels that spell out the the contents of the string. Or it could go anywhere in between. That's it's choice. All that matters is that the language definition is changed into a call to some standard function by the compiler on the platform of your choice.

Now whether the code can write to the machine or has to make OS calls is choice made by the OS designers. For example in MS DOS you could write directly to the hardware. However in current versions of WINDOWS the OS designers employ many tactics to ensure system design goals such as security or stability etc. are met and and they may force you to use particular OS calls in your standard library rather than lower level calls.

However all this boils down to one thing. You write code in a language that the compiler manufacturer has made compliant with one or more stanard libraries. NOte you may have written some of these libraries yourself (e.g. reusable code such as functions in your language).

The language is your API in a simple sense. The language can use many other APIs (libraries) and some are more standard than others. These librares will then use other libraries/APIs and so on until some electrons and moved down an electrical track. However this paradigm actually continues further as electrical components too have standardised APIs, its just that the implementations are different to software.

Edit: This is a gross over simplification. The compiler's job is just turn your code in your chosen language into code that can be executed. The Linker takes this and combines with stanard libraries. The choice of library is your's though the linker may have default that are cleverly hidden until you are ready to make an informed choice. It is the job of the library concerned to decide what calls to make.

Now if you write code and don't make your own OS calls and only call the stanard library then you start to get something called portable code that could be taken to another platform and compiled and thus executed there. Again its a simplification as there many other platform specfic things to take into account. Actually you can take this further by a process called cross-compilation where you compile on one platform but the code will execute on another - but I leave that as an exercise for the reader.


the idea behind C\C++ is write once-COMPILE many, unlike java which should be write once run many. Basically you are right, when you compile the basic cout\printf\puts etc.. are only headers in the programe, the actualy implementaion resides in either a dll\so file or a static obj\a file (depandig you you statically link which makes your executabl portable or dynamiccaly link which makes dependencu betweem your software and the libraries it was compiled against (version included)). This implementation usually ships with the compiler (VS\gcc) unless you get some external libraries which contain some low level system interaction.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜