Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

visual studio 2015 - How to build a DLL version of libjpeg 9b?

I want to build a DLL version of libjpeg 9b. According to the document here, it seems that we need to add a preprocessor __declspec(dllexport) or __declspec(dllimport) before the declaration of each function to be exported, in addition to setting the Configuration Type to "Dynamic Library (.dll)". But this is not an easy job because there are so many functions in libjpeg. So, is there any short-cut or work-around to build a DLL libjpeg without or with little modification of the jpeglib.h? Is there any DLL-ready source of libjpeg 9b available? I am using Visual Studio 2015 on Windows 7 64bit. Thanks for your answer.

PS: I downloaded the source of libjpeg 9b from http://www.ijg.org/files/. Is this the official place to download it? I ask because the beginning bytes of .vcxproj (originally .v10) files seems invalid (C2 8B C2 AF C2 A8) so Visual Studio is unable to open it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Published builds (static / dynamic) on [GitHub]: CristiFati/Prebuilt-Binaries - (master) Prebuilt-Binaries/LibJPEG.



Like almost all of the nowadays software, libjpeg is also hosted on [GitHub]: winlibs/libjpeg - libjpeg-9b. I downloaded it from both places, did a comparison and only few minor differences (out of which none in the source code) popped up. I'm going to explain on the GitHub version. Unfortunately, there is no walkthrough on this, so I had to look inside some of the files in order to figure out what has to be done. Here's a list of steps:

1. Prepare the ground

1st thing is to extract the compressed (zip / tar.gz) file content into a folder (and have a cmd console opened there). That folder contains a bunch of files. The 1st one that we need is makefile.vc. As it name suggests, it's a Makefile that consists of a series of rules/instructions used in general to build stuff.

As a remark, I'm using VStudio (Community) 2015 (14.0) for this task (but there should be no differences for newer versions).

The tool that deals with Makefiles is nmake ([MS.Docs]: NMAKE reference). We need to call it against that file. nmake is located in "%VSTUDIO_INSTALL_DIR%VCin make.exe" (the env var doesn't really exist, it's just a path placeholder); typically that is "%SystemDrive%Program Files (x86)Microsoft Visual Studio 14.0VCin make.exe" (e.g. on my machine it's "C:Installx86MicrosoftVisual Studio Community2015VCin make.exe").

Note that when dealing with VStudio command line tools, it's better to either:

  • Use vcvarsall (that's a (batch) tool that sets some env vars like %PATH%, %INCLUDE%, %LIB%, ...), so all the VStudio tools are available without their full path. But since it's also located in "%VSTUDIO_INSTALL_DIR%VCvcvarsall.bat", it doesn't worth the effort to locate/call it
  • Use VS2015 Tools Command Prompt from Start Menu (which basically calls vcvarsall)
  • [MS.Docs]: Building on the Command Line contains more details on this topic

Now that we know where nmake is located, let's run:

"%VSTUDIO_INSTALL_DIR%VCin
make.exe" /f makefile.vc setup-v10

(don't forget to enclose nmake's path in dblquotes("), especially if it contains SPACEs).

If running into issues, Google will probably yield solutions:

setup-v10 is a Makefile target, which simply renames some of the files in the folder (frankly, I don't know why the files aren't like already renamed in the 1st place).

After running the command in the folder, there should be 2 VStudio solution files:

  • jpeg.sln - which contains one project:
    • jpeg.vcxproj - this is the project responsible of building the lib
  • apps.sln - which contains a bunch of projects (which I don't know/care what they do):
    • djpeg.vcxproj - this is the only one that I'm going to mention, since I'll be using it to test the built lib

2. Build the jpeg library

The 1st thing to notice is that the solution/project files generated in the previous section are for VStudio 2010. But that's not a problem, since VStudio 2015 is able to handle them (when loading them, it will do all the necessary conversions to bring them up to date).

Opening jpeg.sln, will reveal some (unpleasant) stuff about the solution (and project):

  • It only has Win32 (32bit or x86) platform
    • It only has Release configuration
  • As you already mentioned, it builds a static library

Anyway, the solution should build OOTB. After completion, you'd notice a Release folder which should contain (besides a bunch of intermediary - .obj files), a 4+ MB jpeg.lib file which is the static lib. Knowing that the code (and project related files) is OK, let's move on to the next step.

2.1 Build the jpeg DLL

In order to avoid breaking existing functionality, we should create a new configuration (note that the process of creating a new platform for your project is (almost) the same):

  • From menu, choose Build -> Configuration Manager...
  • In the dialog box that popped, up click on the Release combo box and select < New...>
  • In the New Solution Configuration dialog box, select a name for the new configuration: I chose Release-DLL (and from now on I'm going to rely on this name)
  • Make sure to select Release in the Copy settings from combobox
  • Check Create new project configurations

After pressing OK, the new the Release-DLL configuration will be identical to Release. Next step is to do the necessary changes to it, in order to achieve our goal. RClick on the jpeg project in the Solution Explorer (in the left side of VStudio window), and choose Properties:

VStudio settings

In the image above, there's the jpeg project settings window, showing how they should look like at the end. I highlighted (using colors) different points of interest:

  • Blue: the Platform / Configuration pair:

    • That is being modified (in the topmost dialog box)
    • Active (in the VStudio main window in the back)

    When modifying some Platform / Configuration settings make sure that it's the active one (the data in the blue rectangles is identical), otherwise you'd bang your head against the walls and waste time trying to figure out why something that seems correct, doesn't work as expected. On my opinion, when this dialog box pops up, it should have the active values, but that's not always the case.

  • Red: items that should be modified (or at least require attention):
    • Configuration Type should obviously be Dynamic Library (.dll)
    • Platform Toolset should be Visual Studio 2015 (I'm mentioning this, since it happened to open a VStudio 2010 project with VStudio2015, and it kept the old toolset, and continued to build with it, but that's only if you have both versions installed)
  • Green: items recommended to be modified. Those are only folders. Notice the $(Platform) variable (that I use as a good practise), which comes in handy when building for more than 1 platform (e.g. Win32 and x64)

Save All and build. After the build succeeds, a new folder Win32-Release-DLL will appear, and like the previous one, it will contain a bunch of files plus jpeg.dll. One might think that it's done, but it's not quite so. All the code is compiled/built in the .dll, but it's not exported, so the .dll is pretty much unusable. You can check many of .dll (or .exe) properties opening it with Dependency Walker. You can look at the screenshots from [SO]: Excel VBA, Can't Find DLL Entry Point from a DLL file (@CristiFati's answer) - in our case the export area will be empty.

One final note: If you think that in the future you'll need to build for other platforms (x64, or even ARM), and also you'll need to do some debugging (add a Debug configuration), add the Debug configuration under Win32 platform first, and only then create the new platform from Win32, otherwise you'll need to add the Debug configuration for every platform created before adding the Debug configuration to Win32.

2.2 Export data from the DLL

Just as a note: besides the __declspec(dllexport) approach, there are 2 more (when dealing with exports from Win .dlls):

But, since we don't know the code and there might be many symbols to export, neither one of the 3 is scalable (they would probably require lots of research/work). Anyway, we'll stick to the original approach:

  1. Save the following piece of code:

    #pragma once
    
    #if defined(_WIN32)
    #  if defined(LIBJPEG_STATIC)
    #    define LIBJPEG_EXPORT_API
    #  else
    #    if defined(LIBJPEG_EXPORTS)
    #      define LIBJPEG_EXPORT_API __declspec(dllexport)
    #    else
    #      define LIBJPEG_EXPORT_API __declspec(dllimport)
    #    endif
    #  endif
    #else
    #  define LIBJPEG_EXPORT_API
    #endif
    

    in a file called jexport.h in the libjpeg source folder. This is a pretty standard header file that deals with .dll exports.
    Next, add it to the project: in Solution Explorer, RClick on Header Files -> Add -> Existing Item...

  2. Make use of the new file

    --- jmorecfg.h.orig 2016-03-30 09:38:56.000000000 +0300
    +++ jmorecfg.h  2017-06-09 21:04:33.762535400 +0300
    @@ -30,6 +30,8 @@
      * in all cases (see below).
      */
    
    +#include "jexport.h"
    +
     #define BITS_IN_JSAMPLE  8 /* use 8, 9, 10, 11, or 12 */
    
    
    @@ -245,7 +247,8 @@
     /* a function referenced thru EXTERNs: */
     #define GLOBAL(type)       type
     /* a reference to a GLOBAL function: */
    -#define EXTERN(type)       extern type
    +
    +#define EXTERN(type)       extern LIBJPEG_EXPORT_API type
    
    
     /* This macro is used to declare a "method", that is, a function pointer.
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...