C libraries outside the ANSI standard

Asked

Viewed 338 times

11

I am aware that, a C program in ANSI standard can be compiled both on Windows, both on Linux.

But when it comes to using sockets? That’s not part of the pattern ANSI C? Because when I use sockets in Windows, I have to use a library called "Winsock.h", on Linux, this library does not exist, and it uses another library called "socket.h". In this case, a program on Windows that uses the library "Winsock.h", error when compiled on Linux, and vise-versa. However, Sockets are not in default?

Another thing is the function fork(), this function exists only in Linux to make threads, I believe. Therefore this function, being non-existent in Windows, is outside the standard ANSI C?

  • 5

    Here’s a good question +1

1 answer

9


You got it right. The libraries of sockets are not part of ANSI. This standard defines only about the language and everything that is considered to standard language library that has only very basic operations, to do the minimum necessary and that does not usually engage with the operating system except in things very simple and standardized as access to the terminal and files, for example.

Even these accesses to the operating system are limited. When you want more sophisticated access you already need non-standard libraries that may or may not be available to multiple compilers, operating systems or even processors.

The same goes for functions that handle processes such as fork().

In these cases there are usually libraries that abstract the operating system. That is, they internally know how to work with each operating system where it will be used but externally it works the same way.

Examples:

All of these work with at least Windows sockets and POSIX sockets.

All allow working with threads on both POSIX and Windows systems. There is still a port of pthreads library, which is POSIX, for Windows.

  • Good answer! +1

  • So, you mean to standardize communication via socket, that is, to be compileable on multiple operating systems, I should use some of these libraries? If so, can you recommend some of these libraries to work on C? Both for the use of sockets and threads.

  • 1

    @Vynstus yes, that’s it. I recommended these. But I can’t tell you what’s best for your situation. Another possibility is program for each platform using its specific library, in some cases it may be better. It’s rare for someone to adopt this but it’s still a possibility.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.