开发者

netdb.h not linking properly

开发者 https://www.devze.com 2023-04-08 07:38 出处:网络
I\'m trying to compile this program, as referenced in Beej\'s Guide to Network Programming on page 19.

I'm trying to compile this program, as referenced in Beej's Guide to Network Programming on page 19.

#include <stdio.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>

int main() {
    int status;
    struct addrinfo hints;
    struct addrinfo *servinfo;          /* Will point to the results */
    memset(&hints, 0, sizeof hints);    /* Make sure the struct is empty */
    hints.ai_family = AF_UNSPEC;        /* Don't care IPv4 or IPv6 */
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;

    if ((status = getaddrinfo(NULL, "3490", &hints, &servinfo)) != 0) {
        fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status));
        exit(1);
    }

    /* Servinfo now points to a linked list of 1 or more struct addrinfos
        ... do everything until you don't need servinfo anymore .... */

    freeaddrinfo(servinfo); /* Free the linked-list */

    return 0;
}    

Among other errors, I see

../main.c:8:18: erro开发者_StackOverflow社区r: storage size of ‘hints’ isn’t known
../main.c:13:19: error: ‘AI_PASSIVE’ undeclared (first use in this function)
../main.c:16:3: warning: implicit declaration of function ‘gai_strerror’

It appears that gcc isn't linking with netdb.h. Eclipse, the IDE that I'm using to build this, has no trouble finding the file. Here's the compiler command:

gcc -O0 -g3 -pedantic -Wall -c -fmessage-length=0 -ansi -MMD -MP -MF"main.d" -MT"main.d" -o"main.o" "../main.c"

Adding -lnetdb doesn't resolve the issue. Also...

~> find /usr/include/ -name netdb.h
/usr/include/bits/netdb.h
/usr/include/gssrpc/netdb.h
/usr/include/netdb.h
/usr/include/rpc/netdb.h

I think these files came preinstalled on my openSUSE host. Why doesn't gcc detect netdb.h? Or am I drawing the wrong conclusion?


../main.c:8:18: error: storage size of ‘hints’ isn’t known
../main.c:13:19: error: ‘AI_PASSIVE’ undeclared (first use in this function)
../main.c:16:3: warning: implicit declaration of function ‘gai_strerror’

It appears that gcc isn't linking with netdb.h....

These are not linking errors, and you don't need a netdb shared object file (there is no such beast; netdb.h simply defines data structures and macros for use in your code).

These are compiler errors: gcc complaining because you're using names that it doesn't recognize (AI_PASSIVE) and data types for which the structure is unknown (struct addrinfo).

The code as you've presented it appears to be correct, and addrinfo is defined in /usr/include/netdb.h. What happens if you compile it like this:

gcc -c main.c

Do you still get the same behavior? If so, take a look at the output of:

gcc -E main.c

This generates a preprocessed version of the code, with all of the #include statements replaced by their actual content. You should be able to grep through this and see if the compiler is actually getting /usr/include/netdb.h if if it's finding something else:

$ gcc -E foo.c | grep netdb.h | awk '{print $3}' | sort -u

Which on my system yields:

"/usr/include/bits/netdb.h"
"/usr/include/netdb.h"
"/usr/include/rpc/netdb.h"

When you add -ansi to the command line, your are changing the way gcc behaves in ways that will break the Linux kernel and many system header files. The addrinfo definition in netdb.h is protected like this:

#ifdef  __USE_POSIX
/* Structure to contain information about address of a service provider.  */
struct addrinfo
{
  int ai_flags;                 /* Input flags.  */
  int ai_family;                /* Protocol family for socket.  */
  int ai_socktype;              /* Socket type.  */
  int ai_protocol;              /* Protocol for socket.  */
  socklen_t ai_addrlen;         /* Length of socket address.  */
  struct sockaddr *ai_addr;     /* Socket address for socket.  */
  char *ai_canonname;           /* Canonical name for service location.  */
  struct addrinfo *ai_next;     /* Pointer to next in list.  */
};

// ...other stuff...
#endif

When you run gcc with the -ansi flag, this undefines the __USE_POSIX macro, because things protected by this may not be strictly ANSI compliant. You can see the difference if you compare this:

gcc -E /usr/include/netdb.h

With this:

gcc -E -ansi /usr/include/netdb.h

Only the former contains the addrinfo structure.


add at the top of your file

#define _XOPEN_SOURCE 600


Linking doesn't attach a header file to the program, pre-processing handles header files.

Linking attaches shared object libraries (look in /usr/lib) to compiled objects. Sometimes it is not sufficent to just add "-lnetdb" as the library might not be in the linker path. In that case, you need to use a -L(path) to add a path entry so the directive "-lnetdb" can find the correct netdb.so file.


Use #include <netinet/in.h> ....along with that.....and in compilation (ubuntu)....

gcc server/client -o server/client.c -lpthread

(here lpthread is a linki processing thread)...which solves your problem. :-D

0

精彩评论

暂无评论...
验证码 换一张
取 消