已发布的版本(静态/动态)[GitHub]:CristiFati/Prebuilt-Binaries - (主)Prebuilt-Binaries/LibJPEG https://github.com/CristiFati/Prebuilt-Binaries/tree/master/LibJPEG.
就像现在几乎所有的软件一样,LibJPEG还托管于[GitHub]: winlibs/libjpeg - libjpeg-9b https://github.com/winlibs/libjpeg/releases/tag/libjpeg-9b。我从两个地方下载了它,进行了比较,只发现了一些细微的差异(源代码中没有)。我将解释一下GitHub版本。不幸的是,没有关于此的演练,所以我必须查看一些文件才能弄清楚必须做什么。以下是步骤列表:
1.准备地面
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.
作为评论,我正在使用VStudio(社区)2015 (14)用于此任务(但对于较新的版本应该没有差异)。
处理的工具Makefiles is NMake ([MS.Docs]:NMAKE 参考 https://learn.microsoft.com/en-us/cpp/build/nmake-reference?view=vs-2015)。我们需要根据该文件调用它。NMake位于 ”${VSTUDIO_INSTALL_DIR}\VC\bin\nmake.exe" (the (Nix style) env var并不真正存在,它只是一个路径占位符);通常是“%SystemDrive%\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\nmake.exe”(例如在我的机器上它是“C:\Install\x86\Microsoft\Visual Studio Community\2015\VC\bin\nmake.exe").
请注意,处理时VStudio命令行工具,最好是:
-
Use VCVarsAll(那是一个 (batch)设置一些的工具env vars like %PATH%, %包括%, %LIB%,...),所以所有的VStudio工具无需完整路径即可使用。但由于它也位于“%VSTUDIO_INSTALL_DIR%\VC\vcvarsall.bat“ (查看[SO]:链接到 CRT(未解析的外部符号 WinMainCRTStartup)(@CristiFati 的回答) https://stackoverflow.com/a/71872682/4788546有关更多详细信息),不值得花费精力去查找/调用它
-
Use VS2015工具命令提示符 from 开始菜单(这基本上称为VCVarsAll)
-
[MS.Docs]:在命令行上构建 https://learn.microsoft.com/en-us/cpp/build/building-on-the-command-line?view=vs-2015包含有关此主题的更多详细信息
现在我们知道在哪里NMake找到了,让我们运行:
"${VSTUDIO_INSTALL_DIR}\VC\bin\nmake.exe" /f makefile.vc setup-v10
(别忘了附上NMake的路径在双引号("),特别是如果它包含SPACEs).
如果遇到问题,Google可能会产生解决方案:
- 很可能遇到的一种:致命错误 U1052:找不到文件“win32.mak”- 查看[SO]:Visual Studio NMake 构建失败并显示:致命错误 U1052:找不到文件“win32.mak”(@CristiFati 的回答) https://stackoverflow.com/a/73167842/4788546
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).
运行该命令后,文件夹中应该有 2VStudio解决方案文件:
2. 构建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,将揭示有关解决方案(和项目)的一些(令人不愉快的)内容:
-
它只有Win32(32 位或x86) 平台
-
正如您已经提到的,它构建了一个静态库
无论如何,解决方案应该建立OOTB。完成后,你会注意到Release应该包含的文件夹(除了一堆中间 -.obj文件),一个4+ MB jpeg.lib文件是静态的.lib。知道代码(以及项目相关文件)没问题后,让我们继续下一步。
2.1 构建jpeg .dll
为了避免破坏现有功能,我们应该创建一个新配置(注意创建新的过程platform对于您的项目来说(几乎)是相同的):
-
从菜单中选择构建 -> 配置管理器...
-
在弹出的对话框中,向上点击Release组合框并选择
-
In the 新解决方案配置对话框中,为新配置选择一个名称:我选择发布DLL(从现在起我就要依赖这个名字了)
-
确保选择Release in the 从以下位置复制设置组合框
-
Check 创建新的项目配置
按下后OK,新的发布DLL配置将相同于Release。下一步是对其进行必要的更改,以实现我们的目标。RClick on the jpeg项目在解决方案浏览器(在左侧VStudio窗口),然后选择特性:
在上图中,有jpeg项目设置窗口,显示它们最终的样子。我突出显示(使用颜色)不同的兴趣点:
Save All并构建。构建成功后,新建一个文件夹Win32-发布-DLL就会出现,和前一个一样,它会包含一堆文件plus jpeg.dll。人们可能会认为事情已经完成,但事实并非如此。所有的代码都是编译/构建 in the .dll,但它是未出口, 所以.dll几乎无法使用。你可以检查很多.dll (or .exe) 属性打开它依赖步行者 http://www.dependencywalker.com(或较新的[GitHub]: lucasg/依赖项 https://github.com/lucasg/Dependencies)。您可以查看以下屏幕截图[SO]:Excel VBA,无法从 DLL 文件中找到 DLL 入口点(@CristiFati 的回答) https://stackoverflow.com/a/44177222/4788546- 在我们的例子中,导出区域将是空的。
最后一点:如果您认为将来您需要为其他平台构建(x64, 甚至ARM),并且您还需要进行一些调试(添加Debug配置),添加Debug配置下Win32平台first, and 只有那时创建新平台Win32,否则您需要添加Debug添加之前创建的每个平台的配置Debug配置为Win32.
2.2 导出数据.dll
正如注释:除了__declspec(dll导出)方法,还有另外 2 个(在处理来自Win .dlls):
-
将导出列表作为参数传递给链接器([MS.Docs]:/EXPORT(导出函数) https://learn.microsoft.com/en-us/cpp/build/reference/export-exports-a-function?view=vs-2015)
-
将它们枚举到[MS.Docs]:模块定义文件 https://learn.microsoft.com/en-us/previous-versions/ms925051(v=msdn.10)
但是,由于我们不知道代码并且可能需要导出许多符号,因此这 3 个符号中的任何一个都不可扩展(它们可能需要大量的研究/工作)。无论如何,我们会坚持原来的方法:
-
保存以下代码:
#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
在一个名为jexport.h in the libjpeg源文件夹。这是一个非常标准的头文件,处理.dll出口。
接下来,将其添加到项目中:解决方案浏览器, RClick on 头文件 -> 添加 -> 现有项目...
-
使用新文件
--- 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.
上面是一个diff. See [SO]:从 PyCharm 社区版中的鼠标右键单击上下文菜单运行/调试 Django 应用程序单元测试? (@CristiFati 的回答) https://stackoverflow.com/a/42989490/4788546 (打补丁utrunner部分)了解如何应用补丁Win(基本上,每一行以one "+"标志进入,每行以one "-"标志熄灭)。但是,由于更改很小,因此也可以手动完成。需要更改的文件是jmorecfg.h(需要 2 件事):
我认为这一步是某种解决方法(gainarie),但正如我所说,“真正的事情”只是需要太多的工作(和时间)。
-
告诉compiler考虑我们的改变
编辑项目设置(再次小心Platform / 配置), 选择配置属性 -> C/C++ -> 预处理器 -> 预处理器定义并替换旧值(在我的例子中:WIN32;NDEBUG;_LIB;_CRT_SECURE_NO_WARNINGS) to WIN32;LIBJPEG_EXPORTS;_CRT_SECURE_NO_WARNINGS;NDEBUG。我所做的是:
-
Adding LIBJPEG_EXPORTS
-
Removing _LIB
-
有些转移
[MS.Docs]:/D(预处理器定义) https://learn.microsoft.com/en-us/cpp/build/reference/d-preprocessor-definitions?view=vs-2015可能包含一些有用的信息。
As a 边注,经过上述更改后,原始(或“正常”)行为(Release配置)不会OK(它会生成一堆警告,但这在技术上是不正确的)。那是因为我不想依赖_LIB宏来区分静态/动态库构建。为了把事情理顺,add LIBJPEG_STATIC in the 预处理器定义 for Release配置(遵循与上述相同的程序)。
Save All并构建。构建成功后,jpeg.dll被覆盖了,更重要的是:一个新文件jpeg.lib将被创建;这告诉我们jpeg.dll出口东西。您可以使用以下命令再次检查依赖步行者.
3. 测试.dll
加载apps.sln解决方案,最好是在一个新的VStudio实例。正如我所说,我只关心djpeg项目。
-
1st thing to do, is to build it for the existing Release configuration. But, because of the changes done to jpeg library, it won't build OOTB (there will be linker errors). In order to get rid of them, edit its settings, the Preprocessor Definitions (just like in the previous step), and add LIBJPEG_STATIC (don't forget the separator (;)).
构建应该成功并在djpeg\发布文件夹中应该有一个djpeg.exe文件。尝试运行它,将会成功(这是我唯一关心的事情)。
-
构建一个新的可执行文件,使用我们的.dll:
-
就像在jpeg库的案例,创建一个新配置:发布DLL(确保完成所有步骤,但不要更改)
-
There's only one change required: let the linker know where we built our .lib file. Go to project settings, Configuration Properties -> Linker -> Input -> Additional Dependencies: the 1st is Release\jpeg.lib. Obviously, the path is not correct, so we need to replace Release by Win32-Release-DLL (of course you could use VStudio macros (for Platform / Configuration)). Might check [MS.Docs]: .Lib Files as Linker Input https://learn.microsoft.com/en-us/cpp/build/reference/dot-lib-files-as-linker-input?view=vs-2015.
Save All并构建。构建成功后,在djpeg\Release-DLL,应该有一个djpeg.exe文件。尝试运行它会fail,因为找不到jpeg.dll。通过复制来解决这个问题jpeg.dll去检查[MS.Docs]:动态链接库搜索顺序 https://learn.microsoft.com/en-gb/windows/desktop/Dlls/dynamic-link-library-search-order对于语义):
Again, you can check the 2 executables with Dependency Walker (but the size difference says enough: the 2nd djpeg.exe is significantly smaller), to see which one depends on the jpeg.dll. Anyway, here's the output on my computer (yes, Dependency Walker can act as a CmdLine as well :) ):
e:\Work\Dev\StackOverflow\q044450813\src\libjpeg-libjpeg-9b>"c:\Install\x86\Depends\Dependency Walker-politist-texan\AllVers\depends.exe" /c /ot:static.txt djpeg\Release\djpeg.exe
e:\Work\Dev\StackOverflow\q044450813\src\libjpeg-libjpeg-9b>"c:\Install\x86\Depends\Dependency Walker-politist-texan\AllVers\depends.exe" /c /ot:dynamic.txt djpeg\Release-DLL\djpeg.exe
e:\Work\Dev\StackOverflow\q044450813\src\libjpeg-libjpeg-9b>type static.txt | findstr -i "jpeg.dll"
e:\Work\Dev\StackOverflow\q044450813\src\libjpeg-libjpeg-9b>type dynamic.txt | findstr -i "jpeg.dll"
[ ] e:\work\dev\stackoverflow\q044450813\src\libjpeg-libjpeg-9b\djpeg\release-dll\JPEG.DLL
[ ] e:\work\dev\stackoverflow\q044450813\src\libjpeg-libjpeg-9b\djpeg\release-dll\JPEG.DLL 2017-06-09 21:16 2017-06-09 21:16 237,056 A 0x00000000 0x0003ECC8 x86 GUI CV,Unknown 0x10000000 Unknown 0x0003E000 Not Loaded N/A N/A 0.0 14.0 6.0 6.0
Update #0
我还: