Skip to content
Advertisement

Undefined reference to `getaddrinfo_a’

I get linker error while compiling a minimal program that uses getaddrinfo_a on Linux. The program in question

#define _GNU_SOURCE
#include <stdio.h>
#include <netdb.h>

int main(int argc, char **argv) {
  int err;
  err = getaddrinfo_a(0, NULL, 0, NULL);
}

Compiler output:

 $ cc -lanl minimal.c
/tmp/cc89BuFU.o: In function `main':
minimal.c:(.text+0x24): undefined reference to `getaddrinfo_a'
collect2: error: ld returned 1 exit status

 $ cc --version
cc (Ubuntu 4.9.2-10ubuntu13) 4.9.2

Advertisement

Answer

You are using command in wrong way. Use

cc  minimal.c -lanl

-lanl should come after not before file name.

gcc [options] [source files] [object files] [-Ldir] -llibname [-o outfile]

https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html

-l  It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.

According to standard also, order of library matters. Linker didn’t check for symbol from previously specified libraries. Ref

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement