Unformatted text preview:

Orna Agmon [email protected] ProgrammingOrna [email protected] 26.5.2003May 30, 2003 Portable Programming Slide 1Orna Agmon [email protected]• Essence of Portability• Levels of Portability• Portable Programming (mainly C)• Cross Unix issues• Files• Shell scriptsMay 30, 2003 Portable Programming Slide 2Orna Agmon [email protected] built it for Linux, I want people to use Linux to use my application.After all, Linux is the best platform!May 30, 2003 Portable Programming Slide 3Orna Agmon [email protected] Should I Write Portable (Cross-Platform)Code• Be able to compile and test on various platforms.• Latent bugs may be more obvious on other platforms. Crossplatform testing improves the code quality.• Make use of platform-specific tools, such as valgrind (on Linux)or third (on OSF1)• Be able to use whatever machine falls into your hands for thepurpose of running your code.• Be able to use multi-architechtural PVM.May 30, 2003 Portable Programming Slide 4Orna Agmon [email protected] of PortabilityUsing standards :• Verifying (by documentation) the standard behavior of toolsbefore using them. i.e., what the tools are supposed to do, andnot what they usually do.• Relying only on standard behavior of functions and tools.Avoiding possibly cool system-special features.• Using standards in a level which is as low as possible. (e.g., incross-platform source rather than a widely portable compiler orlinker)• Wrapping tools in order to guaranty at least that they do notcrash when accepting an undesired syntax.May 30, 2003 Portable Programming Slide 5Orna Agmon [email protected] of PortabilityUsing portable tools (languages, compilers, OSes) and features:• Widely available• Free softwareActually laying hands: on other systems, compilers, OSes, shells,architectures, and building and running on them.Documenting in free and open formats (pdf, ps...).May 30, 2003 Portable Programming Slide 6Orna Agmon [email protected] of Required/ Desired Portability• Many unknown systems or just two known ones? What areexpected future developments?• Is the software required to work on other platforms, or also haveits look’n’feel?• How system-specific is the code?The scope of this lecture will be user space programs, and where toexpect systems to vary. Some hints may even be given as to how tosolve these issues.May 30, 2003 Portable Programming Slide 7Orna Agmon [email protected] Windows Solutions• Emulationof UNIX environment: cygwin , Packages of Gnu toolsfor Windows.• Emulation of Windows environment: wine• Scripting: perl, python, tcl/tk• Common user interface libraries• Specific code (depending on the type of machine, discovered byconfigure)May 30, 2003 Portable Programming Slide 8Orna Agmon [email protected] standards• ISO C (ANSI C) since 1989. Widely supported, hence preferredstandard.• K&R (the first book on C by Kernighan and Ritchie) C. OldStandard.• C9X. Not widely supported by compilers.May 30, 2003 Portable Programming Slide 9Orna Agmon [email protected] standards• gcc is highly portable itself, but some extensions are not. In gcc,the “-pedantic” option will warn about the use of gcc extensions.• macros from <features.h> will enforce usage of chosen standardsin the source level instead of compiler level.• Example: for ( int i=0; i < 10; ++i){}. i may be declaredjust within the loop (gcc, compiled as c), stay declared after theloop (Visual C, compiled as cpp), or the syntax may not compileat all (cc of OSF1, compiled as c).May 30, 2003 Portable Programming Slide 10Orna Agmon [email protected]”The good things about standards is that there are so many differentones to choose from.”—Patty Seybold, 1988.May 30, 2003 Portable Programming Slide 11Orna Agmon [email protected] Types• Some C types specify minimal sizes only: int is at least 16 bit,long is at least 32 bit. short is exactly 16 bit, char is exactly 8.• Exact data types are defined in C99 in <stdint.h>. OnopenBSD, for example, the same types may be defined in<sys/types.h>. Solaris: <sys/inttypes.h>. The purpose ofautoheader is to solve these issues.• <limits.h> supplies, well, limits! (maximal values that datatypes can hold)• Many Fortran compilers have parameters to define the sizes ofvariables, such as “ -i4 -r8”. Those parameters enforceconsistency within a program.May 30, 2003 Portable Programming Slide 12Orna Agmon [email protected] Types still...• Usage of special types will ensure consistency with systemlibraries: sizet for sizes, time t for the return value of the timefunction. Here, again, there may be differences regarding locationof definitions, and autoheader comes to assist.• Consistency within the software can be achieved by usingtypedefs, defined in one header for the whole package. Thosetypes must be consistent for each system, though they may varybetween systems.• Bit operations are dangerous regarding varying data sizes.May 30, 2003 Portable Programming Slide 13Orna Agmon [email protected] pointers to Data TypesCasting pointers is dangerous. Period.• For example, the space of one 8-byte real will accommodate four2-byte int values on some system, and two 4-byte int values onothers) .• Casting to larger data types, for example (int *) to (float *), maylead to illegal addresses.• It is not even necessary for sizeof(int)*2=sizeof(double).May 30, 2003 Portable Programming Slide 14Orna Agmon [email protected] Stuff• Usage of static memory (relevant to FORTRAN)• Linking languages: the name of the compiled function varies.Some compilers may add anotherin the end of subroutineswhich havewithin their name. Example: when the FORTRANsubroutine uses myfunc and myfunc (both in C), the linker willlook for a C subroutines called myfuncand my func .• Stack size• memory size.May 30, 2003 Portable Programming Slide 15Orna Agmon [email protected] is architecture dependent.• Little-endian: the least significant byte is at the lowest addressin memory. Examples: Intel, Alpha.• Big-endian: the most significant byte is at the lowest address inmemory. Examples: MIPS, Sparc.Relevant when storing binary data or communicating it.May 30, 2003 Portable Programming Slide


View Full Document

Radford ITEC 310 - Portable Programming

Documents in this Course
Load more
Download Portable Programming
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Portable Programming and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Portable Programming 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?