the accountant
the accountant

Reputation: 526

how can libraries achieve portability among different OS

like c/c++ standard libraries.

since the functions in the standard library eventually will call a specific OS API's to do it's jobs , how can these language libraries achieve portability, if I wanted to compile my code to run on other Os. will the libraries change it's code(the calls to the particular Os APIs) ?, is there different versions of these libraries target different OS's ?. or what ? thanks in advance.

Upvotes: 1

Views: 215

Answers (1)

Imobilis
Imobilis

Reputation: 1489

A quick example..

#ifdef OS_WINDOWS
   //definition for Windows
#else
  //define it for Unix
#endif

They use pre-processor to determine OS, some pointer tricks to determine endianness. Nothing too complex in general. Once stds determine the OS, using the pre-defined definitions.. they use the appropriate code that relates.

// Real-life example:
if(definition == 2) system("cls");
else system("clear");

Upvotes: 1

Related Questions