cgccc-ares

gcc error: `undefined symbol: ares_dns_parse` when using c-ares library


I was trying to build a C program that uses the ares_dns_parse function from the c-ares library. I have decided to install the latest v1.31.0 according to the installation steps here: https://github.com/c-ares/c-ares/blob/main/INSTALL.md, as follows:

$ wget https://github.com/c-ares/c-ares/releases/download/v1.31.0/c-ares-1.31.0.tar.gz
$ tar -xvf c-ares-1.31.0.tar.gz
$ cd c-ares-1.31.0
$ ./configure
$ make 
$ sudo make install

I have set up a simple C program as follows:

ares_test.c

#include <stdio.h>
#include <ares.h>


int main()
{
  char buf[1000] = "test buffer";
  size_t buf_len = sizeof(buf);

  ares_status_t ares_res;
  ares_dns_record_t *dnsrec;

  ares_res = ares_dns_parse((const unsigned char *)buf, buf_len, 0, &dnsrec);

  if (ares_res != ARES_SUCCESS){

          perror("ares_dns_parse() error: \n");
  }

 return 0;
}

Compiling with gcc with -v verbose flag:

gcc ares_test.c -lcares -I /usr/local/include -o ares_test -v

Output:

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:hsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 9.4.0-1ubuntu1~20.04.2' --with-bugurl=file:///usr/share/doc/gcc-9/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,gm2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-9 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-9-9QDOt0/gcc-9-9.4.0/debian/tmp-nvptx/usr,hsa --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 9.4.0 (Ubuntu 9.4.0-1ubuntu1~20.04.2)
COLLECT_GCC_OPTIONS='-I' '/usr/local/include' '-o' 'ares_test' '-v' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/9/cc1 -quiet -v -I /usr/local/include -imultiarch x86_64-linux-gnu ares_test.c -quiet -dumpbase ares_test.c -mtune=generic -march=x86-64 -auxbase ares_test -version -fasynchronous-unwind-tables -fstack-protector-strong -Wformat -Wformat-security -fstack-clash-protection -fcf-protection -o /tmp/ccpRO6c2.s
GNU C17 (Ubuntu 9.4.0-1ubuntu1~20.04.2) version 9.4.0 (x86_64-linux-gnu)
        compiled by GNU C version 9.4.0, GMP version 6.2.0, MPFR version 4.0.2, MPC version 1.1.0, isl version isl-0.22.1-GMP

GGC heuristics: --param ggc-min-expand=95 --param ggc-min-heapsize=123533
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/9/include-fixed"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/9/../../../../x86_64-linux-gnu/include"
ignoring duplicate directory "/usr/local/include"
  as it is a non-system directory that duplicates a system directory
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-linux-gnu/9/include
 /usr/local/include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
GNU C17 (Ubuntu 9.4.0-1ubuntu1~20.04.2) version 9.4.0 (x86_64-linux-gnu)
        compiled by GNU C version 9.4.0, GMP version 6.2.0, MPFR version 4.0.2, MPC version 1.1.0, isl version isl-0.22.1-GMP

GGC heuristics: --param ggc-min-expand=95 --param ggc-min-heapsize=123533
Compiler executable checksum: 01da938ff5dc2163489aa33cb3b747a7
COLLECT_GCC_OPTIONS='-I' '/usr/local/include' '-o' 'ares_test' '-v' '-mtune=generic' '-march=x86-64'
 as -v -I /usr/local/include --64 -o /tmp/cc8nt9W0.o /tmp/ccpRO6c2.s
GNU assembler version 2.34 (x86_64-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.34
COMPILER_PATH=/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-linux-gnu/9/:/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/:/usr/lib/gcc/x86_64-linux-gnu/9/../../../../lib/:/lib/x86_64-linux-gnu/:/lib/../lib/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-linux-gnu/9/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-I' '/usr/local/include' '-o' 'ares_test' '-v' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-linux-gnu/9/collect2 -plugin /usr/lib/gcc/x86_64-linux-gnu/9/liblto_plugin.so -plugin-opt=/usr/lib/gcc/x86_64-linux-gnu/9/lto-wrapper -plugin-opt=-fresolution=/tmp/ccDBFgG1.res -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lgcc_s --build-id --eh-frame-hdr -m elf_x86_64 --hash-style=gnu --as-needed -dynamic-linker /lib64/ld-linux-x86-64.so.2 -pie -z now -z relro -o ares_test /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/crti.o /usr/lib/gcc/x86_64-linux-gnu/9/crtbeginS.o -L/usr/lib/gcc/x86_64-linux-gnu/9 -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu -L/usr/lib/gcc/x86_64-linux-gnu/9/../../../../lib -L/lib/x86_64-linux-gnu -L/lib/../lib -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib -L/usr/lib/gcc/x86_64-linux-gnu/9/../../.. /tmp/cc8nt9W0.o -lcares -lgcc --push-state --as-needed -lgcc_s --pop-state -lc -lgcc --push-state --as-needed -lgcc_s --pop-state /usr/lib/gcc/x86_64-linux-gnu/9/crtendS.o /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/crtn.o
COLLECT_GCC_OPTIONS='-I' '/usr/local/include' '-o' 'ares_test' '-v' '-mtune=generic' '-march=x86-64'

Running binary:

$ ./ares_test

Output:

./ares_test: symbol lookup error: ./ares_test: undefined symbol: ares_dns_parse

However, with the exact same C program, just with the last argument (&dnsrec) to the ares_dns_parse() function removed, and compiled the exact same way as above (minus the -v flag), the following error is shown:

ares_test.c: In function ‘main’:
ares_test.c:13:14: error: too few arguments to function ‘ares_dns_parse’
   13 |   ares_res = ares_dns_parse((const unsigned char *)buf, buf_len, 0);
      |              ^~~~~~~~~~~~~~
In file included from /usr/local/include/ares.h:417,
                 from ares_test.c:2:
/usr/local/include/ares_dns_record.h:1008:28: note: declared here
 1008 | CARES_EXTERN ares_status_t ares_dns_parse(const unsigned char *buf,

This tells me that gcc is able to find the ares_dns_parse function in /usr/local/include/ares_dns_record.h.

Note: I am running this from an Ubuntu machine.

Are there any additional linking required, or any steps I'm missing? Any help would be appreciated, thank you.


Solution

  • With help from @n. m. could be an AI, the solution is to simply add an extra flag to the gcc compilation command: -Wl,-rpath=/usr/local/lib:

    $ gcc ares_test.c -lcares -Wl,-rpath=/usr/local/lib -o ares_test
    

    Well, but the value given to -rpath might not always be /usr/local/lib though, but depends on the argument you provided to the --prefix flag for the ./configure command.

    For example if the value is /usr/randompath:

    $ ./configure --prexix=/usr/randompath
    

    Then, the additional flag would be -Wl,-rpath=/usr/randompath/lib

    $ gcc ares_test.c -lcares -Wl,-rpath=/usr/randompath/lib -o ares_test
    

    In fact, the -I /usr/local/include is not even required.