"Error: Cannot find file '-lws2_32'" while compiling OCaml with OPAM on Windows

Hello! Newbie here interested in learning OCaml. When trying to do the setup for Windows as described here OCaml on Windows · OCaml Documentation, I get this error:

[ERROR] The compilation of ocaml-base-compiler.5.2.1 failed at "make -j7".

#=== ERROR while compiling ocaml-base-compiler.5.2.1 ==========================#
# context     2.3.0 | win32/x86_64 |  | https://opam.ocaml.org#9f34991c038ca38ca54a43232b9cae1c14a3f1e3
# path        ~\AppData\Local\opam\default\.opam-switch\build\ocaml-base-compiler.5.2.1
# command     ~\AppData\Local\opam\.cygwin\root\bin\make.exe -j7
# exit-code   2
# env-file    ~\AppData\Local\Temp\opam-axele-22540\ocaml-base-compiler-22540-ab1bc1.env
# output-file ~\AppData\Local\Temp\opam-axele-22540\ocaml-base-compiler-22540-ab1bc1.out
### output ###
# cp flexdll-sources/flexdll_mingw64.o flexdll-sources/flexdll_initer_mingw64.o byte/bin
# [...]
# ** Fatal error: Cannot find file "-lws2_32"
# make[2]: *** [Makefile:1309: runtime/ocamlrun.exe] Error 2
# make[2]: *** Waiting for unfinished jobs....
# ** Fatal error: Cannot find file "-lws2_32"
# make[2]: *** [Makefile:1321: runtime/ocamlrund.exe] Error 2
# ** Fatal error: Cannot find file "-lws2_32"
# make[2]: *** [Makefile:1327: runtime/ocamlruni.exe] Error 2
# make[2]: Leaving directory '/cygdrive/c/Users/axele/AppData/Local/opam/default/.opam-switch/build/ocaml-base-compiler.5.2.1'
# make[1]: *** [Makefile:750: opt.opt] Error 2
# make[1]: Leaving directory '/cygdrive/c/Users/axele/AppData/Local/opam/default/.opam-switch/build/ocaml-base-compiler.5.2.1'
# make: *** [Makefile:831: world.opt] Error 2

Any help would be greatly appreciated. I’ve also posted to Stack Overflow.
Thank you!
(I’m aiming to learn along the advent-of-code challenges)

Edit:
I solved the issue. There was a problem with an incorrect gcc version being prioritised in the PATH. Identifying the correct version and adding it to path, superseding the incorrect version (I removed the wrong version), solved it for me!

1 Like

EDIT: I see you solved the issue on Stack Overflow.

Yes! Made an edit here as well.

This makes me very happy - have fun, and I hope you get to stay using OCaml on Windows! :blush:

This sounds like a bug, given that you were clearly using opam’s internally managed Cygwin installation. Can you share the PATH to gcc which was causing the problem and possibly what software package had installed it?

This is probably related to [ci] Add cygwin bin directories to PATH by tobil4sk · Pull Request #26508 · ocaml/opam-repository · GitHub and "dunelongcmd" Linking Error on Windows using ocamlopt.
Searching for Issues to reference I came across Resolve the Cygwin bin directory above Windows system32 directory by dra27 · Pull Request #5832 · ocaml/opam · GitHub which suggests to me that the cygwin bin directory should be added to the PATH by opam env. But as the one, who created the discuss thread I linked above, this doesn’t happen for me:

PS C:\Users\Benjamin> opam --version
2.3.0
PS C:\Users\Benjamin> opam env
$env:OPAM_LAST_ENV = 'C:\Users\Benjamin\.opam\.last-env\env-54eb3877a71fdbd17bdefee28595f8c8-0'
$env:OPAM_SWITCH_PREFIX = 'C:\Users\Benjamin\.opam\default'
$env:CAML_LD_LIBRARY_PATH = 'C:\Users\Benjamin\.opam\default\lib\stublibs;C:\Users\Benjamin\.opam\default\lib\ocaml\stublibs;C:\Users\Benjamin\.opam\default\lib\ocaml'
$env:OCAML_TOPLEVEL_PATH = 'C:\Users\Benjamin\.opam\default\lib\toplevel'
$env:MANPATH = ':/cygdrive/c/Users/Benjamin/.opam/default/man'
$env:Path = 'C:\Users\Benjamin\.opam\default\bin;C:\Users\Benjamin\.opam\.cygwin\root\usr\i686-w64-mingw32\sys-root\mingw\bin;C:\Users\Benjamin\.opam\.cygwin\root\usr\x86_64-w64-mingw32\sys-root\mingw\bin;C:\windows\system32;C:\windows;C:\windows\System32\Wbem;C:\windows\System32\WindowsPowerShell\v1.0\;C:\windows\System32\OpenSSH\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\PuTTY\;C:\Program Files\Git\cmd;C:\ProgramData\chocolatey\bin;C:\Program Files\nodejs\;C:\Program Files (x86)\Windows Kits\10\Windows Performance Toolkit\;;C:\Program Files\NVIDIA Corporation\NVIDIA app\NvDLISR;C:\Users\Benjamin\AppData\Local\Programs\opam\bin;C:\Users\Benjamin\AppData\Local\Microsoft\WindowsApps;C:\Users\Benjamin\AppData\Local\Programs\Microsoft VS Code\bin;'

(The MANPATH also looks weird but I can’t test whether it works because I don’t have man in my PATH)

I’m sorry - I’d missed your original Discuss post! This isn’t quite the same issue, as the failure here is within a package build. For pragmatic reaons, Cygwin’s bin directory is made available while packages are being compiled (or you wouldn’t compile OCaml!), but the idea is that once the packages are built, one is at least trying to use native Windows.

Unfortunately, adding Cygwin’s bin directory to PATH is not trivial - but the full explanation of why is the opam PR of mine you referenced.

It seems the issue arose because of another gcc installation from Strawberry Perl,
it was after I removed the following from the PATH:

C:\Strawberry\c\bin;
C:\Strawberry\perl\site\bin;
C:\Strawberry\perl\bin;

and added

C:\Users\axele\AppData\Local\opam\.cygwin\root\bin;

when it started to work.

That’s great, thanks - out of curiosity, did you re-try the opam installation in between removing Strawberry Perl and adding C:\Users\axele\AppData\Local\opam\.cygwin\root\bin to PATH (i.e. from what you remember you did, was the second step definitely necessary?)

I think I saved to path before running

opam switch create default ocaml-base-compiler.5.2.1

which solved it. So, to answer your question: I don’t know if saving to path is necessary.

1 Like