[SOLVED] How to modify/compile a program to run on multiple platforms?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.