DanBloomberg / leptonica

Leptonica is an open source library containing software that is broadly useful for image processing and image analysis applications. The official github repository for Leptonica is: danbloomberg/leptonica. See leptonica.org for more documentation.
Other
1.72k stars 384 forks source link

Cross-Compiling Leptonica for Windows on Arch Linux #749

Open Danny4021 opened 1 week ago

Danny4021 commented 1 week ago

Hello,

as stated in the title, I'm currently trying to cross-compile a Windows DLL for Leptonica on my Arch Linux System (Since our software needs to run on Windows and Linux) and I can't really get it to work. I was trying to find anything inside the documentation about cross-compiling but I've not been able to find anything about it. Here are the steps I took:

After running these steps I get the following error:

jpegio.c:144:10: fatal error: jpeglib.h: No such file or directory
  144 | #include "jpeglib.h"
      |          ^~~~~~~~~~~
compilation terminated.

But that's not the main issue, since I can circumvent it by copying the necessary header files into the leptonica/src folder on my system (For some reason this error only occurs when trying to cross-compile. It doesn't happen when I don't configure the host when running ./configure)

After copying all the header files it's missing I get the following error after re-running make

Making all in prog
make[2]: Entering directory '/home/leptonica/prog'
  CCLD     convertfilestopdf.exe
/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: cannot find -lpng16: No such file or directory
/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: cannot find -ljpeg: No such file or directory
/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: cannot find -ltiff: No such file or directory
/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: cannot find -lwebp: No such file or directory
/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: cannot find -lwebpmux: No such file or directory
/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: cannot find -lopenjp2: No such file or directory
/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: cannot find -lz: No such file or directory
collect2: error: ld returned 1 exit status
make[2]: *** [Makefile:2952: convertfilestopdf.exe] Error 1
make[2]: Leaving directory '/home/leptonica/prog'
make[1]: *** [Makefile:536: all-recursive] Error 1
make[1]: Leaving directory '/home/leptonica'
make: *** [Makefile:420: all] Error 2

Now to my questions: Am I doing something fundamentally wrong? Does anyone have recent experience cross-compiling leptonica for Windows or does someone have a tutorial on this matter? (I've been looking for a while but I haven't been able to find anything) I'd just like to know if I'm on the right track or if I'm doing something very wrong.

Any kind of help on that matter would be much appreciated. Thanks in advance! Kind regards

(If this helps, here are the packages I've installed on arch in order to compile it):

mingw-w64-binutils 2.39-1
mingw-w64-crt 11.0.0-1
mingw-w64-environment 1-5
mingw-w64-gcc 13.1.0-1
mingw-w64-headers 11.0.0-1
mingw-w64-make 1-5
mingw-w64-winpthreads 11.0.0-1
libjpeg-turbo 3.0.2-2
libpng 1.6.43-1
libtiff 4.6.0-4
libtool 2.4.7+4+g1ec8fa28-7
lzlib 1.14-1
make 4.4.1-2
autoconf 2.72-1
automake 1.16.5-2
DanBloomberg commented 1 week ago

I have not had any experience building windows programs from unix. The linker is not finding the image compression libraries. Have you looked at section 3 (b),(c) of the README.html?

Danny4021 commented 1 week ago

Thank you very much for the quick response!

I've re-read the mentioned paragraphs of the article and checked the necessery dependencies in section I/O libraries leptonica is dependent on once more.

Sadly, I can't use MSYS2 or Cygwin for my purposes since both of them are for Windows-Only. I've followed the steps in section 3 (b) and installed mingw32 but it refers back to section 2 if I want to build a DLL. So I guess that both steps 3 (b) and 3 (c) are only applicable if you use MSYS or Cygwin on a Windows system, since following the steps of section 2 creates just the so files on Linux systems.

I've also re-confirmed that all the packages mentioned in section I/O libraries leptonica is dependent on are installed and inside the expected folder. (Since I don't want to use the static makefile I've confirmed that all the files are present in form of shared libraries)

Is there maybe a way to simulate a Windows environment on linux like what MSYS2 or Cygwin do for Windows?

Thanks in advance!

DanBloomberg commented 1 week ago

Sorry, I haven't used cross-compiling (mingw). I will send an email to Dave Bryan in case he has a suggestion for you.

DanBloomberg commented 1 week ago

Although Dave Bryan hasn't used gnu on linux when running mingw, he made this observation about cross-compilation:

The cross-compiler looks in alternate places for headers and libraries, rather than
in the "standard" places used by the native compiler.  These can be
determined by running the cross-compiler with something more-or-less like
this (where "x86_64-w64-mingw32-gcc" is a long-handed symbol for the
actual cross-compiler):

  x86_64-w64-mingw32-gcc -print-search-dirs

This should display the set of directories where gcc expects to find
libraries.

For headers, this should do it (again, more-or-less, as I've never used
cross-compilers):

  x86_64-w64-mingw32-cpp -v /dev/null

Placing the header and library files in some directories shown in the
search paths should resolve this issue. 
stweil commented 1 week ago

@Danny4021, do you have x86_64-w64-mingw32-pkg-config in your search PATH? It is required and will automatically set the right compiler flags for the header files.

I use ../configure --host=x86_64-w64-mingw32 --prefix=/usr/x86_64-w64-mingw32 PKG_CONFIG_PATH=/mingw64/lib/pkgconfig on Debian GNU Linux. It prints somethink like this:

[...]
checking for x86_64-w64-mingw32-pkg-config... /usr/bin/x86_64-w64-mingw32-pkg-config
checking pkg-config is at least version 0.9.0... yes
checking for zlib... yes
checking for libpng... yes
checking for libjpeg... yes
checking for DGifOpenFileHandle in -lgif... no
checking for gif_lib.h... no
checking for libtiff-4... yes
checking for libwebp... yes
checking for libwebpmux >= 0.5.0... yes
checking for libopenjp2 >= 2.0.0... yes
[...]

The directory /mingw64 contains the cross library packages which are installed with pacman on my build host.

Danny4021 commented 4 days ago

Hello @stweil, thanks for the response! No, I haven't the pkg-config in my search path. It doesn't seem like ths is installed by default when installing the mingw package for linux and is found inside another package (the mentioned /mingw64 also does not exist on my arch linux system).

I'm currently on vacation so I'll get back to you all next monday when I get to try your suggested solutions. Thanks for the help!