English 中文(简体)
Writing application for both Unix and Windows
原标题:

I ll write a program for Interactive UNIX (http://en.wikipedia.org/wiki/INTERACTIVE_UNIX). But in a year it will be ported to Windows. I ll write it in ANSI C and/or SH-script. When it runs on Windows it will be run as a Windows service. How do I make it as easy as possible for me?

I want to change as little as possible when I port it, but to make it good code.

Unfortunately, Interactive Unix is a old system and the only shell that exist is /bin/sh

最佳回答

If you are even considering doing this in SH script, then you should give serious consideration to Python which is already portable.

问题回答
  • Port early and frequently
  • Encapsulate non portable code. (Don t spread too many #ifdefs all over your code - rather create functions implemented separately for each OS in separate source files.
  • Be very strict with data types (use long short in structs/classes and not int)
  • I.e. switch on the highest warning level and resolve all warnings.

You can use platform-dependent ifdef-include pragmas and as strict types as possible. GLib has some nice ones defined which could be used on nearly every platform or architecture.

A shell script only option is not a viable alternative as on Windows platforms, there s no Bourne shell, Bash or KSH by default, and unfortunately PowerShell seems to be rare on XP machines. But you can create both a traditional batch file and a Bourne shell script.

But as others said, it s easier if you use a higher level language that s platform independent. And why wouldn t you? :)

I would recommand using ANSI-C and Lua (an embeddable small script interpreter). Try to use this with the basic required C functions you need.

You need to port and test often. If you work one year on unix and then try to switch it will be much harder, because often the best porting solution is a different design which is implemented on all platforms.

Windows can t run sh scripts directly, you need to use cygwin for that. So if you really want to run on vanilla Windows, you better use C. Stick to C89 and be careful. If you use any system calls, stick to POSIX ones and you should find them or equivalents on Windows. Windows also has a pretty comprehensive Berkeley sockets-alike library, so you can use that too within reason.

You re still going to have to do some #ifdefing.

You ll end up compiling it with MinGW if you make it a Windows task, if you stray too far into the UNIX den, you ll have to make it a cygwin binary instead, which has some baggage associated with it.

If it is not an option to add something that is inherently portable like python, ruby, perl, java etc. then your best option is probably to use ANSI C. One reason for C s initial popularity was it s (relatively good) portability. That said, anything that is closely tied to the OS, such as graphics, networking, etc are much less portable in C than in something like Python. You should strive to make "wrappers" for OS specific functions and keep those partitioned off from the main code. This way when it comes time to port it over, you re rewriting the wrappers, and everything else should compile without many issues.

All that said, it is a LOT easier to write something in Python and have it work everywhere. Plus it is more "fun" to write. So if you can avoid "interactive unix" in the future, do so.





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签