简体   繁体   English

如果stdio是跨平台C / C的标准库抽象

[英]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. 我正在为跨平台编译C,并且已经弄清楚了。 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. 现在,我想知道您是否需要特定于Windows / mac / linux的库,或者stdio (glibc)是一个跨平台的库,可以将所有低级syscall提取为一个不错的标准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. 另外,我在这里看到了很多#ifdef _WIN32 ,即使它只是stdio一个简单文件系统包装器,因此不确定stdio是否不会涵盖所有内容。

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. 环顾四周,我看到一些类似libuv的 “跨平台”库具有特定于Windows与unix的代码,因此不确定是否需要挂入“本机”(特定于操作系统的)功能以及何时/您通常需要这样做的地方。

Wondering first if glibc / stdio is a cross-platform abstraction around the syscalls and common other functions. 首先想知道glibc / stdio是否是围绕syscalls和常见其他功能的跨平台抽象。 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). stdio.hC11标准的一部分(请参阅n1570 ;您确实应该下载并阅读该内容)。 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. 如果限制自己仅使用该标准描述的功能(按照标准指定的方式),并且使用符合标准的实现(例如,编译器和C标准库实现),则应该是安全的。

However, many features (eg directories, symbolic links, file truncation) are outside of the C11 standard. 但是,许多功能(例如目录,符号链接,文件截断) 不在 C11标准范围内。 You might be interested by the POSIX standard. 您可能对POSIX标准感兴趣。

Sockets are not part of the C11 standard (but mostly POSIX, see this ). 套接字 不是 C11标准的一部分(但大多数是POSIX,请参见this )。

Some libraries (eg Glib ) try to define common abstractions for several OSes. 一些库(例如Glib )尝试为多个OS定义通用抽象。 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 , ....) (使用C ++,您还可以使用POCOBoostQt等 。)

BTW, Linux man pages (start with intro(2) & intro(3) ...) often mention the standard followed by most functions. 顺便说一句,Linux手册页(从intro(2)intro(3)开始 ...)经常提到大多数功能都遵循的标准。 For example chown(2) is defined in POSIX.1-2008. 例如,在POSIX.1-2008中定义了chown(2)

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). 例如,某些glibc版本(仍在使用中)可能无法使用C11 线程 (例如thrd_create )(因为它具有早于C11的pthreads(7) )。 Also, some libraries implement standard functions beyond what the standard requires (eg on Linux fopen(3) understands m in the mode string). 此外,某些库实现的标准功能超出了标准的要求(例如,在Linux上, fopen(3)在模式字符串中理解m )。

Remember the maxim: there is no portable code, only code that has been ported (to some specific system[s]). 请记住一个准则:没有可移植的代码,只有已移植的代码(已移植到某些特定系统)。 See also this . 另请参见

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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