但我得到一个编译错误如下:
$gcc simple-tun.c simple-tun -lpthreadsimple-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 -lpthreadsimple-tun: In function `settun':(.text+0x2b7): multiple deFinition of `settun'/tmp/cc5Ms4xP.o:simple-tun.c:(.text+0x1cb): first defined heresimple-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 heresimple-tun: In function `mktun':(.text+0x1e2): multiple deFinition of `mktun'/tmp/cc5Ms4xP.o:simple-tun.c:(.text+0xf6): first defined heresimple-tun: In function `net_connect':(.text+0xe27): multiple deFinition of `net_connect'/tmp/cc5Ms4xP.o:simple-tun.c:(.text+0x1115): first defined heresimple-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 heresimple-tun: In function `clIEnt_connect':(.text+0xe95): multiple deFinition of `clIEnt_connect'/tmp/cc5Ms4xP.o:simple-tun.c:(.text+0x1183): first defined heresimple-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 heresimple-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 heresimple-tun: In function `server_connect':(.text+0xfa2): multiple deFinition of `server_connect'/tmp/cc5Ms4xP.o:simple-tun.c:(.text+0x1290): first defined heresimple-tun: In function `print_usage':(.text+0xe05): multiple deFinition of `print_usage'/tmp/cc5Ms4xP.o:simple-tun.c:(.text+0x10f3): first defined heresimple-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_@R_403_5991@+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 @R_403_5991@ will be created.collect2: error: ld returned 1 exit status
我无法在搜索中找到解决方案.我的代码有什么根本原因吗?有人能帮助我发现我做错了吗?
如果我需要发布更多片段或更多输出,请告诉我.
解决方法 >您不能像这样使用PTHREAD_MUTEX_INITIAliZER – 它必须用作初始值设定项,而不是常规赋值表达式.您有两种方法可以解决它 – 调用pthread_mutex_init()或添加类型转换以使用PTHREAD_MUTEX_INITIAliZER作为复合文字.您的选择:pthread_mutex_init(&q->mutex,NulL);
要么:
q->mutex = (pthread_mutex_t)PTHREAD_MUTEX_INITIAliZER;
>您的链接器错误问题是由此命令行引起的:
gcc simple-tun.c simple-tun -lpthread
你错过了-o,所以你试图将程序与自己联系起来.那是个坏消息.你可能想要的是:
gcc simple-tun.c -o simple-tun -lpthread
实际上,你也应该在那里添加一些警告标志.
总结以上是内存溢出为你收集整理的C pthread mutex:`{‘之前的预期表达式全部内容,希望文章能够帮你解决C pthread mutex:`{‘之前的预期表达式所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)