[英]Does C++ deprecate some parts of the Linux API?
I am in the middle of reading The Linux Programming Interface and Linux programming by examples .我正在阅读Linux 编程接口和Linux 示例编程。 Both are very good books and explain Linux API very well.
这两本书都是非常好的书,并且很好地解释了 Linux API。 But quite often I find myself thinking that in real world projects I would prefer C++ standard library, Boost or some other good C++ library (there are many well written and portable C++ libs) over C API whenever possible.
But quite often I find myself thinking that in real world projects I would prefer C++ standard library, Boost or some other good C++ library (there are many well written and portable C++ libs) over C API whenever possible. This naturally bags a question - why do I need to use Linux API directly when good C++ compiler and libs (Boost, TBB and etc) are available on target platforms?
这自然会引发一个问题——当目标平台上有良好的 C++ 编译器和库(Boost、TBB 等)时,为什么我需要直接使用 Linux API? I guess the same could be said about Windows API too, but I don't know much about Windows system programing.
我想 Windows API 也可以这么说,但我对 Windows 系统编程了解不多。
This is not new to C++.这对 C++ 来说并不新鲜。 In C, there have been two ways to open files for a long, long time:
在C中,打开文件的方式很久很久了:
// Only on POSIX
int fdes = open("file.txt", O_RDONLY);
Or:或者:
// Any hosted C environment, POSIX or otherwise
FILE *fp = fopen("file.txt", "rb");
So why would anyone ever use the POSIX-specific version?那么为什么有人会使用 POSIX 特定的版本呢? The answer is simple -- there are a large number of system calls which work with POSIX file descriptors.
答案很简单——有大量使用 POSIX 文件描述符的系统调用。 For example,
select
.例如,
select
。 You can also make things other than files, like pipes and sockets, and you can pass them to other processes.您还可以制作文件以外的东西,例如管道和 sockets,您可以将它们传递给其他进程。 There is a long tradition of using POSIX file descriptors, and we have a large number of books and references on how to do network programming with them.
使用 POSIX 文件描述符有着悠久的传统,我们有大量关于如何使用它们进行网络编程的书籍和参考资料。
So the trade-off is between the portable version and the powerful version.所以要在便携版和强大版之间进行权衡。 It always has been.
一直都是。
The other half of this is that time you work with files on Linux you are working with the POSIX interface.另一半是您在 Linux 上处理文件的时间,您正在使用 POSIX 接口。 Libraries just hide it from you.
图书馆只是对你隐藏它。 Boost uses it, the C runtime uses it, the JRE uses it, and GHC uses it.
Boost 使用它,C 运行时使用它,JRE 使用它,GHC 使用它。 Many (most?) language runtimes are written in C, and direct access to the system calls is preferred.
许多(大多数?)语言运行时都是用 C 编写的,最好直接访问系统调用。
You should use higher level API whenever possible.您应该尽可能使用更高级别的 API。 It's usually faster to work with and makes it easier to port your code to another platform.
它通常使用起来更快,并且更容易将您的代码移植到另一个平台。 However:
然而:
C++ standard is not published for a particular platform. C++ 标准未针对特定平台发布。 It is platform independent, So If you are going to use some platform features/functionality you will have to use platform dependent feature/functionality usually called
system
api.它与平台无关,因此如果您要使用某些平台特性/功能,则必须使用平台相关的特性/功能,通常称为
system
api。 So in that sense No the C++ library does not deprecate linux/windows api.所以从这个意义上说,没有 C++ 库不会弃用 linux/windows api。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.