1

I have been trying real hard to get libJPEG 9b to work on my Visual Studio Community 2017 project. I have tried a few methods:

  • Using NMake from Visual studio Command Prompt which gave me jpeg.sln and apps.sln, I built those and got cjpeg.exe and djpeg.exe which seem to be working. I have no idea how to link that library even now to my current project and make it work like a normal library!
  • I followed the MinGW instructions on this answer, which gave me .a, .la and .dll.a files. After this I added the include and lib directory paths in my project properties and the .a library file in the linker properties. I also copy-pasted the .h files in my project directory.

I am trying out the code given here. Now, I seem to be getting a bunch of linking errors.

1>------ Build started: Project: JpegReadWrite, Configuration: Debug x64 --- 
1>JpegReadWrite.obj : error LNK2019: unresolved external symbol     jpeg_std_error referenced in function main 
1>JpegReadWrite.obj : error LNK2019: unresolved external symbol jpeg_CreateDecompress referenced in function main 
1>JpegReadWrite.obj : error LNK2019: unresolved external symbol jpeg_destroy_decompress referenced in function main 
1>JpegReadWrite.obj : error LNK2019: unresolved external symbol jpeg_mem_src referenced in function main 
1>JpegReadWrite.obj : error LNK2019: unresolved external symbol jpeg_read_header referenced in function main 
1>JpegReadWrite.obj : error LNK2019: unresolved external symbol jpeg_start_decompress referenced in function main 
1>JpegReadWrite.obj : error LNK2019: unresolved external symbol jpeg_read_scanlines referenced in function main 
1>JpegReadWrite.obj : error LNK2019: unresolved external symbol jpeg_finish_decompress referenced in function main         

1>D:\tonbo_stuti\libjpeg\jpeg-build\lib\libjpeg.a : warning LNK4272: library machine type 'X86' conflicts with target machine type 'x64'          

1>C:\Users\tonbo\Documents\Visual Studio 2017\Projects\serverAppl\x64\Debug\JpegReadWrite.exe : fatal error LNK1120: 8 unresolved externals 1>Done building project "JpegReadWrite.vcxproj" -- FAILED.

I am thoroughly confused on how to make libJPEG work and don't understand what's going on as I am new to the Visual Studio environment. I checked that these symbols actually are defined in "jpeglib.h" and I have included that. I thought I can do the same steps for a .a file as for a .lib file, but something is going wrong.

I would really appreciate any help.

Community
  • 1
  • 1
Stuti Rastogi
  • 1,162
  • 2
  • 16
  • 26
  • 1>D:\tonbo_stuti\libjpeg\jpeg-build\lib\libjpeg.a : warning LNK4272: library machine type 'X86' conflicts with target machine type 'x64' <--- You are building for different platforms, i.e. libjpeg was built with x86, but you try to link it in x64 context. Recompile libjpeg with x64 and try again. – MABVT May 05 '17 at 06:41
  • The relevant part seems to be "LNK4272: library machine type 'X86' conflicts with target machine type 'x64'". Maybe the library is built for a 32 bit machine. – Olaf Dietsche May 05 '17 at 06:42
  • Thank you. @MABVT could you advise me how to recompile libjpeg with x64? This was what I followed last time in the Mingw installation instructions: cd /d/tmp/jpeg-9b ./configure --prefix=/d/tmp/jpeg-build make make install I think by default it took x86, how do I change that? – Stuti Rastogi May 05 '17 at 07:12
  • @StutiRastogi See answer :D – MABVT May 05 '17 at 08:11

1 Answers1

0

Converting to answer:

You need to use the same target, i.e. libjpeg x64.

If you want to use MingW, I recommend doing as below:

Installing MSys2 64-bit

  1. Go to http://www.msys2.org/ and click on “msys2_x86_64-.exe” on the top-middle of the page.
  2. Follow the installation instructions on the Web-Page but DO NOT CHANGE the installation path. This will take a while!

Installing additional software

  1. If no MSys-Shell instance is open, otherwise step 4
    • Open the Explorer and navigate the the msys2 installation directory (usually C:\msys64).
    • Double-Click on msys2_shell.cmd
  2. Enter the command pacman -Su for each of the below listed names (case-sensitive)! If asked for confirmation (Y/n), enter Y and hit the Enter-key. E.g. pacman -Su automake
    • m4
    • autoconf
    • automake
    • pkg-config
    • make
    • libtool

Installing the x86 and x86_64 toolchains

The base installation of MSys2-x64 comes without any build-toolchain. In order to compile for x86 and x86_64, we need to install the toolchains for i686 and x86_64 manually.

Therefore, perform the subsequent steps in the MSys-Shell instance:

  1. If no MSys-Shell instance is open: a. Open the Explorer and navigate the the msys2 installation directory (usually C:\msys64). b. Double-Click on msys2_shell.cmd
  2. Enter pacman -Su mingw-w64-i686-toolchain, hit enter and confirm with “Y”
  3. Wait a while
  4. Enter pacman -Su mingw-w64-x86_64-toolchain, hit enter and confirm with “Y”
  5. Wait a while

Going for libjpeg

  1. Download source and store in (wherever you want)
  2. Launch a mingw32 shell (Goto the MSys installation directory and click on "mingw32.exe"
  3. In the shell, enter "cd " to go to your source!
  4. Enter "touch generate_x86_install.sh"
  5. Open the file in your favorite editor (I use notepad++)
  6. Copy the below contents into it
  7. Run the shell script from mingw32-shell by entering "./generate_x86_install.sh"

#! /bin/bash

if [ "${MSYSTEM}" != "MINGW32" ]; then
 echo "You must run this script from a MingW32 shell!"
 exit;
fi

PWD="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
TGT=${PWD}/deploy/Win32


mkdir -p ${TGT}/include
mkdir -p ${TGT}/lib
mkdir -p ${TGT}/bin 
mkdir -p ${TGT}/man/man1

./configure static --prefix=${TGT} --exec-prefix=${TGT} --
includedir=${TGT}/include --libdir=${TGT}/lib

echo "----------- CLEAN           -----------"
make clean
echo "----------- MAKE            -----------"
make
echo "----------- INSTALL         -----------"
make install
echo "----------- INSTALL-LIB     -----------"
make install-lib
echo "----------- INSTALL-HEADERS -----------"
make install-headers
echo "----------- CHECKING OUTPUT -----------"
objdump -f  ${TGT}/lib/libjpeg.a | grep "file format"

Finally

Do the same thing with "mingw64.exe", BUT:

  1. Copy the generate_x86_install.sh and rename it to generate_x64_install.sh
  2. Replace all occurrences of "32" with "64"

Conclusion

Cross-Compiling with MSys2 and the MingW toolchains is awesome and you should have both: x86 and x86_64 libjpegs.

The objdump in the end should display the proper architectures. E.g.:

// x86 output
jcapimin.o:     file format pe-i386
jcapistd.o:     file format pe-i386
jctrans.o:     file format pe-i386
jcparam.o:     file format pe-i386
jdatadst.o:     file format pe-i386
// ...

// x64 output
jcapimin.o:     file format pe-x86-64
jcapistd.o:     file format pe-x86-64
jctrans.o:     file format pe-x86-64
jcparam.o:     file format pe-x86-64
jdatadst.o:     file format pe-x86-64
// ...

You finally just have to link against the respective lib in deploy/Win32/lib or deploy/Win64/lib.

I tested it on my own machine having two wonderfully built libjpeg.a :D

MABVT
  • 1,350
  • 10
  • 17
  • Thanks for the detailed response, but the shell script gave me errors for both Win32 and Win64. configure: WARNING: you should use --build, --host, --target configure: loading site script /mingw32/etc/config.site checking build system type... Invalid configuration `static': machine `static' not recognized configure: error: /bin/sh ./config.sub static failed _(Continued in next comment)_ – Stuti Rastogi May 05 '17 at 09:07
  • ./generate_x86_install.sh: line 18: --libdir=/d/tonbo_stuti/jpegsr9b/jpeg-9b/deploy/Win32/lib: No such file or directory ------- CLEAN ------- make: *** No rule to make target 'clean'. Stop. ------- MAKE -------- make: *** No targets specified and no makefile found. Stop. -------- INSTALL -------- make: *** No rule to make target 'install'. Stop. ----------- INSTALL-LIB ----------- make: *** No rule to make target 'install-lib'. Stop. ------- INSTALL-HEADERS ------- make: *** No rule to make target 'install-headers'. Stop. _(continued)_ – Stuti Rastogi May 05 '17 at 10:47
  • ----------- CHECKING OUTPUT ----------- C:\msys64\mingw32\bin\objdump.exe: 'D:/tonbo_stuti/jpegsr9b/jpeg-9b/deploy/Win32/lib/libjpeg.a': No such file – Stuti Rastogi May 05 '17 at 10:47
  • So I thought static was giving the issue, and I used the configure command without static. I got the .a files, and when linking the x64 one I got 2 new unresolved errors: `1>libjpeg.a(jerror.o) : error LNK2019: unresolved external symbol __imp___iob_func referenced in function output_message 1>libjpeg.a(jmemmgr.o) : error LNK2019: unresolved external symbol sscanf referenced in function jinit_memory_mgr` Any idea how to get rid of these? Thanks for all your help! – Stuti Rastogi May 05 '17 at 10:59
  • No the problem is the file system structure. Static was fine. It can't find the lib-dir. It is not created :/ How do your shell scripts look like? – MABVT May 05 '17 at 11:05
  • But they were created. All the mkdir operations were successful, and I checked if the folders were present which they were. I did get ----------- INSTALL-LIB ----------- make: *** No rule to make target 'install-lib'. Stop. ----------- INSTALL-HEADERS ----------- make: *** No rule to make target 'install-headers'. Stop. when I tried without static. I don't know why the script couldn't find the directories :( – Stuti Rastogi May 05 '17 at 11:18
  • which libjpeg version do you use? – MABVT May 05 '17 at 11:26
  • 1
    Was able to fix the two unresolved symbol errors: - Added FILE _iob[] = { *stdin, *stdout, *stderr }; extern "C" FILE * __cdecl __iob_func(void) { return _iob; } to stdafx.h - Added legacy_stdio_definitions.lib; to the linker input. My program still has issues, but at least it ran! So thanks for all the input :) – Stuti Rastogi May 05 '17 at 12:36
  • Ah ok... So what you could try also is to install the "gcc" package in MSys2 using pacman -Su gcc, maybe this provides you with what you need... I missed this one above possibly. – MABVT May 05 '17 at 18:13
  • Yes, thanks! Got it working finally with all the different tricks. :) – Stuti Rastogi May 08 '17 at 05:15