2

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++.

red0ct
  • 4,840
  • 3
  • 17
  • 44
z0lupka
  • 236
  • 4
  • 19
  • 3
    The question is taking one tiny example and asserting it applies more widely.. the question really is "Why does *this specific [netlink] man page* provide an example which does not compile locally as C?" (File a bug report, perhaps.) – user2864740 Dec 20 '19 at 23:00
  • And there *is* an implicit language "binding", being merely exposed as a particular language with a particular set calling conventions - be it C or C++ or whatnot. C (or even C++ as the case may apply) happen to be the *native* "binding" of the library. Just as a Python module documentation.. – user2864740 Dec 20 '19 at 23:02
  • @z0lupka No. That's a large leap to make from an *example that merely fails to compile in C*.. C++ has quite a bit of just-work-like-C compatibility (ie. using `printf` in C++ doesn't make `printf` a C++ function). Maybe it's a typo that went unnoticed.. – user2864740 Dec 20 '19 at 23:06
  • @Eljay I know C pretty well, but thanks. – z0lupka Dec 20 '19 at 23:08
  • @user2864740 You edited your comment. So what about binding? C++ is implicit *native* binding here? Strongly doubt. – z0lupka Dec 20 '19 at 23:09
  • @z0lupka See C++ name mangling and ABI, etc. Have fun using an arbitrary C++ library in C. The "binding" of a C++ library is for C++ and to use it in C requires significant hurdles (see SWIG, C-friendly [read: not C++] API exposure, etc.). – user2864740 Dec 20 '19 at 23:09
  • `Apparently it's c++` How do you know it's C++? Do you have any reference for that? – KamilCuk Dec 20 '19 at 23:11
  • 2
    @Eljay You can initialize, but you can't assign like that. – Barmar Dec 20 '19 at 23:12
  • @user2864740 What do you want to prove to me? That writing C++ examples in the MAN page of C library is normal? Btw without any notice. I still don't think so.. – z0lupka Dec 20 '19 at 23:13
  • 2
    I think this is just an anomaly. Normally Unix man pages use C syntax, I think the author of this one just screwed up. – Barmar Dec 20 '19 at 23:14
  • @Barmar • Yep, you are correct. I've not done C in a while. I've deleted my misleading / incorrect comment. – Eljay Dec 20 '19 at 23:15
  • 2
    He probably meant to write `struct msghdr msg = { ... };`, but for some reason he split them up into separate statements. – Barmar Dec 20 '19 at 23:15
  • @KamilCuk *"How do you know it's C++?"* - **any other options?** – z0lupka Dec 20 '19 at 23:16
  • Yes, it's a bug. – KamilCuk Dec 20 '19 at 23:17
  • 2
    Example compiles in `C++`, but is not pure `C++` code (it would look a bit different). I would say this is `C` code with one small C++ feature. Here is [equivalent examle](https://www.godbolt.org/z/PJ7_3X). Basically error in documentation which is so painless that it is ignore by everyone. – Marek R Dec 20 '19 at 23:37
  • I'm voting to close this question as off-topic because the assertion is false. – Rob Dec 21 '19 at 01:16
  • I'm voting to close this question as off-topic because it asks about offsite documentation, which is similar to asking for [support for offsite resources](https://meta.stackoverflow.com/questions/255745). – JaMiT Dec 21 '19 at 05:13
  • `There are some other places with this C++ feature using` - you posted the same code `msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 };`. Where is that other place exactly where C++ feature is used? – KamilCuk Dec 21 '19 at 12:47
  • @KamilCuk There are two different places with `msg` initialization. But OK, perhaps it just someone's negligence.. – z0lupka Dec 21 '19 at 15:08

1 Answers1

6

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 1
    This specific example, `struct msghdr` and `sendmsg()`, and the other APIs used are from userspace C libraries. So it really not so much in C because the kernel is in C, but because the userspace libraries being used are C libraries. Those userspace libraries and headers being in C is of course related to the kernel being written in C. – TrentP Dec 21 '19 at 01:16
  • Even after the correction it's still erroneous. The order of members (and absence of other intervening members) in `msghdr` is unspecified. You need to either use designated initializers or assignment of members to use `msghdr` correctly. – R.. GitHub STOP HELPING ICE Dec 21 '19 at 04:06
  • @KamilCuk Thank you for clarifying! But the statement about the error is somewhat doubtful.. Updated my question. – z0lupka Dec 21 '19 at 12:20
  • I don't understand, why would you doubt it? Linux man pages _by intent_ don't want to provide C++ api. The headers are written in C, `extern "C"` can be missing in netlink headers, `recvmsg` (or any other function exported by netlink) function is not mangled according to C++. – KamilCuk Dec 21 '19 at 12:41