30

Is there anyone who succeed to include libjpeg in some compiler? I tried everything: Dev C++, VS10, CodeBlocks, copy the headers and the lib by hand, add with the linker but nothing. Right now I am really confisued as there is not an official guide on how to compile it in any compiler. I would be really happy if someone could provide a tutorial on how the library can be compiled in any compiler. Thank you in advance.

John Carter
  • 53,924
  • 26
  • 111
  • 144
NiVeR
  • 9,644
  • 4
  • 30
  • 35
  • On a unix-like system (eg Cygwin, since you seem to be on Windows), try the standard `./configure && make && make install`. That seems to work fine for me on Ubuntu for the .tar.gz archive from here: http://sourceforge.net/projects/libjpeg/files/libjpeg/6b/ – John Carter Sep 29 '12 at 11:47
  • But as you said, I am on windows, and here won't work the same. – NiVeR Sep 29 '12 at 11:56
  • Are you compiling the library from source, or trying to link to a compiled library you've downloaded? – John Carter Sep 29 '12 at 12:03
  • I am trying to link it in Dev C++.If it already compiled there will be only a .a or .dll file or not? – NiVeR Sep 29 '12 at 12:05
  • Can you post the errors/problems in a pastebin and link?~~ ~~Do you need to compile? There are some binary packages [here](http://gnuwin32.sourceforge.net/packages/jpeg.htm) but they are compiled with mingw.~~ edit: sorry I thought you were tring to compiler libjpeg. – Roman A. Taycher Sep 29 '12 at 11:32
  • Yes I want to compile. I need to use some of the functions for reading and writing JPEG's. I am compiling this code http://pastebin.com/3wkp9MWQ and I add all the headers and .lib .a files then it gives following errors: C:\Program Files (x86)\Dev-Cpp\lib\jpeg-bcc.lib file not recognized: File format not recognized (this usually means GCC does not like a file extension) collect2: ld returned 1 exit status C:\Users\NIVER\Desktop\Makefile.win [Error] [Project1.exe] Error 1 (if this is the only error: please check your library includes) – NiVeR Sep 29 '12 at 11:52
  • Related: https://stackoverflow.com/questions/46055720/cannot-load-any-image-with-cimg/46058914#46058914 – lucidbrot Sep 20 '18 at 08:20

7 Answers7

46

To compile libjpeg 9 in Visual Studio 2012, here are the steps (after unzipping the archive file):

  1. Download the file WIN32.MAK (for example, from http://www.bvbcode.com/code/f2kivdrh-395674-down), and place a copy in the root source code directory (possibly C:\jpeg-9, but it depends where you unzipped it). I will refer to this directory as %jpegsrc% from now on. Having this file is important; otherwise step 3 will produce an error.

  2. In the Visual Studio command prompt, open to %jpegsrc%:

    cd %jpegsrc%

  3. At the same command prompt, execute the following:

    NMAKE /f makefile.vc setup-v10

    This will create two Visual Studio 2010 solutions in %jpegsrc%: jpeg.sln and apps.sln.

  4. Open each solution in Visual Studio 2012. Each one will prompt you to update all the projects to 2012 format. Click on “Update.” One time I did it, the prompt did not appear. In case that happens, right-click on the jpeg solution in the Solution Explorer, and choose “Update VC++ projects...,” which will produce the same prompt.

  5. Save and build each solution as normal. (You have to build the jpeg.sln solution before apps.sln, since the latter depends on the former.)

Note: this process should work correctly in Visual Studio 2010, without the updating, but I have not tested it.

Update: This method still works in Visual Studio 2015 for libjpeg-9a.

AthanasiusOfAlex
  • 1,290
  • 2
  • 16
  • 23
  • 1
    This works fine on VSTudio2013 as well. I have problems compiling the x64 version for apps, is it supported x64? – notNullGothik Feb 03 '14 at 02:05
  • 2
    what is win32.mak and why isn't it included with the other library files? – jiggunjer Aug 13 '15 at 10:01
  • Also this seems to make a jpeg.lib file, which I think is a static library. How to make the .dll with the corresponding .lib? – jiggunjer Aug 13 '15 at 10:07
  • 1
    @jiggunjer win32.mak is (quoting from the file itself) the “Win32 application master NMAKE definitions file for the Microsoft Plaform SDK for Win32 programming sample.” Basically, it lets you compile programs for Windows in C using NMAKE as the automator. The file used to be included in Visual Studio, but not in more recent versions. I just compiled libjpeg using Visual Studio 2015, and I was able to generate a DLL simply by opening the ‘jpeg.sln’ solution, right-clicking on the ‘jpeg’ project, chosing ‘Properties’ and then changing the ‘Configuration type’ to ‘Dynamic library (dll).’ – AthanasiusOfAlex Aug 13 '15 at 15:54
  • 1
    @notNullGothik Have a look at https://msdn.microsoft.com/en-us/library/9yb4317s.aspx on the MSDN website. In short, you create a new target platform, while copying the settings from the existing target. – AthanasiusOfAlex Aug 13 '15 at 16:10
  • Does the dll come with an import lib? My first guess is that changing the property to dll simply changes the extension. Normally a dll from the same source would need some preprocessor command.. – jiggunjer Aug 14 '15 at 15:21
  • 3
    This works with VS2013 x64. I copied Win32.Mak from "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Include". – Dženan Feb 05 '16 at 15:17
  • Thanks. It still works in Visual Studio 2017 for libjpeg-9b. – v.slobodzian May 21 '18 at 15:50
  • 1
    Cannot confirm that this works in VS2017 command prompt. Getting `fatal error U1073: don't know how to make 'setup-v10'` every time. No specific error visible. Or `fatal error U1073: don't know how to make 'jconfig.h'` when leaving the `setup-v10` option out. – Vinz May 28 '18 at 16:43
25

Here is how I've built libjpeg using MinGW on Windows :

1. Get MinGW with MSYS

I've got a copy from http://sourceforge.net/projects/mingw/. Quoting from www.mingw.org :

MSYS is a collection of GNU utilities such as bash, make, gawk and grep to allow building of applications and programs which depend on traditionally UNIX tools to be present.

We will need it to run the configure script that comes with libjpeg sources.


2. Get libjpeg sources

From http://www.ijg.org/, take the Unix format package (the Windows one won't work with this procedure). I took the jpeg_8d version.


3. Prepare a building directory

I've made a temporary directory named tmp in D:\, but you could choose whatever suits your needs. The thing that matters is the name of paths in MSYS. As it brings some * Unixity * to Windows, paths cannot be used in their original form. In a nutshell:

C:\path\to\file becomes /c/path/to/file in MSYS land, an so D:\tmp becomes /d/tmp.

Decompress the libjpeg sources in D:\tmp, so you have a jpeg-8d directory in there.

Create a jpeg-build directory inside D:\tmp, it will hold the built library.

Now everything is ready for the build.


4. ./configure, make, make install

That is the mantra of building in Unix land. An option should be added to redirect the install process to D:\tmp\jpeg-build.

Run the following commands in an MSYS shell (also named MinGW shell in Windows start menu):

cd /d/tmp/jpeg-8d
./configure --prefix=/d/tmp/jpeg-build
make
make install

As an additional step, you can run make test for safety.

These commands will build both static and shared versions of libjpeg.


5. Take the goods, delete the temporaries

If everything runs fine, you can delete the D:\tmp\jpeg-8d directory, but keep the jpeg-build one. It contains:

  • an include directory, containing libjpeg headers. You can move them to your compiler's headers directory.
  • a lib directory, with .a file to pass to the linker. You can move them to your compiler's library directory.
  • a bin directory, holding the libjpeg shared library libjpeg-8.dll and jpeg tools.
  • a share directory, containing man pages for the jpeg tools.

You can now build your program and link it against libjpeg by indicating the right include and library paths.


You can find many details about the libjpeg building and installation process in install.txt inside the source package.

I hope this will be useful.

overcoder
  • 1,523
  • 14
  • 24
  • I installed the MinGW but I can't find any executable. – NiVeR Sep 29 '12 at 13:52
  • What executable are you looking for ? You should switch to the jpeg-8d directory before running commands: `cd /d/tmp/jpeg-8d`. I'll update my answer. – overcoder Sep 29 '12 at 14:24
  • I did everything as you said , i configured Dev C++ by coping all the headers and other files, now I should by able to run the example.c included in libjpeg downloaded archive? – NiVeR Sep 29 '12 at 16:07
  • I haven't used the example.c provided in libjpeg sources, but nothing prevent it from working. I've used libjpeg in the past in a little game engine and it worked like a charm. – overcoder Sep 29 '12 at 16:14
  • when I try to compile that I get a lot of errors. Can you try it please. – NiVeR Sep 29 '12 at 16:17
  • OK, I'll see it. To get more help, try also posting another question, including details about the errors you get. – overcoder Sep 29 '12 at 16:40
  • I don't understand why this http://pastebin.com/V0PCiAte linker errors even tough I have aded the .a and the .lib files to the linker. – NiVeR Sep 29 '12 at 16:44
  • This is obviously the linker complaining about missing symbols. And the example.c also doens't include a `main` entry point. What have you specified as arguments and options ? I recommand posting a new question with details about what you have done so far, and editing this question to indicate a link to the new one. – overcoder Sep 29 '12 at 17:04
  • For me setting the build directory with the prefix option didn't work :( Now it all gets dumped in the source dir. – jiggunjer Dec 01 '15 at 06:23
  • Also use `--host=x86_64-w64-mingw32` whith configure to get a non-cygwin windows version. – jiggunjer Dec 01 '15 at 06:42
  • it finally works by using [MSYS2](https://www.msys2.org) for downloading the 64bits version of the MinGW toolkit (gcc, make...). Be sure to check all the steps for the installation, at the end you should have `C:\msys64\mingw64\bin` with make.exe ... – Paltoquet Aug 26 '21 at 12:43
17

It is really simple to build jpeg.lib with VS10.

First, download the libjpeg source code in zip format. At the time I'm writing this you can find it here.

Then extract the contents of the zip file to your disk.

Then open a VS2010 command prompt shell (or call vcvarsall.bat on any command prompt window), cd to the jpeg source directory (jpeg-8d in the download referenced above) and type the following:

nmake -f makefile.win setup-v10

This will generate two VS2010 solutions, and a bunch of project files. The solutions are:

  • jpeg.sln, which builds jpeg.lib
  • apps.sln, which builds the sample applications.

Good luck!

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
  • 1
    Can you give me a link so I can download the jpeg.sln and apps.sln? Because I have some problem it says that 'nmake' is not a know internal commond. – NiVeR Sep 29 '12 at 16:32
  • The sln and vcxproj files come with libjpeg, you already have them. Your problem is that you are not using a command prompt window that has the Visual Studio environment variables set. You have to run vcvarsall.bat to set that up, or start a VS command prompt from the Start menu. – Miguel Grinberg Sep 29 '12 at 19:47
  • Yes I am using that prompt, the problem is that it says that it couldn't locate the location of the folder VS Common Tools. – NiVeR Sep 30 '12 at 09:39
  • You may want to reinstall Visual Studio and try again. The nmake.exe comes with both the Express and paid editions of VS. On my VS2012 installation I have it in "C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\bin" – Miguel Grinberg Sep 30 '12 at 17:30
  • Ok, It's fine now. I am using Code Blocks and everything seems to work well. Thanks for the big help. – NiVeR Sep 30 '12 at 17:33
  • makefile.vc throws error fatal error U1050: Must specify CPU environment variable ( CPU=i386, CPU=ALPHA) – Ali Kazmi Dec 25 '16 at 06:51
  • 2
    Getting `fatal error U1073: don't know how to make 'setup-v10'` – Vinz May 28 '18 at 16:32
4

If you don't happen to have Visual Studio 2010 installed, here is what works on Visual Studio 2017:

Basic / Common steps:

  1. Download the latest version of libjpeg from: http://www.ijg.org/ (zip version) and extract it to a folder
  2. Open the "Developer Command Prompt for VS2017"
  3. Change directory (cd) to where you extracted the library source

VS 2017 Approach:

  1. Include the Windows SDK v7.1A directory (required for Win32.Mak by nmake later on) by running: set INCLUDE=%INCLUDE%;c:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Include

  2. Run nmake /f makefile.win setup-v15 (note the v15 for VS2017 here)

From here on follow what @AthanasiusOfAlex explained to upgrade the Visual Studio 2010 solution to the Visual Studio version you are running. If you want the Debug configuration, follow what @SteveEng explained.

Errors you might stumble across:

  • If nmake fails and tells you it doesn't know how to make jconfig.h, manually rename the file jconfig.vc to jconfig.h
  • If nmake fails and tells you Win32.Mak cannot be found, manually copy it from the SDK dir mentioned in step #4 to the libjpeg source directory. If you don't happen to have that SDK version installed, download the file from a trustworthy resource.
  • If nmake fails and tells you it doesn't know how to make setup-v15, trial and error your way through starting with setup-v10, setup-v11, etc... These are VS versions and one of them should work as long as you have any VS version later than VS 2008 installed.

Hope this helps people going through similar pain with this.

Vinz
  • 3,030
  • 4
  • 31
  • 52
  • Minor type, it's jconfig.vc for Visual C. – Crypth Jun 17 '18 at 20:41
  • 2
    I used the [file link from Anathasius's answer](http://www.bvbcode.com/code/f2kivdrh-395674-down) and had thus to use `makefile.vc` instead of `makefile.win`. Thanks for the answer! – lucidbrot Sep 20 '18 at 07:40
3

If you want debug mode as well in MSVC. Follow AthanasiusOfAlex's method, build the release, then:

  1. Right-click on the project and select properties at the very bottom
  2. Click on configuration manager and on the active solution configuration drop-down select -new-
  3. Set the name to debug and on the drop-down select copy configuration settings from release and click OK
  4. Close the dialog, go to general settings and under Target Name add a d to the end so it looks like this: $(ProjectName)d
  5. On Whole Program Optimization drop-down select: No Whole Program Optimization
  6. Then click on the C/C++ under configuration properties on the left and on the Debug Information Format drop-down select C7 compatible (/Z7)
  7. Under optimization select disabled and select NO for both Enable Fiber-Safe Optimizations and Whole Program Optimizations
  8. Under preprocessor, preprocessor definitions, click on edit and use the following: WIN32 _DEBUG DEBUG _LIB _CRT_SECURE_NO_WARNINGS
  9. Under code generation, under runtime library select Multi-threaded Debug DLL (/MDd)

Build and you're done!

SteveEng
  • 331
  • 2
  • 6
0

I built libjpeg-turbo in Visual Studio 2022 cmd using following steps:

set PATH=D:\build\build_utils\nasm-2.16.01;%PATH%

mkdir build_jpeg

cd build_jpeg

cmake -G"NMake Makefiles" -DCMAKE_BUILD_TYPE=Release ..\libjpeg-libjpeg-turbo-2.1.0

nmake

All the exe, dll, and libs will be created in build_jpeg folder.

-1

This is for VS2019 with version number 16.0

Consider the Visual Studio Version name correctly like this for creating the .sln file.

Product name Code name Version number

Visual Studio 2019 Dev16 16.0

Visual Studio 2017 Dev15 15.0

Visual Studio 2015 Dev14 14.0

Visual Studio 2013 Dev12 12.0

Zeeshan
  • 357
  • 1
  • 8