Add missing -lWs2_32 flag when compiling on Windows
When compiling lcdf-typetools with msys2 on Windows, I had to change
g++ -g -O2 -o cfftot1.exe cfftot1.o maket1font.o ../libefont/libefont.a ../liblcdf/liblcdf.a
g++ -g -O2 -o otfinfo.exe otfinfo.o ../libefont/libefont.a ../liblcdf/liblcdf.a
g++ -g -O2 -o otftotfm.exe automatic.o dvipsencoding.o glyphfilter.o metrics.o otftotfm.o secondary.o uniprop.o util.o ../libefont/libefont.a ../liblcdf/liblcdf.a
g++ -g -O2 -o ttftotype42.exe ttftotype42.o ../libefont/libefont.a ../liblcdf/liblcdf.a
into
g++ -g -O2 -o cfftot1.exe cfftot1.o maket1font.o ../libefont/libefont.a ../liblcdf/liblcdf.a -lws2_32
g++ -g -O2 -o otfinfo.exe otfinfo.o ../libefont/libefont.a ../liblcdf/liblcdf.a -lws2_32
g++ -g -O2 -o otftotfm.exe automatic.o dvipsencoding.o glyphfilter.o metrics.o otftotfm.o secondary.o uniprop.o util.o ../libefont/libefont.a ../liblcdf/liblcdf.a -lws2_32
g++ -g -O2 -o ttftotype42.exe ttftotype42.o ../libefont/libefont.a ../liblcdf/liblcdf.a -lws2_32
to make the build succeed. I didn't yet try to figure out where to change this (I assume somewhere in configure.ac).
Otherwise I get the following error(s):
g++ -g -O2 -o cfftot1.exe cfftot1.o maket1font.o ../libefont/libefont.a ../liblcdf/liblcdf.a
../libefont/libefont.a(otf.o):C:\Programs\MSYS\msys64\home\me\lcdf-typetools\libefont/./../include/efont/otfdata.hh:122: undefined reference to `__imp_ntohs'
I'm worried about including this on all Windows builds. It might require some serious configury.
There is already configure.ac code for finding notes:
AC_SEARCH_LIBS([ntohs], [-lnet -lwinsock32])
but this assumes that the function being searched for is called ntohs, which, on msys2, it is not (it appears to be called __imp_ntohs).
But anyway the configure.ac section called “ntohs, ntohl” is the place to look. Perhaps an AC_LINK_IFELSE is required; perhaps it will be as simple as adding -lws2_32 to the AC_SEARCH_LIBS line.
I primarily needed some hints about how to proceed, so thanks a lot for the first pointer. I'm not claiming that this library is needed unconditionally, I just want to find the correct way to fix this. Apparently the cross-compiled version works fine, so the configuration is not entirely off :)
From configure.ac I took the following minimal example:
#include <winsock2.h>
int main()
{
ntohs(0x0020);
ntohl(0x03040020);
return 0;
}
Compiling on MSYS2:
# OK
$ g++ -c ntohs_test.cpp
# FAIL
$ g++ ntohs_test.cpp -o ntohs_test.exe
C:\Programs\MSYS\msys64\tmp\ccZ88Mfh.o:ntohs_test.cpp:(.text+0x15): undefined reference to `__imp_ntohs'
C:\Programs\MSYS\msys64\tmp\ccZ88Mfh.o:ntohs_test.cpp:(.text+0x23): undefined reference to `__imp_ntohl'
collect2.exe: error: ld returned 1 exit status
# FAIL
$ g++ ntohs_test.cpp -lwinsock32 -o ntohs_test.exe
C:/Programs/MSYS/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.2.1/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lwinsock32
collect2.exe: error: ld returned 1 exit status
# OK
$ g++ ntohs_test.cpp -lWs2_32 -o ntohs_test.exe
In CMD:
# OK
> g++ -c ntohs_test.cpp
# FAIL
> g++ ntohs_test.cpp -o ntohs_test.exe
C:\Users\me\AppData\Local\Temp\ccbbPtg4.o:ntohs_test.cpp:(.text+0x1e): undefined reference to `_imp__ntohs@4'
C:\Users\me\AppData\Local\Temp\ccbbPtg4.o:ntohs_test.cpp:(.text+0x2f): undefined reference to `_imp__ntohl@4'
collect2.exe: error: ld returned 1 exit status
# OK
> g++ ntohs_test.cpp -lWs2_32 -o ntohs_test.exe
According to https://docs.microsoft.com/en-us/windows/desktop/api/winsock/nf-winsock-ntohs one requires at least Windows 8 and the Ws2_32.dll library, but the compilation worked for me on both Windows 7 and 10.
It confuses me why the configure is checking for winsock32 (neither -lnet nor -lwinsock32 works for me natively, but I didn't try the cross-compiler yet).
The output from ./configure is currently:
checking whether ntohs and ntohl are defined... yes
checking for library containing ntohs... no
But the code
AC_SEARCH_LIBS([ntohs], [-lnet -lwinsock32])
seems wrong for various reasons:
- The code should fail when the library is not found (now it simply proceeds)
- It tries to link with
-l-lnetinstead of just-lnet(the-lprefix is probably too much?) - I don't know 100% if this is wrong, but the linking is checked with
char ntohs ();instead of trying to link the proper code (just a few lines above) with all the correct headers etc.
configure:6710: checking for library containing ntohs
configure:6741: gcc -o conftest.exe -g -O2 conftest.c >&5
C:\Programs\MSYS\msys64\tmp\ccarkQXf.o: In function `main':
C:\Programs\MSYS\msys64\home\me\lcdf-typetools/conftest.c:56: undefined reference to `ntohs'