_                _   _                 ____            _     _ 	
   / \   _ __   ___ | |_| |__   ___ _ __  |  _ \ _ __ ___ (_) __| |	
  / _ \ | '_ \ / _ \| __| '_ \ / _ \ '__| | | | | '__/ _ \| |/ _\` |	
 / ___ \| | | | (_) | |_| | | |  __/ |    | |_| | | | (_) | | (_| |	
/_/   \_\_| |_|\___/ \__|_| |_|\___|_|    |____/|_|  \___/|_|\__,_|	
                                                                bbs
  XQTRs lair...
Home // Blog // NULL emag. // Files // Docs // Tutors // GitHub repo
written by xqtr of another droid bbs [andr01d.zapto.org:9999]                   
                                                                                
BBSes, today are running in multiple platforms... windows, linux, rpi, os2,     
c64, macos etc. Writing mods/apps/doors for all of these systems it may         
be possible, but it's not easy. Someone could use a programming language        
that is supported by many of these systems and compile his program to           
each one. It may sound logical and easy, but it's not.                          
                                                                                
Each system differs in small or even big things, that the programmer must       
have in mind to successfully compile his program. This text will only           
show some of the most common mistakes and its not, by any mean, an in           
depth analysis for cross compilation.                                           
                                                                                
Conditional Compilation                                                         
The most important tool in your arsenal for cross compiling programs. You       
wont cross compile a thing, with out writing code which is unique for           
each system. For sure in a cc program, the 80% of code is usable in all         
systems, but you will have to write a 20%, which will be unique. To do          
this, we have to use conditional compiling.                                     
                                                                                
We write code for each system and let the compiler know with some               
flags/definitions. For example in FreePascal we can write something like        
this:                                                                           
  {$IfDef Windows}                                                              
    ..code here..                                                               
  {$EndIf}                                                                      
  {$IfDef Linux}                                                                
    ..code here..                                                               
  {$EndIf}                                                                      
When the compiler reach the first IfDef, will check if is compiling in          
a Windows system. If so, then it will compile the code between that             
IfDef block. If the system is not Windows, it will bypass this block and        
go to the next one, which tell to the compiler to check if it's compiling       
in a Linux machine etc.                                                         
                                                                                
So, we can write code, for each and every system we care about.                 
Conditional compilation is a big chapter and each programming language          
implements it in a different way, so you have to learn about it.                
                                                                                
                                                                                
Path Delimiters                                                                 
The most common mistake a programmer does, is to use the exact path             
delimiter that his system uses, inside the code. For example in Linux the       
path delim. is '/' but in Windows is '\'. If we use the exact character         
in the code, then the program will not compile in both systems. Instead         
we should use a variable which will represent the path delim. char.             
depending the system we compile the program. For example in FreePascal          
such a variable is PathDelim from the SysUtils unit. So instead of writing:     
                 mypath := currentfolder + '\' + filename;                      
we should write:                                                                
              mypath := currentfolder + pathdelim + filename;                   
The first will compile only under Windows, but the second will compile          
under all systems that FPC has a compiler.                                      
                                                                                
                                                                                
Know the OS!                                                                    
You wont be able to cross compile if you don't have some knowledge of the       
system OS you are going to compile for. Each OS has its own                     
characteristics, libraries, tools, structure etc. So you have to know           
about them. When you will learn them, you will be able to decide if you         
will use compiler directives/definitions or write code that is compatible       
with all systems or both. Its not advisable to always use compiler              
directives and in some cases you can't write the code with out using            
them. But to know this and make the wise decision, you must have some           
knowledge of the systems you are going to compile for. Some OSes use big        
or little endian format, the alignment for records, differs from system         
to system or the default signedness etc.                                        
                                                                                
                                                                                
Uppercase or Lowercase, Not Both!                                               
Basically this is the same as above, but because its a very common mistake      
it deserves to be mentioned separately. When you use files in a program and     
also use their file names inside your code, make sure that the case is          
the same. Don't include files with upper case filenames and refer to them,      
in your code, with lower case names. This will cause too much trouble in        
systems like Linux/MacOS/RPi. Prefer to use lower case filenames and stick      
to that guide, so you avoid trouble.                                            
                                                                                
                                                                                
Write once use many...                                                          
When you write your code, make it in a way that its going to be easy to         
reuse it again and again. Make your own Units/Libraries of code that can        
cross compile and use them, instead of re-writing the same code over and        
over. Just make it simple and not over do it.                                   
                                                                                
                                                                                
Use standards                                                                   
Use standard tools, libraries, components etc. as you can. Don't                
customize your code with extra stuff that are not very usable. The case         
will be, that a custom applet/component/lib that is used in one                 
system is not used in another. But if you use standard libraries from           
your programming language, you will have less problems cross compiling          
the code.                                                                       
                                                                                
                                                                                
Go slow and steady, to go faster...                                             
Don't try to write a program for a system and then port it to another.          
By doing so, you wont manage to simultaneously publish your code for all        
systems, but each port will have a delay. Also while porting the code to        
another system, perhaps you may "brake" the code. You will write code for       
one port that it may not be compatible with another and vice versa.             
Instead, write the code for a procedure/function and make sure that it          
compiles in all systems you want. Then proceed with another and so on.          
This way, you will be sure that your code inside your functions is              
working in all systems and the debugging process will be easier.                
                                                                                
                                                                                
                                                                                
             ,s.      .                                                         
     .       `$'      "s                   +  ý s                    sý'        
    "s,.  .   ;   , ,s$'    ,sS$$Ss,   ,sý        ýs. ,s. úú - ----- `$s. --- - 
 ,ssS$$$$$$SsS$SssS$Sý""""ýS$$$$$$SýýýýýS$SSSSýýýýýSS.`$',S$$.`s.`$SýýýS$SssSs. 
;$$$$$$'.`ý$$$$$$$$',sS$$Ss,`S$S' + ." ý ý$',sS$$$Ss.`ý; $$ý',s$' ,s$$Ss.`$$$$$ 
' `$$$$s s',S',ss.',$$$$$$$$Ss.s' sS$S',$.,S$$$$$$$$$Ss$s.,dS$$$$$$$$$$$$ $Sýý$ 
. ,$$$$$+`$s.$$$$$$$$$$$$$$S$$$$$SssssS$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$Sý', ;$;  
$$$$Sý',S$$$$$$$$S$$$$$$$$$$$$$$$$$$$$$ýýýý$$$$$$$$$$$$$$$$$$$$$$$$$$$$."ý' ',s 
$$Sý'."'ý$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ý ggg ^^ . ^^^^øøb$$$$$$$$$$$$$$$ý"+',$$$ 
$',s' s ,$$$$$$$$$$ý ,, ý aaa ý.,aaa,,  $$$ee,o$b $$$eea,. ý$$$$$$$$$$',s.`$$$$ 
$.`ý', d$$$$$$$$$$$ $$$   $$$ $$$'''$$$ %$$  `$$P $$$``$$$l $$$$$$$$$$.`ý',$Sý" 
$$S$$';$$$$$$$$$$$$ `$$o.d$$' $$$---$$$ $$$------ $$$- -$$$ $$$$$$$$$$$S.`$$;   
',s.`s Y$$$$$$$$$$$a .oeee$o. $$$ ° $$$ $$$ °°... $$$ °° .. $$$$$$$$$$$$$ $$$Ss 
.`ý',$+ `$$$$$$$$$$ $$$P-4$$$ `$$---$$$ $$$---.o$ $$$------ $$$$$$$$$ý"ý',$$$$$ 
$$$',ss+$$$$$$$$$$$ $$$ n S$$ p Y$" $$$ ^$$a.a$$' $$$ s$$$$$$$$$$$$$$.~ýs$$$$$$ 
$$$.`ý'. $$$$$$$$$$szzz$$$zzzz$szz. $$$ szzzzzzzs8zzzz$$$$$$$$$$$$$$$Ss"+ S$Sý$ 
$$'bz`S',$$SýS$$$SýS$$$$$$$$$$$$$S$Szzz$$SýS$$ýS$$$$$$$$$SýýS$$$$$$$',sSsS$' ,$ 
;$SssS $$$$s .`ý$ Ss.`ýS$$$$Sý',ssssS$.`$',.`$. `ýS$$$Sý',SS.`$'.`ýS.`S$$$$sS$$ 
 `ýS$$.`ýýý',$',ý ý$$$Sssssss%S$$$$$$$$ ',$',ý',$SsssssS$ýýS$ ; $$s.ý+`S$S$$Sý' 
(-------- -- úú"s' ú  `ý$'    `ý$'      ,           `S$'   $ ,$.  `ýSSSý'       
org:bz-basic.asc        ;       "s     `ý'            " s "' `ý'                
                       ,$.       '                                              
                       `ý'