【问题标题】:Unix Sockets example, compiler can't find headersUnix 套接字示例,编译器找不到标头
【发布时间】:2019-10-27 20:10:20
【问题描述】:

平台:运行 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;
}

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

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

我错过了什么?

【问题讨论】:

  • it gives - 不是更让步吗?比如哪个标题包含defs.h
  • Kamil - 谢谢,您的简单问题解决了!我一直在愚蠢地假设 defs.h 是内核头文件。它不是!它包含在我没有注意到的子目录中的示例中。我只需要将 defs.h 文件复制到源文件所在的目录中,然后将其 - #include &lt;defs.h&gt; 更改为 - #include "defs.h" - 即可编译。 (我确信有一个更正确的方法来引用子目录中的头文件,但现在我将使用它,直到我学会了正确的方法。)
  • 您所说的“内核标头”可能是标准系统搜索路径中可用的标头。您可以只 #include "subdirectory/defs.h" 或将子目录添加到包含搜索路径 gcc -I subdirectory
  • 谢谢,我明白了。

标签: c linux ubuntu compilation libraries


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-16
    • 2015-06-25
    • 1970-01-01
    • 2014-06-02
    相关资源
    最近更新 更多