简体   繁体   中英

How to use headers in Unix Network Programming

I was going through the classic book Unix Network Programming ,I meet a problem which is about compile.
eg I can compile correctly the source code which is /home/song/unpv13e/tcpcliserv/tcpserv01.c. after I copy it to /home/song/unpv13e/tcpcliserv/test.c and make test.c, it reminds me error as the below shows.

/tmp/ccI4xfzr.o: In function `main':

/home/song/unpv13e/tcpcliserv/test.c:11: undefined reference to Socket' /home/song/unpv13e/tcpcliserv/test.c:18: undefined reference to Bind' /home/song/unpv13e/tcpcliserv/test.c:20: undefined reference to Listen' /home/song/unpv13e/tcpcliserv/test.c:21: undefined reference to Signal' /home/yuhongsong/unpv13e/tcpcliserv/test.c:31: undefined reference to Close' /home/song/unpv13e/tcpcliserv/test.c:24: undefined reference to Accept' /home/song/unpv13e/tcpcliserv/test.c:26: undefined reference to Fork' /home/song/unpv13e/tcpcliserv/test.c:27: undefined reference to Close' /home/song/unpv13e/tcpcliserv/test.c:28: undefined reference to `str_echo' collect2: error: ld returned 1 exit status make: *** [test] Error 1

The function name begins with a capital letter is defined in "unp.h". where do the problem arises,how to solve it,and how to use the header suitablely. the source code shows as below:

#include "unp.h"
#include "sigchldwait.c"

int  main(int argc, char **argv)
{
    int                 listenfd, connfd;
    pid_t               childpid;
    socklen_t           clilen;
    struct sockaddr_in  cliaddr, servaddr;


    listenfd = Socket(AF_INET, SOCK_STREAM, 0);

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family      = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port        = htons(SERV_PORT);

    Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));

    Listen(listenfd, LISTENQ);
        Signal(SIGCHLD, sig_chld);
    for ( ; ; ) {
        clilen = sizeof(cliaddr);
        connfd = Accept(listenfd, (SA *) &cliaddr, &clilen);

        if ( (childpid = Fork()) == 0) {    /* child process */
            Close(listenfd);    /* close listening socket */
            str_echo(connfd);   /* process the request */
            exit(0);
        }
        Close(connfd);          /* parent closes connected socket */
    }
}

This is a linkage problem, you nee to link against this lib.

You probably compiled correctly against this lib (using the header) but you did not give the lib at lib time.

It si possible that you even need to compile it if you get it from sources.

问题是默认 make test.c 等于gcc -I../lib -g -O2 -D_REENTRANT -Wall -c -o test.o test.c .但是 test.c 需要一个静态库,其中函数例如接受来自。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM