我正在使用pthread库创建两个线程。我使用两个队列在两个线程(生产者-消费者)之间传递数据,因此希望有一个互斥来同步队列中线程的推送。
但是我得到了一个编译错误,如下:
$ gcc simple-tun.c simple-tun -lpthread
simple-tun.c: In function ‘new_queue’:
simple-tun.c:920:13: error: expected expression before ‘{’ token我得到错误的函数是:
908 struct queue * new_queue () {
909
910 struct queue * q;
911 q = (struct queue *) malloc (sizeof(struct queue));
912
913 if (q == NULL)
914 return NULL;
915
916
917 q->head = NULL;
918 q->tail = NULL;
919 q->is_empty = 1;
920 q->mutex = PTHREAD_MUTEX_INITIALIZER;
921
922 return q;
923 }结构队列为:
struct queue {
80 struct node * head;
81 struct node * tail;
82 int is_empty;
83 pthread_mutex_t mutex;
84 };如果我注释掉了920行,链接器就会开始给出“多个声明错误”
$ gcc simple-tun.c simple-tun -lpthread
simple-tun: In function `settun':
(.text+0x2b7): multiple definition of `settun'
/tmp/cc5Ms4xP.o:simple-tun.c:(.text+0x1cb): first defined here
simple-tun: In function `_fini':
(.fini+0x0): multiple definition of `_fini'
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crti.o:(.fini+0x0): first defined here
simple-tun: In function `mktun':
(.text+0x1e2): multiple definition of `mktun'
/tmp/cc5Ms4xP.o:simple-tun.c:(.text+0xf6): first defined here
simple-tun: In function `net_connect':
(.text+0xe27): multiple definition of `net_connect'
/tmp/cc5Ms4xP.o:simple-tun.c:(.text+0x1115): first defined here
simple-tun: In function `data_start':
(.data+0x0): multiple definition of `__data_start'
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o:(.data+0x0): first defined here
simple-tun: In function `client_connect':
(.text+0xe95): multiple definition of `client_connect'
/tmp/cc5Ms4xP.o:simple-tun.c:(.text+0x1183): first defined here
simple-tun: In function `data_start':
(.data+0x8): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-linux-gnu/4.7/crtbegin.o:(.data+0x0): first defined here
simple-tun:(.rodata+0x0): multiple definition of `_IO_stdin_used'
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crt1.o:(.rodata.cst4+0x0): first defined here
simple-tun: In function `server_connect':
(.text+0xfa2): multiple definition of `server_connect'
/tmp/cc5Ms4xP.o:simple-tun.c:(.text+0x1290): first defined here
simple-tun: In function `print_usage':
(.text+0xe05): multiple definition of `print_usage'
/tmp/cc5Ms4xP.o:simple-tun.c:(.text+0x10f3): first defined here
simple-tun: In function `_init':
(.init+0x0): multiple definition of `_init'
/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../x86_64-linux-gnu/crti.o:(.init+0x0): first defined here
/usr/lib/gcc/x86_64-linux-gnu/4.7/crtend.o:(.tm_clone_table+0x0): multiple definition of `__TMC_END__'
simple-tun:(.data+0x10): first defined here
/usr/bin/ld: error in simple-tun(.eh_frame); no .eh_frame_hdr table will be created.
collect2: error: ld returned 1 exit status我在我的搜索中找不到解决方案。我的代码有什么根本性的错误吗?有人能帮我找出我做错了什么吗?
如果我需要发布更多的代码片段或更多的输出,请让我知道。
发布于 2013-07-26 00:56:05
PTHREAD_MUTEX_INITIALIZER -它必须用作初始值设定项,而不是在正则赋值表达式中。您有两个选择来修复它--要么调用pthread_mutex_init(),要么添加一个类型转换以将PTHREAD_MUTEX_INITIALIZER用作复合文字。您可以选择:Pthread_mutex_init(&q->互斥锁,空);
或者:
q->mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIALIZER;
gcc simple-tun.c simple-tun -lpthread
您丢失了一个-o,因此您试图将程序与其自身链接起来。这是个坏消息。你可能想要的是:
gcc simple-tun.c -o simple-tun -lpthread
实际上,你也应该在那里添加一些警告标志。
发布于 2013-07-26 00:56:16
PTHREAD_MUTEX_INITIALIZER就是这样,一个在声明中使用的初始化器:
pthread_mutex_t foo = PTHREAD_MUTEX_INITIALIZER;如果您有一个在其他地方创建的pthread_mutex_t -例如由malloc创建的-使用pthread_mutex_init对其进行初始化
pthread_mutex_init(&q->mutex, NULL);从SUSV2 documentation for pthread_mutex_init
在默认互斥锁属性合适的情况下,宏
PTHREAD_MUTEX_INITIALIZER可用于初始化静态分配的互斥锁。其效果等同于通过调用pthread_mutex_init()并将参数attr指定为NULL来进行动态初始化,只是不执行任何错误检查。
https://stackoverflow.com/questions/17864095
复制相似问题