kgriffs
kgriffs

Reputation: 4258

How do you compile static pthread-win32 lib for x64?

It looks like some work has been done to make pthread-win32 work with x64, but there are no build instructions. I have tried simly building with the Visual Studio x64 Cross Tools Command Prompt, but when I try to link to the lib from an x64 application, it can't see any of the function exports. It seems like it is still compiling the lib as x86 or something.

I've even tried adding /MACHINE to the makefile in the appropriate places, but it doesn't help. Has anyone gotten this to work?

Upvotes: 6

Views: 16611

Answers (7)

rogerdpack
rogerdpack

Reputation: 66741

For me, I just use a 64-bit windows compiler (mingw-w64 cross compiler in this particular case) then make (with2.9.1) like:

$ make clean GC-static 

Or if cross compiling from linux:

$ make clean GC-static CROSS=i686-w64-mingw32-

Then how I install it for use (some of this may not be needed, of course),

cp libpthreadGC2.a $your_mingw_prefix/lib/libpthread.a
cp pthread.h sched.h semaphore.h $your_mingw_prefix/include

then to use it, you have to define this (example ffmpeg configure line):

--extra-cflags=-DPTW32_STATIC_LIB 

Anyhow that's one way.

Another way is to do the same then modify the *.h files and remove all references to dllexport from the headers (or manually define DPTW32_STATIC_LIB in the headers).

ex header way:

 sed -i '1s/^/#define PTW32_STATIC_LIB\n/' $your_mingw_prefix/include/pthread.h

or even better in case it gets defined elsewhere:

sed -i '1s/^/#if !defined(PTW32_STATIC_LIB)\n#define PTW32_STATIC_LIB\n#endif\n/' $your_mingw_prefix/include/pthread.h

Upvotes: 5

AakashPatil
AakashPatil

Reputation: 61

You can use the vcpkg here. Which is the Windows package manager for C++. It supports pthread building and also other open source libraries.

I wanted to use a static pthread library. When i downloaded the pthread i got the dll(pthread.dll) and import lib(pthread.lib) i.e I can not use only pthread.lib I had to use the pthread.dll file.

So using vcpkg I have built the static lib. Which I can use without any dll dependencies

Using "vcpkg" you can build both Static and Dynamic Libraries

You can use below steps

Below i have added the steps for all DLL (x86|x64) and LIB (x86|x64) cases. You can build it as per your need.

Clone the vcpkg from git directory vcpkg git repo

From the directory where you have cloned vcpkg run below command- Which will install the vcpkg

bootstrap - vcpkg.bat

Check for the library availability by running below commands

vcpkg search pthread

Which will show you below result

mbedtls[pthreads]                     Multi-threading support
pthread              3.0.0            empty package, linking to other port
pthreads             3.0.0-6          pthreads for windows

As you can see it supports pthread for windows

1 .Building Dynamic Library with import lib (DLL)

Building x86 DLL

vcpkg install pthreads:x86-windows

Which will build the dll and import library in .\vcpkg\installed\x86-windows from here copy the lib and include and you can use them

Building x64 DLL

vcpkg install pthreads:x64-windows

Which will build the dll and import library in .\vcpkg\installed\x64-windows from here copy the lib and include folders.

2. Building Static Library (LIB)

Building x86 LIB

vcpkg install pthreads:x86-windows-static 

Which will build the dll and import library in .\vcpkg\installed\x86-windows-static from here copy the lib and include and you can use them

Building x64 LIB

vcpkg install pthreads:x64-windows-static

Which will build the dll and import library in .\vcpkg\installed\x64-windows-static from here copy the lib and include folders.

NOTE : Try to use with admin privileges

Upvotes: 6

Victor S
Victor S

Reputation: 5132

I was successful in replacing "pthread-win32" with "pthreads4w" https://sourceforge.net/projects/pthreads4w/ and compiling in MSVC2019 for x64 target using the console nmake command. Even statically link.

Upvotes: 0

Vladimir Fekete
Vladimir Fekete

Reputation: 49

to expand kgriffs answer one has to do two more things to actually build a 64bit DLL and not 32bit DLL.

First download latest pthreads via CVS (as suggested here)

1) use 64bit build tools - achieved by loading correct VC environment settings in command line (more about it here):

C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\vcvarsall.bat amd64

(change the 11.0 to whatever version you are using)

2) As it is written in the pthreads Makefile:

TARGET_CPU is an environment variable set by Visual Studio Command Prompt as provided by the SDK (VS 2010 Express plus SDK 7.1) PLATFORM is an environment variable that may be set in the VS 2013 Express x64 cross development environment

which means, that if it was not done by the vcvars (in my case it wasn't) you need to set TARGET_CPU or PLATFORM (just in case I set them both):

set TARGET_CPU=x64 set PLATFORM=x64

3) and now the final step:

nmake clean VC nmake clean VC-debug

this will make a 64bit DLL files (and proper import library and PDB). I can verify that it works with Visual Studio 2012.

Upvotes: 2

user3726672
user3726672

Reputation: 325

Here's how I did it (VS2015). Should work for older Visual Studios too.

1) Download the release .zip from SourceForge
2) Unpack to a clean folder- should see "pthreads.2"
3) Open up your Visual Studio command prompt, navigate to "pthreads.2." 4) Run "nmake", no arguments. It produces a help message listing all the legal commands you can give 'nmake' to build it. For more info, see "pthreads.2\FAQ" file which explains their 3 different flavors of 'cleanup' handling.

I would suggest building "VC" and "VC-debug" (and maybe the static ones of those) only. The 'real' pthreads is a C system library on POSIX platforms like Linux, so only those combos are going to give you the exact same C error behavior on Windows that you'd get on Linux, FreeBSD, etc.

Upvotes: 2

kgriffs
kgriffs

Reputation: 4258

Until it's officially released, it looks like you have to check out the CVS head to get version 2.9 of the library. Version 2.9 has all the x64 patches, but you will still have problems if you try to compile the static library from the command line.

The only workaround I know of is to use the DLLs instead of statically linking the LIB.

Upvotes: 3

Head Geek
Head Geek

Reputation: 39858

This message might help.

Upvotes: 0

Related Questions