System library versus standard library

In summary: MAC.In summary, the conversation discusses the differences between using stdio.h and fcntl.h for file input/output in C. It is mentioned that fcntl.h is only for Linux while stdio.h is for any operating system that runs C. The discussion also touches on the possibility of writing portable code using only standard libraries but it is noted that the point of using Windows is not to write command line programs. Additionally, there is mention of third party libraries that attempt to provide cross-platform windowing and graphics capabilities, but they have not gained popularity.
  • #1
RedX
970
3
Is fcntl.h more fundamental than stdio.h?

Using just stdio.h, I can define a FILE type, and use an fopen() command to open a file and return the address of the file into the FILE variable.

Using fcntl.h, I use open() instead of fopen(), and instead of returning a FILE type, open() returns an integer that's called a file descriptor.

fcntl.h I believe is only for Linux, while stdio.h is for any operating system that runs C.

Would I be correct in saying that each operating system has to write their own stdio.h that meets C standards, and Linux uses fcntl.h to do that, while Windows uses windows.h?

So if I look at stdio.h for Linux, it would define fopen() in terms of open() from fcntl.h, and similarly with Windows except using Window's version of an open() command to define fopen()?
 
Technology news on Phys.org
  • #2
Back in the day with DOS, you had several file methods, but streams were the standard. It was a debate for a while and streams won. You will notice that stdio has a lot of macros. That is because everything is a file stream.

Windows eventually went away from streams, and everything was a handle. But for a long time windows supported the all of the DOS and low level IO methods.

It has actually evolved beyond Handles in windows, and now security at both the object level and the driver level is being emphasized. You cannot easily access the disk at the sector level.

But yes you have the concept. Somewhere every iteration of C for each operating system has it own core internals and the standard libraries are the common interface.

Keep in mind that it is impossible to write portable code in C. Unix and Windows are too far apart. And so is the MAC. Though basic file IO is not something that really needs to be hard.

Some of the benefits to not using the stdio is when you are taking advantage of tasking and asynchrounous file IO. But Unix really does not emphasize that, OS/2 and Windows do, though even that has evolved a bit.
 
  • #3
The whole idea and beauty of streams is that you can capture and redirect streams. That is where stdin, stdout, stderr come into play. In Unix and Dos for that matter you create small programs and you link them together on the command line.
 
  • #4
airborne18 said:
But yes you have the concept. Somewhere every iteration of C for each operating system has it own core internals and the standard libraries are the common interface.

Keep in mind that it is impossible to write portable code in C. Unix and Windows are too far apart. And so is the MAC. Though basic file IO is not something that really needs to be hard.

I'm slightly confused here. If your program only uses standard libraries, then it ought to be portable right? The pre-processor will look for <stdio.h> wherever your operating system stores it under its file management, find it and then paste it into your program. It is not the programmer's responsibility to make sure that the version of <stdio.h> for a particular operating system will produce the correct results.

So it seems to me that source code should be portable if you stick to C-standard libraries.

Executable code might not be portable though, since different operating systems have different executable formats.

But before compiling and linking, it seems to me that <stdio.h> should be <stdio.h> functionally, although the internals would be different and particular to each OS, because the makers of the OS made it that way so people would use their OS.
 
  • #5
It is portable, but are you going to write command line programs your entire life? Yes you can write a command line windows program that will also be a command line program in Unix.

Yes the source code is portable, but not very useful. The point in windows is not to write command line programs. They exist for very specific purposes, but you want to write Windows based programs.

So yes, but the C standard libraries are not flashy programs that people actually write for commerical purposes. That is what I mean.
 
  • #6
airborne18 said:
It is portable, but are you going to write command line programs your entire life? Yes you can write a command line windows program that will also be a command line program in Unix.

Yes the source code is portable, but not very useful. The point in windows is not to write command line programs. They exist for very specific purposes, but you want to write Windows based programs.

So yes, but the C standard libraries are not flashy programs that people actually write for commerical purposes. That is what I mean.

But in principle, can one construct flashy functions built out of only the standard C libraries, and then by using only those functions, make it portable? Or is the only way to make flashy functions is to use OS-specific functions provided by the OS-manufacturers?

I vaguely recall that in C there is a standard library called graphics.h, and since it is a standard library it should be available in Mac and Unix too.

I guess what I'm trying to say is that with computers, you can sort of get self-assembly. Using already existing functions, you build a function slightly more flashy. Once you have these functions, you use them to build something more flashy. But the reward if you keep doing this is something that is portable, since it is built out of only the standard C library. But I guess then you will be reinventing the wheel, and you might as well just create your own operating system using standard C.
 
Last edited:
  • #7
There are no flashy windowing in the standard libraries ( ANSI ). Throughout the years there have been many third party libraries that have attempted to build cross platform windowing and graphics libraries. For C++ you will find them. But they have never caught on, simply because each paradigm is different enough that you give up too much naitive functionality.


graphics.h should not be a std library.. not ANSI. Dos had it, but I do not believe that windows has it.

Windows uses the GDI for graphics.

The best you can do with the standard libs is a text based windowing system that uses Escape sequences, and that is more trouble than it is worth.
 
  • #8
This issue is the reason people like the java concept, and the .net type frameworks where there is a psuedo compiled opcode chunk that is platform indepdent.
 

FAQ: System library versus standard library

What is the difference between a system library and a standard library?

A system library is a collection of pre-written code that is specific to a particular operating system, while a standard library is a collection of code that is available for use across different operating systems. System libraries are often included with the operating system, while standard libraries may need to be downloaded or installed separately.

Which one should I use for my project?

It depends on the specific needs of your project. If your project is only going to be used on a specific operating system, then using the corresponding system library may be more efficient. However, if you want your project to be portable and run on multiple operating systems, then using a standard library would be a better choice.

What are some examples of system libraries?

Some examples of system libraries include the Windows API for Windows operating systems, the Cocoa framework for macOS, and the glibc for Linux.

And what are some examples of standard libraries?

Some examples of standard libraries include the C++ Standard Library, the Java Standard Library, and the Python Standard Library. These libraries are designed to be platform-independent and can be used on multiple operating systems.

Can I use both a system library and a standard library in my project?

Yes, it is possible to use both types of libraries in a project. For example, you may use a system library for specific operating system functions and a standard library for more general functions that need to be portable. However, it is important to ensure compatibility between the libraries and avoid conflicts.

Back
Top