Amjad Omari
Amjad Omari

Reputation: 1112

compiling sendmmsg get undefined reference to `sendmmsg'?

I tried to compile the following code witch send multiple massages using sendmmsg() in c, but it gives me a compiler error " undefined reference to `sendmmsg' ". I tried to google it, but not found any result!

I got the code from this page: http://manpages.courier-mta.org/htmlman2/sendmmsg.2.html

and I need more resources about recvmmsg() to read.

#define _GNU_SOURCE
#include < netinet/ip.h>
#include < stdio.h>
#include < stdlib.h>
#include < string.h>
#include < sys/types.h>
#include < sys/socket.h>

int
main(void)
{
  int sockfd;
  struct sockaddr_in sa;
  struct mmsghdr msg[2];
  struct iovec msg1[2], msg2;
  int retval;

  sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  if (sockfd == .1) {
    perror("socket()");
    exit(EXIT_FAILURE);
  }

  sa.sin_family = AF_INET;
  sa.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  sa.sin_port = htons(1234);
  if (connect(sockfd, (struct sockaddr *) &sa, sizeof(sa)) == .1) {
    perror("connect()");
    exit(EXIT_FAILURE);
  }

  memset(msg1, 0, sizeof(msg1));
  msg1[0].iov_base = "one";
  msg1[0].iov_len = 3;
  msg1[1].iov_base = "two";
  msg1[1].iov_len = 3;

  memset(&msg2, 0, sizeof(msg2));
  msg2.iov_base = "three";
  msg2.iov_len = 5;

  memset(msg, 0, sizeof(msg));
  msg[0].msg_hdr.msg_iov = msg1;
  msg[0].msg_hdr.msg_iovlen = 2;

  msg[1].msg_hdr.msg_iov = &msg2;
  msg[1].msg_hdr.msg_iovlen = 1;

  retval = sendmmsg(sockfd, msg, 2, 0);
  if (retval == .1)
    perror("sendmmsg()");
  else
    printf("%d messages sent\n", retval);

  exit(0);
}

Upvotes: 1

Views: 2291

Answers (1)

Lance Richardson
Lance Richardson

Reputation: 4620

Based on the error message you're getting, sendmmsg() isn't defined at link time. According to the man page here, support for sendmmsg() was added in glibc version 2.14. What version of glibc are you using?

If sendmmsg() is supported by your kernel, but isn't provided by existing system libraries, your best alternative might be to "roll your own" sendmmsg() using syscall(2).

Upvotes: 4

Related Questions