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

sock = nl_socket_alloc();

上面的代码总是无法编译并出现以下错误:/home/micah/Documents/C++/Socket_fun/Socket_fun/src/main.cpp|5|error: 'sock' does not name a type

我从 libnl 示例中得到了这个,因为它不起作用,我想知道,正确的方法是什么?

4

1 回答 1

3

该代码必须在函数中,您不能只是在函数上下文之外开始调用函数:

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

另外,你用什么编译?我建议将其编译为 C,而不是 C++。

于 2011-12-30T02:16:54.140 回答