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:
- 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.
- 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)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.