LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-20-2012, 11:51 PM   #1
Ajit Gunge
Member
 
Registered: Jan 2008
Location: Pune
Distribution: RHEL,fedora
Posts: 253
Blog Entries: 1

Rep: Reputation: 21
Post How to modify/compile a program to run on multiple platforms?


Hi All,
Very Good Morning...I have a general question about programming in general.If I have written a program that is compiled for a particular platform(Linux/windows) how can I ensure that with minimal changes I can run the same program on other platforms also.I mean what is that extra peace of code that one adds to ensure such a situation.Can anyone please let me know?

Thanks,
Ajit
 
Old 02-21-2012, 12:01 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Which language?
In very general terms, sticking to -ANSI (or equiv) flag for eg C would help...
However(!) anything that depends on the actual OS convention could be a prob eg *nix is case sensitive, uses '/' instead of '\' etc....
Try to isolate OS dependent code in separate fns/files that you can easily replace with target OS equivalents, and program the internal interfaces as reqd.
You may want to ask the Mods to move this to the Programming forum for deeper info via the Report button
 
Old 02-21-2012, 02:09 AM   #3
dudeman41465
Member
 
Registered: Jun 2005
Location: Kentucky
Distribution: Debian
Posts: 794

Rep: Reputation: 56
I'm not an expert programmer, but I would imagine using programming languages like Python or Java are usually easier to do this with, because you use the built-in functions of the language more than those of the operating system itself. For example, if you write a python program on a Windows box, and don't issue any os dependent commands, then that script would run on both platforms.

Another way around this would be to identify the differences (as chrism01 said) and then use conditional statements to execute the code appropriate to the environment. For example, here's an example from a python program I wrote and use called "PingChecker". Its purpose is to ping a list of devices, however because there's no way in Python (that I could find) to ping a computer using built-in functions, I was forced to issue commands to the operating system. However, the syntax for the ping command differs between Unix and Windows computers, so I used conditional statements to set the command to execute based on the type of environment. In Python, executing os.name will return a different value based on your environment, so I used that to determine the environment, and then set a variable that would later be executed as a command. See the example.

PingChecker Download

Code Snippet from PingChecker
Code:
import os

if os.name=="nt":
    ping="ping -n 1 "
elif os.name=="posix":
    ping="ping -c 1 "

Last edited by dudeman41465; 02-21-2012 at 02:12 AM. Reason: spelling
 
1 members found this post helpful.
Old 02-21-2012, 02:11 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves. Moreover, the thread title has been changed to something related to the actual question.
 
Old 02-21-2012, 08:44 AM   #5
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
One of the greatest challenges is nowadays the use of the presentation layer.
Windows GUI takes a complete different approach than Linux X or whatever framework is used. How the widgets interact with each other is also very different.

There are various approaches: one could do "it all" by themselves, or use a cross-platform GUI toolkit (like Qt).
If a program is already written and now you want to port it to another platform, you'll have to figure out on what components your application depends. This also may be effected by the programming language used.

There are a few variables here:
- Programming language: some are more suitable to porting from one platform to another than others. C, C++ and Java are among those
- Dependencies: If your application depends on libraries, those libraries must equally be available on both platforms
- GUI: One may want to use a specific GUI toolkit, but it should be one that is available on both platforms
- Other platform specifics: as said earlier: each platform has also lower level specifics for various tasks, like using file-IO.

It seldomly is a single step, single thing that one does that always works so that all applications work the same on all platforms. Sometimes it's so hard, that it isn't worth the trouble. See applications using DirectX rather than OpenGL for their graphics. OpenGL is cross-platform, DirectX is not. If developers would have portability in mind when they start writing their applications, we'd have a very different world today.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
configure Makefile to compile for multiple platforms StargateSteve Programming 0 03-26-2007 11:54 AM
How Do I Run/Compile a Program Written in C? debsan Fedora 2 05-25-2006 02:58 PM
PGP Password program for multiple platforms? arobinson74 Linux - Software 0 11-19-2004 12:53 AM
How to compile and run my first program vanhelsing Programming 2 07-11-2004 02:41 PM
how to compile and run c program under mandrake 9.2 ?? mschua44 Mandriva 7 02-26-2004 11:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration