简体   繁体   中英

If stdio is cross-platform C / standard library abstraction for C

I am looking at compiling C for cross-platform, and have that figured out. Now I am wondering if you need libraries specific to windows/mac/linux, or if stdio (glibc) is a cross-platform library that abstracts away all the low-level syscalls into a nice standard API. Not sure if it's doing that. For example, file-system stuff, seems like that is cross-platform. But maybe net connection stuff isn't, not sure. Also, I see here a lot of #ifdef _WIN32 , even though it is just a simple file-system wrapper over stdio , so not sure if stdio is not covering everything.

Looking around a bit I see some "cross-platform" libraries like libuv have code specific to windows vs. unix, so not sure if there are cases where you need to hook into "native" (os-specific) functionality, and when/where you typically need to do that.

Wondering first if glibc / stdio is a cross-platform abstraction around the syscalls and common other functions. Then wondering if one could explain/outline briefly when you need to write platform-specific functionality, and if there is not a standard library / abstraction on top of that.

stdio.h is part of the C11 standard (see n1570 ; you really should download and read that). If you restrict yourself to use only functions described by that standard (in the way that standard specifies), and if you use standard conforming implementations (eg compilers and C standard library implementations), you should be safe.

However, many features (eg directories, symbolic links, file truncation) are outside of the C11 standard. You might be interested by the POSIX standard.

Sockets are not part of the C11 standard (but mostly POSIX, see this ).

Some libraries (eg Glib ) try to define common abstractions for several OSes. You might consider these if you want to easily write source code compilable on several common platforms. (With C++ you could also use POCO , Boost , Qt , ....)

BTW, Linux man pages (start with intro(2) & intro(3) ...) often mention the standard followed by most functions. For example chown(2) is defined in POSIX.1-2008.

Notice also that standards are specifications, which might not be entirely followed or respected (and they are bugs even in standards). For example C11 threads (eg thrd_create ) might not be available in some (still used) versions of glibc (because it has pthreads(7) which predated C11). Also, some libraries implement standard functions beyond what the standard requires (eg on Linux fopen(3) understands m in the mode string).

Remember the maxim: there is no portable code, only code that has been ported (to some specific system[s]). See also this .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM