PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, September 12, 2022

[FIXED] How exactly cross platform libraries works for different machines

 September 12, 2022     c++, cross-platform     No comments   

Issue

Recently, I was learning about networking in C++ and I found boost.asio which is a cross platform library, and then I got a thought about how this library is a cross platform since Windows provides different library for networking and even mac also, so how its function works on different machines, does cross platform libraries create their own functions for this purpose or they contain different private functions of different machine's logics and provides public functions which then during compiling time check for on which machine that codes are compiling and change our written functions with machines defined libraries.

For example

//Operations for windows
Private void WindowsFunc 
{ code } 

//Operations for mac
Private void MacFunc
{ code }

//library's functions
Public void Do
{
    //Performs different operations
    //for different machines

    If (windows)
        WindowsFunc
    else if (Mac)
        MacFunc
}

It could be a solution may be😗


Solution

There are several possible ways:

  1. Use ifdefs
char* doFoo(void){
#ifdef _WIN32
  return "win32";
#elif ...
  return ...;
#endif ...
}

The pro is, that it doesn't require much setup.

The con is, that it clutters the code, if you do it too much.

  1. Have different implementations in different directories.

As a folder structure:

root
|---->Other files
|---->windows implementation
|   |->foo.c
|---->linux implementation
    |->foo.c
|---->macos implementation
    |->foo.c

And then you can use some build system, or a custom shell script for selecting things.

(Pseudo code)

if(OS==windows)
  compileDirectory(windowsImpl);
else if(OS==linux)
  compileDirectory(windowsImpl);
if(OS==macos)
  compileDirectory(windowsImpl);

The pro is, that it doesn't clutter the code, allows better adding of new features and some sort of abstraction.

The contra is, that it can be quite tremendeous to setup.



Answered By - JCWasmx86
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing