Search code examples
c++clinuxmanpagenetlink

Why does Linux NETLINK man page provide C++ examples but not C?


I dealt with API and examine its man pages netlink(3) and netlink(7).
Suddenly I faced with such construction:

struct msghdr msg;

msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };

I tried it in C, but it gives the error:

error: expected expression before ‘{’ token

Apparently it's (I don't know what standard).
Of course it's obvious that Netlink is an API and has no particular language binding. But this is C implementation and all MAN pages I've seen about C API have pure C examples. Why there is no note about the language used in examples? What is this practice for and why is this not f.e. on Python or whatever?


UPD: I don't think it's a typo or an unintentional error in MAN page. There are some other places with this C++ feature using, e.g.:

struct iovec iov = { buf, sizeof(buf) };
struct sockaddr_nl sa;
struct msghdr msg;
struct nlmsghdr *nh;

msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
len = recvmsg(fd, &msg, 0);

for (nh = (struct nlmsghdr *) buf; NLMSG_OK (nh, len);
     nh = NLMSG_NEXT (nh, len)) {
...

It looks like a conscious choice of C++.


Solution

  • Why does Linux man page provides C++ examples but not C?

    Linux man pages provide C examples. They don't provide C++ examples.

    I tried it in C, but it gives the error:

    It's an error in the documentation. It's meant to be:

    struct msghdr msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };
    

    I believe you could post a bug report on linux-man mailing list.

    Why there is no note about the language used in examples?

    As stated on the project homepage, Linux man pages document the Linux kernel and the C library interface. The Linux kernel is written in C (primarily, there is some assembly, etc.). As such, Linux man pages serve examples in the C programming language.

    What is this practice for and why is this not f.e. on Python or whatever?

    The practice is that C language is used in the development of Linux. It's not Python or any other language, because the Linux kernel is written in the C programming language. The Linux kernel is written in the C programming language because the creator of the Linux kernel, Linus Torvalds, made a decision a very long time ago to write the kernel in the C programming language.

    It's easiest for kernel developers to distribute kernel interface API for user-space programs in the language the kernel is written itself, so they don't have to rewrite it all for user-space programs in another programming language. They also can use for example the same header file that user-space uses in the kernel itself. That, for example, reduces bugs and workload, etc. As such, it's easiest for kernel developers to provide the documentation using examples written in the C programming language.