Ferit
Ferit

Reputation: 9717

C - Writing On Windows Compiling On UNIX And Vice Versa

I am planning to write an ANSI-C program on Windows with Netbeans using Cygwin suite, and later on i want to compile the source code on an UNIX family OS and use the program. Should i worry about any kind of compability problems?

Upvotes: 0

Views: 352

Answers (2)

Alexey Frunze
Alexey Frunze

Reputation: 62106

If you use only the functionality described in the C standard, the set of possible incompatibilities typically reduces to:

  • signedness of char
  • sizes of all types (e.g. int=long=32-bit in Windows, not necessarily so on UNIX), I mean literally all, including pointers and enums
  • poorly thought out type conversions and casts, especially involving pointers and negative values (mixing of signed and unsigned types in expressions is error-prone too)
  • alignment of types and their padding in structures/unions
  • endianness
  • order of bitfields
  • implementation-defined/specific behavior, e.g. right shifts of negative values, rounding and signs when dividing signed values
  • floating-point: different implementation and different optimization
  • unspecified behavior, e.g. orders of function parameter and subexpression evaluation, the direction in which memcpy() copies data (low to high addresses or the other way around), etc etc
  • undefined behavior, e.g. i+i++ or a[i]=i++, modifying string literals, pointer dereference when the object it's pointed to is gone (e.g. free()'d), not using or misusing const and volatile, etc etc
  • supplying standard library functions with inappropriate parameters leading to undefined behavior, e.g. calling printf()-like functions with wrong number or kind of parameters
  • non-ASCII characters/strings
  • filename formats (special chars, length, case sensitivity)
  • clock/time/locale formats/ranges/values/configuration

There's much more. You actually have to read the standard and note what's guaranteed to work and what's not and if there are any conditions.

If you use something outside of the C standard, that functionality may not be available or identical on a different platform.

So, yes, it's possible, but you have to be careful. It's usually the assumptions that you make that make your code poorly portable.

Upvotes: 1

ddyer
ddyer

Reputation: 1786

There will be comparability problems, but as long as you stick to basic unix functionality, they ought to be manageable for command line applications. However, if your app has a GUI or has to interact with other programs in the unix environment, you'll probably regret your approach.

Another way to go would be to run the appropriate flavor of unix in a virtualbox on your desktop, and be pretty sure there are no compatibility problems.

Upvotes: 1

Related Questions