Porting C code between different "platforms"

888 Views Asked by At

I have been reading statement in a lot of books/links like - "There are many factors which determine the porting of c code from one platform to other".Are the platform they refer OS or microprocessor.What are these factors on which porting of code from one platform to the other depends on. Thank you in advance.

3

There are 3 best solutions below

2
On BEST ANSWER

Platform probably refers to OS + compiler + virtual memory address space + underlying HW architecture.

Some of the factors are:

  1. The API provided by the OS for dynamic memory allocation, thread synchronization, shared resource protection, etc.

  2. The size of primitive types, which is determined by the compiler (except for sizeof(char) which is 1 by the C-language standard).

  3. The size of pointers, which depends on the size of the virtual memory address space (4 bytes on 32-bit platforms and 8 bytes on 64-bit platforms).

  4. The endian-ness of the underlying HW architecture, which yields different behavior when accessing types larger than char via "raw address" (e.g., using char*).

0
On

Understanding platform as OS, you have to take into account that lot of code use native calls to OS hooks and there are libraries that interact only with one OS. You can avoid this problems using libraries that support your target OS.

On the other hand, if you understand platform as architecture, you have some low level caveats like bitwise, typing, address size etc...

0
On

"platform" could refer to one or more of OS, compiler toolchain, or target processor.

Some of the factors that affect portability might include:

  • library functions or system calls used in the program,
  • non-standard extensions that might be used that are supported in one toolchain but not another,
  • representation of types that can change from one platform to another. For example int might be 32bits on one platform and 64-bits on another, or the endianess of numeric values might be different.