Sümer Kolçak
Sümer Kolçak

Reputation: 171

How to fix GCC error : "asm/socket.h: No such file" on Sli-Taz Linux?

Story : I tried compiling the code below on Sli-Taz Linux 32 bit, as well as on 64 bit. In both cases I get the same gcc error. I searched for libc packages in the repo and could not find anything named libc-dev. If I can not find a solution, I may also try it with Alpine Linux.

Compile error for : gcc server.c

In file included from /usr/include/sys/socket.h:40:0,
                 from server.c:5:
/usr/include/bits/socket.h:381:24: fatal error: asm/socket.h: No such file or directory
compilation terminated.

Code for : server.c

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <err.h>

char response[] = "test";

int main()
{
  int one = 1, client_fd;
  struct sockaddr_in svr_addr, cli_addr;
  socklen_t sin_len = sizeof(cli_addr);

  int sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0)
    err(1, "can't open socket");

  setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int));

  int port = 90;
  svr_addr.sin_family = AF_INET;
  svr_addr.sin_addr.s_addr = INADDR_ANY;
  svr_addr.sin_port = htons(port);

  if (bind(sock, (struct sockaddr *) &svr_addr, sizeof(svr_addr)) == -1) {
    close(sock);
    err(1, "Can't bind");
  }

  listen(sock, 5);
  while (1) {
    client_fd = accept(sock, (struct sockaddr *) &cli_addr, &sin_len);
    printf("Client IP : [%s]\n", inet_ntoa(cli_addr.sin_addr));

    if (client_fd == -1) {
      perror("Can't accept");
      continue;
    }

    write(client_fd, response, sizeof(response) - 1); /*-1:'\0'*/
    close(client_fd);
  }

Description of the server.c : It is a program that will listen to port 90 and if a client tries to connect to port 90, it will respond to them with the message : test.

Upvotes: 1

Views: 1788

Answers (1)

Rup
Rup

Reputation: 34418

You need to install the linux-api-headers package.

/usr/include/asm/* are APIs exposed by the kernel and so are generally taken from the kernel source and updated as you upgrade the installed kernel. In SliTaz this is the linux-api-headers package; in Red Hat-derived systems this is the kernel-headers package, or Ubuntu it's linux-headers or linux-headers-generic, etc.

Upvotes: 3

Related Questions