linker error for ns_initparse
Asked Answered
E

3

6

Here the code

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <resolv.h>

int main (int argc, char *argv[])
{
    u_char nsbuf[4096];
    char dispbuf[4096];
    ns_msg msg;
    ns_rr rr;
    int i, j, l;

    if (argc < 2) {
        printf ("Usage: %s <domain>[...]\n", argv[0]);
        exit (1);
    }

    for (i = 1; i < argc; i++) {
        l = res_query (argv[i], ns_c_any, ns_t_mx, nsbuf, sizeof (nsbuf));
        if (l < 0) {
            perror (argv[i]);
        } else {
#ifdef USE_PQUERY

/* this will give lots of detailed info on the request and reply */

            res_pquery (&_res, nsbuf, l, stdout);
#else

/* just grab the MX answer info */

            ns_initparse (nsbuf, l, &msg);
            printf ("%s :\n", argv[i]);
            l = ns_msg_count (msg, ns_s_an);
            for (j = 0; j < l; j++) {
                ns_parserr (&msg, ns_s_an, j, &rr);
                ns_sprintrr (&msg, &rr, NULL, NULL, dispbuf, sizeof (dispbuf));
                printf ("%s\n", dispbuf);
            }
#endif
        }
    }

    exit (0);
}

I compile it as

gcc dns.c -lresolv

and I get the following linker error

In function main': dns.c:(.text+0xd5): undefined reference to__ns_initparse' dns.c:(.text+0x130): undefined reference to __ns_parserr' dns.c:(.text+0x16a): undefined reference to__ns_sprintrr'

Help

Educable answered 6/11, 2009 at 16:54 Comment(1)
From some quick googling, it appears that these functions are considered internal and therefore deliberately not exported. They may be present in the static library. This is also probably version-dependent; there have been plenty of versions of the BIND API. jira.secondlife.com/browse/VWR-1598 and newsgroups-index.com/group/…Shuck
G
5

Linking with the resolver library helped fixed his issue for me, I added -lresolv to my compilation command.

Grebe answered 19/11, 2017 at 19:39 Comment(2)
I have the same error as OP when compiling, but adding -lresolv did not solve it for me. My command is g++ -o test -x c++ -std=c++20 -lresolv ./test-resolv.cpp, any idea what I'm doing wrong?Mickel
NM, I Solved my problem, it was pretty silly: the -l commands must come after the source reference (something about the order in which the linked loads libraries from -l to resolve references).Mickel
D
1

I believe you need to add

#include <arpa/nameser.h> 

to your file

Dinorahdinosaur answered 24/10, 2015 at 23:7 Comment(1)
This will not work since a header cannot define a reference.Slush
P
0

I don't have enough reputation to reply to sdgfsdh, but Darakian is right, adding arpa/namesrv.h will help. Many implementations of libresolv use private symbols and make those symbols available using macros.

For example, macOS' nameser.h contains:

#define ns_initparse        res_9_ns_initparse
Persist answered 28/4, 2024 at 11:33 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.