Unix套接字示例,编译器找不到标头

问题描述 投票:0回答:1

平台:运行Ubuntu的Odroid N2

我正在尝试从此处的Unix套接字示例编译并链接客户端和侦听器-https://opensource.com/article/19/4/interprocess-communication-linux-networking;代码是:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include "sock.h"

void report(const char* msg, int terminate) {
  perror(msg);
  if (terminate) exit(-1); /* failure */
}

int main() {
  int fd = socket(AF_INET,     /* network versus AF_LOCAL */
                  SOCK_STREAM, /* reliable, bidirectional, arbitrary payload size */
                  0);          /* system picks underlying protocol (TCP) */
  if (fd < 0) report("socket", 1); /* terminate */

  /* bind the server's local address in memory */
  struct sockaddr_in saddr;
  memset(&saddr, 0, sizeof(saddr));          /* clear the bytes */
  saddr.sin_family = AF_INET;                /* versus AF_LOCAL */
  saddr.sin_addr.s_addr = htonl(INADDR_ANY); /* host-to-network endian */
  saddr.sin_port = htons(PortNumber);        /* for listening */

  if (bind(fd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0)
    report("bind", 1); /* terminate */

  /* listen to the socket */
  if (listen(fd, MaxConnects) < 0) /* listen for clients, up to MaxConnects */
    report("listen", 1); /* terminate */

  fprintf(stderr, "Listening on port %i for clients...\n", PortNumber);
  /* a server traditionally listens indefinitely */
  while (1) {
    struct sockaddr_in caddr; /* client address */
    int len = sizeof(caddr);  /* address length could change */

    int client_fd = accept(fd, (struct sockaddr*) &caddr, &len);  /* accept blocks */
    if (client_fd < 0) {
      report("accept", 0); /* don't terminate, though there's a problem */
      continue;
    }

    /* read from client */
    int i;
    for (i = 0; i < ConversationLen; i++) {
      char buffer[BuffSize + 1];
      memset(buffer, '\0', sizeof(buffer));
      int count = read(client_fd, buffer, sizeof(buffer));
      if (count > 0) {
        puts(buffer);
        write(client_fd, buffer, sizeof(buffer)); /* echo as confirmation */
      }
    }
    close(client_fd); /* break connection */
  }  /* while(1) */
  return 0;
}

我认为必要的头文件应该在系统上,因为作为练习,我在这里从源代码(使用本机编译过程)-https://wiki.odroid.com/odroid-n2/software/building_kernel重建了linux,并对其进行了编译,链接和引导。但是,在尝试编译套接字示例时,我缺少一些基本的东西:当我使用

gcc listener.c -o listener...它给出:

defs.h: No such file or directory
#include <defs.h>

到目前为止我尝试过的:

我已经安装了build-essential和linux-libc-dev。结果相同。作为我的问题的答案,我找到了安装内核头文件的指南。我尝试如下:

  • 当我使用uname -r时,我得到:4.9.196 +
  • 当我尝试安装特定于该版本的头文件时,如下所示:sudo apt install linux-headers-$(uname -r)...我得到:E: Unable to locate package linux-headers-4.9.196

  • Linux源代码及其所有包含文件在/ home / odroid / linux中;所以我想我可以通过执行以下操作来包含该库:gcc -I/home/odroid/linux/include listener.c -o listener

但是,当我这样做时,由于类型冲突,它给出了很多错误,但仍然找不到defs.h。

我的路径:/ usr / local / sbin:/ usr / local / bin:/ usr / sbin:/ usr / bin:/ sbin:/ bin:/ usr / games:/ usr / local / games:/ snap / bin

我想念什么?

c linux ubuntu compilation libraries
1个回答
0
投票

似乎defs.h不是内核头文件。它包含在示例中;参见上面的评论。

© www.soinside.com 2019 - 2024. All rights reserved.