Problem with static linking of pthread

Discussion of chess software programming and technical issues.

Moderator: Ras

User avatar
Giorgio Medeot
Posts: 52
Joined: Fri Jan 29, 2010 2:01 pm
Location: Ivrea, Italy

Problem with static linking of pthread

Post by Giorgio Medeot »

Hi all,
I'm trying to compile Stockfish, using mingw compiler (gcc version 4.5.2).
If I use dynamic linking of the pthread library all goes fine, but when I try to static link I go into troubles.
After some trial and fail, I've managed to get the code to compile, but as soon as I launch the executable, it crashes with a segfault.

Here is the compiler log:

Code: Select all

-------------- Clean: Debug_static in stockfish_201_skill ---------------

Cleaned "stockfish_201_skill - Debug_static"

-------------- Build: Debug_static in stockfish_201_skill ---------------

[  4.5%] mingw32-g++.exe -Wall  -g -Wall -Wcast-qual -fno-exceptions -fno-rtti -Wno-long-long -Wextra -msse -DSKILL_LEVELS -DPTW32_STATIC_LIB  -Wall    -c F:\PortableApps\PortableApps\chess\stockfish\stockfish_skill_201\benchmark.cpp -o obj_static_debug\benchmark.o
...
...
[ 95.5%] mingw32-g++.exe -Wall  -g -Wall -Wcast-qual -fno-exceptions -fno-rtti -Wno-long-long -Wextra -msse -DSKILL_LEVELS -DPTW32_STATIC_LIB  -Wall    -c F:\PortableApps\PortableApps\chess\stockfish\stockfish_skill_201\ucioption.cpp -o obj_static_debug\ucioption.o
[100.0%] mingw32-g++.exe  -o bin_static_debug\stockfish_skill.exe obj_static_debug\benchmark.o obj_static_debug\bitbase.o obj_static_debug\bitboard.o obj_static_debug\book.o obj_static_debug\endgame.o obj_static_debug\evaluate.o obj_static_debug\history.o obj_static_debug\main.o obj_static_debug\material.o obj_static_debug\misc.o obj_static_debug\move.o obj_static_debug\movegen.o obj_static_debug\movepick.o obj_static_debug\pawns.o obj_static_debug\position.o obj_static_debug\san.o obj_static_debug\search.o obj_static_debug\timeman.o obj_static_debug\tt.o obj_static_debug\uci.o obj_static_debug\ucioption.o   -static-libgcc -static-libstdc++ -lpthreadGC2 -lsupc++ -lws2_32  
Output size is 3.04 MB
Process terminated with status 0 (0 minutes, 48 seconds)
0 errors, 0 warnings (0 minutes, 48 seconds)
And the debugger output:

Code: Select all

Building to ensure sources are up-to-date
Build succeeded
Selecting target: 
Debug_static
Adding source dir: F:\PortableApps\PortableApps\chess\stockfish\stockfish_skill_201\stockfish_skill\
Adding source dir: F:\PortableApps\PortableApps\chess\stockfish\stockfish_skill_201\
Adding file: bin_static_debug\stockfish_skill.exe
Starting debugger: 
done
Registered new type: wxString
Registered new type: STL String
Registered new type: STL Vector
Setting breakpoints
Debugger name and version: GNU gdb (GDB) 7.2
Child process PID: 948
Program received signal SIGSEGV, Segmentation fault.
In ntdll!RtlpWaitForCriticalSection () (C:\WINDOWS\system32\ntdll.dll)
At F:\PortableApps\PortableApps\chess\stockfish\stockfish_skill_201\search.cpp:2350
The call stack:

Code: Select all

#0 7C92B21A	ntdll!RtlpWaitForCriticalSection() (C:\WINDOWS\system32\ntdll.dll:??)
#1 7C911046	ntdll!RtlEnumerateGenericTableLikeADirectory() (C:\WINDOWS\system32\ntdll.dll:??)
#2 00000000	0x00e9e7c8 in ptw32_thread_reuse_lock() (??:??)
#3 00000000	0x0043d190 in pthread_cond_init() (??:??)
#4 0042E517	(anonymous namespace)::ThreadsManager::init_threads(this=0x5d2720) (F:\PortableApps\PortableApps\chess\stockfish\stockfish_skill_201\search.cpp:2350)
#5 0042A08E	init_threads() (F:\PortableApps\PortableApps\chess\stockfish\stockfish_skill_201\search.cpp:356)
#6 0041387A	main(argc=1, argv=0x3e25c0) (F:\PortableApps\PortableApps\chess\stockfish\stockfish_skill_201\main.cpp:69)
I guess it must be some kind of compiler/linker issue, but I really don't have a clue about what can be wrong. Any hint will be really appreciated :)

Thanks and cheers,
  • Giorgio
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: Problem with static linking of pthread

Post by bob »

Giorgio Medeot wrote:Hi all,
I'm trying to compile Stockfish, using mingw compiler (gcc version 4.5.2).
If I use dynamic linking of the pthread library all goes fine, but when I try to static link I go into troubles.
After some trial and fail, I've managed to get the code to compile, but as soon as I launch the executable, it crashes with a segfault.

Here is the compiler log:

Code: Select all

-------------- Clean: Debug_static in stockfish_201_skill ---------------

Cleaned "stockfish_201_skill - Debug_static"

-------------- Build: Debug_static in stockfish_201_skill ---------------

[  4.5%] mingw32-g++.exe -Wall  -g -Wall -Wcast-qual -fno-exceptions -fno-rtti -Wno-long-long -Wextra -msse -DSKILL_LEVELS -DPTW32_STATIC_LIB  -Wall    -c F:\PortableApps\PortableApps\chess\stockfish\stockfish_skill_201\benchmark.cpp -o obj_static_debug\benchmark.o
...
...
[ 95.5%] mingw32-g++.exe -Wall  -g -Wall -Wcast-qual -fno-exceptions -fno-rtti -Wno-long-long -Wextra -msse -DSKILL_LEVELS -DPTW32_STATIC_LIB  -Wall    -c F:\PortableApps\PortableApps\chess\stockfish\stockfish_skill_201\ucioption.cpp -o obj_static_debug\ucioption.o
[100.0%] mingw32-g++.exe  -o bin_static_debug\stockfish_skill.exe obj_static_debug\benchmark.o obj_static_debug\bitbase.o obj_static_debug\bitboard.o obj_static_debug\book.o obj_static_debug\endgame.o obj_static_debug\evaluate.o obj_static_debug\history.o obj_static_debug\main.o obj_static_debug\material.o obj_static_debug\misc.o obj_static_debug\move.o obj_static_debug\movegen.o obj_static_debug\movepick.o obj_static_debug\pawns.o obj_static_debug\position.o obj_static_debug\san.o obj_static_debug\search.o obj_static_debug\timeman.o obj_static_debug\tt.o obj_static_debug\uci.o obj_static_debug\ucioption.o   -static-libgcc -static-libstdc++ -lpthreadGC2 -lsupc++ -lws2_32  
Output size is 3.04 MB
Process terminated with status 0 (0 minutes, 48 seconds)
0 errors, 0 warnings (0 minutes, 48 seconds)
And the debugger output:

Code: Select all

Building to ensure sources are up-to-date
Build succeeded
Selecting target: 
Debug_static
Adding source dir: F:\PortableApps\PortableApps\chess\stockfish\stockfish_skill_201\stockfish_skill\
Adding source dir: F:\PortableApps\PortableApps\chess\stockfish\stockfish_skill_201\
Adding file: bin_static_debug\stockfish_skill.exe
Starting debugger: 
done
Registered new type: wxString
Registered new type: STL String
Registered new type: STL Vector
Setting breakpoints
Debugger name and version: GNU gdb (GDB) 7.2
Child process PID: 948
Program received signal SIGSEGV, Segmentation fault.
In ntdll!RtlpWaitForCriticalSection () (C:\WINDOWS\system32\ntdll.dll)
At F:\PortableApps\PortableApps\chess\stockfish\stockfish_skill_201\search.cpp:2350
The call stack:

Code: Select all

#0 7C92B21A	ntdll!RtlpWaitForCriticalSection() (C:\WINDOWS\system32\ntdll.dll:??)
#1 7C911046	ntdll!RtlEnumerateGenericTableLikeADirectory() (C:\WINDOWS\system32\ntdll.dll:??)
#2 00000000	0x00e9e7c8 in ptw32_thread_reuse_lock() (??:??)
#3 00000000	0x0043d190 in pthread_cond_init() (??:??)
#4 0042E517	(anonymous namespace)::ThreadsManager::init_threads(this=0x5d2720) (F:\PortableApps\PortableApps\chess\stockfish\stockfish_skill_201\search.cpp:2350)
#5 0042A08E	init_threads() (F:\PortableApps\PortableApps\chess\stockfish\stockfish_skill_201\search.cpp:356)
#6 0041387A	main(argc=1, argv=0x3e25c0) (F:\PortableApps\PortableApps\chess\stockfish\stockfish_skill_201\main.cpp:69)
I guess it must be some kind of compiler/linker issue, but I really don't have a clue about what can be wrong. Any hint will be really appreciated :)

Thanks and cheers,
  • Giorgio
I have had similar problems, but using the intel compiler. I wanted to compile on A and run on B, where A and B have different library versions, but it apparently does not quite work for reasons unknown...
User avatar
Jim Ablett
Posts: 2331
Joined: Fri Jul 14, 2006 7:56 am
Location: London, England
Full name: Jim Ablett

Re: Problem with static linking of pthread

Post by Jim Ablett »

With Windows version of the Pthread library you can only link dynamically with external dll.

Jim.
User avatar
Giorgio Medeot
Posts: 52
Joined: Fri Jan 29, 2010 2:01 pm
Location: Ivrea, Italy

Re: Problem with static linking of pthread

Post by Giorgio Medeot »

Doh! I wasn't aware of that. Also the 'read me' in the pthread sources were not clear about this (that is, were not clear to me :)), though they do mention some requirement in order to build both the pthread static lib and a statically linked application.
After some more googling, I've now found some reference to the need of calling certain "non portable" functions, when statically linking. Maybe this is the problem.
Well, however that may be, I will keep the compile with the external dll.

Thanks for your help! :)
  • Giorgio