Use of resolv.h

有些话、适合烂在心里 提交于 2019-12-10 11:58:46

问题


I'm trying to find out my DNS server address by reading it from resolv.h's _res struct. According to man 3 resolver the setup code should be.

#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>

extern struct state _res;

and then I just read out whatever I need. My problem is that trying to compile this I get

resolver.c:5:21: error: conflicting types for '__res_state'
extern struct state _res;
                    ^
/usr/include/resolv.h:251:16: note: expanded from macro '_res'
#define _res (*__res_state())
               ^
/usr/include/resolv.h:249:28: note: previous declaration is here
extern struct __res_state *__res_state(void) __attribute__ ((__const__));
                           ^
1 error generated.

by clang.

What am I doing wrong?


回答1:


You shouldn't declare _res yourself - resolv.h includes the right declaration (despite what the man page implies).




回答2:


#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>


int main() {

    // call this first
    res_init();

    // do something with this list it contains list of dns servers
    _res.nsaddr_list[0];

}


来源:https://stackoverflow.com/questions/11371965/use-of-resolv-h

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!