相关疑难解决方法(0)

编译时-pthread和-lpthread之间的差异

是什么区别gcc -pthreadgcc -lpthread它在编译多线程程序中使用?

multithreading gcc pthreads thread-safety compiler-flags

99
推荐指数
3
解决办法
5万
查看次数

GCC:用于静态链接到 pthread 的 --whole-archive 配方在最近的 gcc 版本中停止工作

针对 pthread 的静态链接在 Linux 上是一个困难的话题。它曾经工作包-lpthread-Wl,--whole-archive -lpthread -Wl,--no-whole-archive(细节都可以在此找到答案)。

效果是符号(对于 pthread)是strong ,而不是 weak。从 Ubuntu 18.04 开始(在 gcc 5.4.0 和 gcc 7.4.0 之间),这种行为似乎发生了变化,pthread 符号现在总是以独立于--whole-archive选项的弱符号结束。

因此,-whole-archive配方停止工作。我的问题的目的是了解工具链(编译器、链接器、标准库)中最近发生了什么变化,以及可以采取哪些措施来恢复旧行为。

例子:

#include <mutex>

int main(int argc, char **argv) {
  std::mutex mutex;
  mutex.lock();
  mutex.unlock();
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

在以下所有示例中,使用了相同的编译命令:

g++ -std=c++11 -Wall -static simple.cpp  -Wl,--whole-archive -lpthread  -Wl,--no-whole-archive
Run Code Online (Sandbox Code Playgroud)

之前,与编译时-static,并行线程符号(例如,pthread_mutex_lock)表现强劲(标记为T通过nm),但现在他们是弱(W):

Ubuntu 14.04: docker run --rm -it ubuntu:14.04 bash

$ apt-get …
Run Code Online (Sandbox Code Playgroud)

c++ gcc pthreads static-linking

5
推荐指数
1
解决办法
690
查看次数