简体   繁体   English

如何在 Unix 网络编程中使用头文件

[英]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.我正在阅读经典书籍Unix网络编程,我遇到了一个关于编译的问题。
eg I can compile correctly the source code which is /home/song/unpv13e/tcpcliserv/tcpserv01.c.例如,我可以正确编译/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.在我将其复制到 /home/song/unpv13e/tcpcliserv/test.c 并制作 test.c 后,它提醒我错误,如下所示。

/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 /home/song/unpv13e/tcpcliserv/test.c:11: 未定义引用Socket' /home/song/unpv13e/tcpcliserv/test.c:18: undefined reference to Bind' /home/song/unpv13e/tcpcliserv/test .c:20: 未定义引用Listen' /home/song/unpv13e/tcpcliserv/test.c:21: undefined reference to Signal' /home/yuhongsong/unpv13e/tcpcliserv/test.c:31: 未定义引用Close' /home/song/unpv13e/tcpcliserv/test.c:24: undefined reference to Accept' 的Close' /home/song/unpv13e/tcpcliserv/test.c:24: undefined reference to /home/song/unpv13e/tcpcliserv/test.c:26: 未定义对Fork' /home/song/unpv13e/tcpcliserv/test.c:27: undefined reference to引用Fork' /home/song/unpv13e/tcpcliserv/test.c:27: undefined reference to Close' /home/song/unpv13e/tcpcliserv/test.c:28: 未定义引用 `str_echo' collect2: 错误: ld 返回 1 退出状态 make: *** [test] 错误 1

The function name begins with a capital letter is defined in "unp.h".以大写字母开头的函数名在“unp.h”中定义。 where do the problem arises,how to solve it,and how to use the header suitablely.问题出在哪里,如何解决,如何正确使用header。 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 需要一个静态库,其中函数例如接受来自。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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