1
#include <netlink/socket.h>
#include <netlink/netlink.h>
struct nl_sock *sock;

sock = nl_socket_alloc();

The above code always fails to compile with the following error: /home/micah/Documents/C++/Socket_fun/Socket_fun/src/main.cpp|5|error: ‘sock’ does not name a type

I got this from the libnl example, and as it doesn't work, I am wondering, what is the correct way to do this?

  • I suggest *libmnl* as an alternative, it is a lot simpler especially in one's learning phase of Netlink. – jørgensen Dec 30 '11 at 05:53
  • I may do that, but since I'm trying to use the inet_diag module, I've switched to the old socket() method, so that I can just copy, paste and adjust from the ss network utility – Thoughtful Dragon Jan 02 '12 at 20:15

1 Answers1

3

That code has to be in a function, you can't just start calling functions outside the context of a function:

int main()
{
    struct nl_sock *sock;
    sock = nl_socket_alloc();
}

Also, what are you compiling with? I would recommend compiling it as C, not C++.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • Yep, I just figured that out, It was bugging me something fierce. Thanks for the quick answer. And as for C, it has to be C++, it's a part of a C++ project. – Thoughtful Dragon Dec 30 '11 at 02:36