ZC: 都是网上搜到的
1、
问题:
在linux下编写多线程,已经添加#include<pthread.h>头文件,qt编译器提示: error: undefined reference to `pthread_create'。
经过百度,得知:“pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a,所以在使用pthread_create()创建线程,以及调用 pthread_atfork()函数建立fork处理程序时,需要链接该库。”
所以,解决方法是:“在编译中要加 -lpthread参数(gcc thread.c -o thread -lpthread)”。
问题是,我不想在终端单独打gcc命令,而是希望能在qtcreator中直接编译。请问该如何设置(添加上这一个-lpthread的后缀)?
解决方法是:
在多线程那个项目的 .pro文件 中添加:LIBS += -lpthread 。
2、
问题:
undefined reference to `shm_open'
undefined reference to `shm_unlink'
解决:
注意一下man shm_open的帮助文件的最后几行:
NOTES
These functions are provided in glibc 2.2 and later.
Programs using
these functions must specify the -lrt flag to cc in order to link
against the required ("realtime") library.
POSIX leaves the behavior of the combination of O_RDONLY and O_TRUNC
unspecified. On Linux, this will successfully truncate an existing
shared memory object - this may not be so on other Unices.
The POSIX shared memory object implementation on Linux 2.4 makes use of
a dedicated file system, which is normally mounted under /dev/shm.
如果你注意到的话,这样编译就能通过了:
gcc -lrt -o test test.c
其实就是要连接库的原因。
3、
4、
5、