tel / saltine

Cryptography that's easy to digest (NaCl/libsodium bindings)
https://github.com/tel/saltine
MIT License
61 stars 29 forks source link

"unknown symbol pthread_mutex_lock" in windows #30

Closed athanclark closed 7 years ago

athanclark commented 7 years ago

Hello, I'm trying to invoke some saltine functions at compile time with TemplateHaskell. I've built saltine-0.0.0.4 independently and run stack test, and everything works fine, I just need to build and test with a little library path lookup awkwardness:

# builds without a hitch
stack build --extra-include-dirs ~/dev/libsodium-win64/include --extra-lib-dirs ~/dev/libsodium-win64/lib

# testing works, but I need to copy a DLL for linking, because Windows
cp ~/dev/libsodium-win64/bin/libsodium-18.dll ./
stack test --extra-include-dirs ~/dev/libsodium-win64/include --extra-lib-dirs ~/dev/libsodium-win64/lib

All the tests pass, which gives me a lot of confidence. But for some reason, I'm getting a strange error when trying to run Crypto.Saltine.Core.Box.newKeypair with a -XTemplateHaskell splice:

The strange thing; GHC says it can't find sodium-0.0.0.4, yet in my stack.yaml I have my local build listed:

# ...
packages:
  - location: '../saltine-0.0.0.4'
    extra-dep: true
# ...

Wouldn't the build fail before getting to my project if it couldn't find the sodium-0.0.0.4 dependency? It's a strange error there, definitely, but the detrimental one for me is the "unknown symbol pthread_mutex_lock" error coming from libsodium.a.

Is this a common error / debug symbol? I'm no C++ dev or anything, but it sounds like this prebuilt libsodium.a includes POSIX symbols or something, when it's intended to build only for Windows. I'm not sure what's going on here.

@jedisct1 Do you happen to know what might be wrong here? Shouldn't the Windows builds exclude POSIX debug symbols?

linearray commented 7 years ago

Which version of libsodium did you use? While I believe you are using 1.0.11, please confirm.

linearray commented 7 years ago

Windows is not my core expertise, but here is what I think happened: You are using the MinGW-compiled libsodium. MinGW supplies an implementation of pthreads, hence pthreads are used instead of the Windows primitives (https://github.com/jedisct1/libsodium/blob/68564326e1e9dc57ef03746f85734232d20ca6fb/src/libsodium/sodium/core.c#L4) When you develop an application using the mingw-libsodium lib, you must link with a suitable pthread implementation, such as the one that comes with MinGW. Alternatively you could switch to the MSVC-compiled libsodium that is also on the download page.

athanclark commented 7 years ago

Yes you're exactly correct, sorry - I didn't realize MinGW had a pthread implementation. Thank you for your help!