1

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.

the accountant
  • 506
  • 6
  • 14
  • 1
    possible duplicate of [Creating a portable library to run on both linux and windows](http://stackoverflow.com/questions/19418908/creating-a-portable-library-to-run-on-both-linux-and-windows) – Ken White May 13 '15 at 19:46
  • 1
    This is one of the big things pre processor directives are used for. – NathanOliver May 13 '15 at 19:50
  • @NathanOliver thanks , so that means the libraries have different implementations for different OS's and when compiling my program the correct implementation will be taken from the static library ? – the accountant May 13 '15 at 19:56
  • @Ken White thanks , the question you put emphasizes that there are different dlls for different OS's, is that correct ? – the accountant May 13 '15 at 19:58
  • 1
    @big.heart Yes. You basically have code to deal with each OS in the program and you use the pre processor to determine which code to actually use. – NathanOliver May 13 '15 at 19:59
  • 1
    See also http://stackoverflow.com/questions/5919996/how-to-detect-reliably-mac-os-x-ios-linux-windows-in-c-preprocessor#5920028 – programmerjake May 13 '15 at 20:02
  • @NathanOliver, I got it, but what about the dynamic linking ? are the dll's have different implementations inside their binary code that will execute differently at run time depends on the OS's they are on ? – the accountant May 13 '15 at 20:02
  • 1
    @big.heart generally there is a seperate .dll (or .so for linux or .dylib for OS X) for each operating system. – programmerjake May 13 '15 at 20:03

1 Answers1

1

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");
Imobilis
  • 1,475
  • 8
  • 29