即使使用 -lpthread 选项,Sqlite3 库也会出现线程错误

Sqlite3 library giving thread error even though using -lpthread option

我正在为我的项目使用 Code::Blocks IDE。

到目前为止,我下载了 sqlite3 合并文件,使用 make install 安装它们并将 libsqlite3.a 库链接到我的项目 项目->构建选项->链接器设置并添加了libslite3.a文件。

现在,当我尝试构建我的项目时,出现了一些错误,例如 undefined reference to pthread_mutex_trylockundefined reference to pthread_create

我在 main 中包含 pthreadthreadmutex headers,我交替添加了选项 -lpthreadpthreadproject->build options->other copiler options 中,我仍然遇到同样的错误。

-l<library> 是 linker 选项,不是编译器选项,所以在 Code::Blocks 它进入 其他 linker 选项

从编译器选项中删除 -lpthread 并将其添加到 linker 选项可能就足够了。

然而,在 GCC 中启用 pthreads 多线程的正确、可移植的方法是 将选项 -pthread(不是 -lpthread)添加到 both 编译器选项:-

项目->构建选项->编译器设置 -> 其他编译器选项

和 linker 选项:

项目->构建选项->链接器设置 -> 其他 link 个选项

这个选项告诉编译器做正确的事情 生成 pthreads 兼容代码并告诉 linker 做正确的事情 link 它与主机平台的 POSIX 线程库。

要在您的程序中使用 pthreads#include <pthreads.h>