How do I know that a C program will be written in Linux will work elsewhere

I need you to explain to me how I can find out if my C program will work, for example, on Windows if I write it in Linux and it works on that Linux and vice versa (a program written in Windows).

And what standards should I follow to write programs that will work on different operating systems? How can you determine if functions from libraries or entire libraries will work on different operating systems?

For example, I have a function fread

, fwrite

, fopen

, fclose

from the library stdio.h

- how to know if I could work with them properly in other systems besides Linux? The same with the functions that work with file descriptors: open

, creat

, close

, read

, write

from libraries: sys/types.h

, sys/stat.h

, fcntl.h

.

+3


source to share


1 answer


Ok, this is pretty simple: your program will run on Windows if it only uses two things:

  • The C standard library as described in the C standard. See n1570.pdf , the latest version of the project, which is very close to the actual standard. Note that many standard C libraries come with all types of extensions, of course you cannot use them. If in doubt, compile, for example, with the option -std=c11

    ( gcc

    or clang

    ), which should hide all declarations for extensions.

    There is another mistake there: Microsoft's implementation of the C standard is incomplete. It depends on your compiler, which is actually linked to the C standard library, in case MinGW

    it is a very old msvcrt.dll

    one that only corresponds to C89, so you can use the older standard instead, for which functions you can use, for example here is the description of the standard library in C89 With Microsoft C compilers, you get different levels of C99 support also in the standard libraries (they link their own runtime DLLs), depending on the version.

  • Third-party libraries available for Windows, such as, SDL

    , GTK+

    , etc.

Using the open

, creat

, close

, read

, write

etc, you are using the extension specified by POSIX. Some of the POSIX features are available on Windows, some are slightly different, some are simply missing. You are better off not using them if you want to remain portable to Windows. The exception to this rule is if you plan to compile your code with Cygwin , which is the full POSIX compatibility layer for Windows.

As per IanAbbott's comment, it's also important to use integer types correctly and implicitly accept things like "a long

has 64 bits" or "a int

may contain a pointer" which are wrong in general, Always use types <stdint.h>

for fixed width, uintptr_t

for pointer values , size_t

for object dimensions, etc.




Although this is not directly related to your question: for most non-trivial programs, you will sooner or later need to use some of the platform's functionality. There are often libraries that abstract this away, for example if you need to efficiently handle I / O events instead of using epoll

Linux, kqueue

FreeBSD, IO completion ports

Windows, etc. you can just use libevent

. But if you don't find such a library, here's a hint:

Clearly separate platform-specific code into your own modules (translation units). Thus, the work of transferring the entire item to another platform becomes significantly less.

+10


source







All Articles