Windows 上的 pthreads 链接问题 - 应该归咎于 gcc 还是 SystemC?

问题描述

嗨,

我最近遇到了一个无法解释的链接问题。

这是一个再简单不过的例子:

sc_main.cpp:

#include <systemc.h>

int sc_main (int argc,char* argv[])
{
  sc_start(SC_ZERO_TIME);
  return(0);
} 

我使用以下代码编译和链接,使用 winlibs-x86_64-posix-seh-gcc-10.2.0-mingw-w64-8.0.0-r7.7z

g++ -pthread main_sc.cpp -o main_sc.exe -s $(LDFLAGS) -I$(SYstemC_HOME)/include -L$(SYstemC_HOME)/lib-mingw64 -lsystemc

现在,因为我的目标是 Win64,并且我想制作一个不依赖 MinGW-64 运行时库的可执行文件,所以我一直在研究许多可能的设置:

Option 1 - LDFLAGS := -static
Option 2 - LDFLAGS := -static-libgcc -static-libstdc++ -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic
Option 3 - LDFLAGS := -static-libgcc -static-libstdc++ -Wl,-Bstatic,--whole-archive -lpthread -Wl,-Bdynamic -Wl,--no-whole-archive

在这一点上,有时可以,有时不可以,具体取决于我使用的 SystemC 版本:

SytemC 2.3.2 + Option 1 : fine
SytemC 2.3.3 + Option 1 : libsystemc.a(sc_prim_channel.o):sc_prim_channel.cpp:(.text+0x44): undefined reference to `__imp_pthread_mutex_unlock'
                          libsystemc.a(sc_prim_channel.o):sc_prim_channel.cpp:(.text$_ZN7sc_core13sc_host_mutex6unlockEv[_ZN7sc_core13sc_host_mutex6unlockEv]+0xa): undefined reference to `__imp_pthread_mutex_unlock'
                          libsystemc.a(sc_prim_channel.o):sc_prim_channel.cpp:(.text$_ZN7sc_core17sc_host_semaphore7trywaitEv[_ZN7sc_core17sc_host_semaphore7trywaitEv]+0x2f): undefined reference to `__imp_pthread_mutex_unlock'

但是

SytemC 2.3.2 + Option 2 or 3 : fine
SytemC 2.3.3 + Option 2 or 3 : libpthread.dll.a(d000077.o):(.text+0x0): multiple deFinition of `pthread_mutex_unlock' ; mingw-w64-v8.0.0/mingw-w64-libraries/winpthreads/src/mutex.c:207: first defined here

请注意,我使用的工具链基于 gcc 10.2.0,但这并不相关。我只使用了这个工具链,因为它基于最新的 MinGW 库 (8.0.0)

事实上,我已经基于相同的 MinGW 8.0.0 库和 gcc 6.3.0 生成了我自己的(交叉编译)工具链(在 linux 上运行),并且我看到了完全相同的结果。>

知道发生了什么吗?我做错了什么吗?我不太确定这个问题是否与 SystemC 或 GCC/winpthreads 有关,但我很想认为 SystemC 可以被免除。 那么谁该受到责备,是否有解决方法

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)