1

出于某种原因,这行代码给了我很大的麻烦。

struct socketaddr_in clientaddr;

错误信息是:

tiny.c:23:24: error: storage size of ‘clientaddr’ isn’t known

如果我删除该行代码,我会收到以下错误消息:

s2s2@s2s2-ThinkPad-T61:~/Documents/Cprogramming/web_server$ make
gcc -std=gnu99 -O2 -lpthread -lrt -o server tiny.c csapp.c
/tmp/ccVxw07i.o: In function `Pthread_create':
csapp.c:(.text+0x7e5): undefined reference to `pthread_create'
/tmp/ccVxw07i.o: In function `Pthread_cancel':
csapp.c:(.text+0x805): undefined reference to `pthread_cancel'
/tmp/ccVxw07i.o: In function `Pthread_join':
csapp.c:(.text+0x825): undefined reference to `pthread_join'
/tmp/ccVxw07i.o: In function `Pthread_detach':
csapp.c:(.text+0x845): undefined reference to `pthread_detach'
/tmp/ccVxw07i.o: In function `Sem_init':
csapp.c:(.text+0x895): undefined reference to `sem_init'
/tmp/ccVxw07i.o: In function `P':
csapp.c:(.text+0x8b5): undefined reference to `sem_wait'
/tmp/ccVxw07i.o: In function `V':
csapp.c:(.text+0x8d5): undefined reference to `sem_post'
/tmp/ccVxw07i.o: In function `Pthread_once':
csapp.c:(.text+0x881): undefined reference to `pthread_once'
collect2: ld returned 1 exit status
make: *** [webServer-gcc] Error 1

以下是csapp.ccsapp.h文件的链接。

感谢所有的帮助。

4

1 回答 1

4

tiny.c:23:24:错误:“clientaddr”的存储大小未知

您需要像struct structname instancename在 C 中一样声明结构是有原因的 - 这样 C 编译器就知道要分配多少内存 - 以及可能如何对齐该数据等。

这是 C 编译器告诉你不存在这样的结构的方式socketaddr_in——它会是sockaddr_in.

以这种方式解决结构命名的常见方法是像这样定义它们:

typedef struct _struct_name
{
    /* ... */
} structname;

thenstructname可以用作没有struct限定符的类型。您也不必对结构的定义执行此操作,您可以稍后再执行。

因此,简短的回答是socketaddr_in在 POSIX 中不作为结构存在 - 它是sockaddr_in.

于 2012-06-16T12:57:16.477 回答