【问题标题】:How to embedd openCV Dll's in Executable如何在可执行文件中嵌入openCV Dll
【发布时间】:2011-07-05 08:00:19
【问题描述】:

我使用 openCV 编写了一个图像匹配工具(控制台应用程序,没有 gui 或 windows)。 我想将我的 EXE 文件移植到另一台计算机,但它要求提供 opencv dll(opencv_core220.dll、opencv_highgui220.dll、...)

我的问题是怎么做。我认为它们中的任何一种都很好:

  1. 将 opencv 重新编译为静态库(.lib 而不是 .dll)。那没有用。我收到 10 个关于 cap_vfw.obj 的链接器错误
  2. 如果可能,如何将 DLL 合并/嵌入到 exe 文件中。

我尝试使用ILMerge,但它不起作用(错误:无法从 a.exe 文件加载程序集),因为它是专为 .Net 设计的

附: - 我在 windows 上使用 Visual Studio 2005,c++ 编译器,openCV 2.2

【问题讨论】:

  • ILMerge 适用于 .Net DLL。 OpenCV 不是基于 .Net,而是基于 C++。

标签: c++ dll visual-studio-2005 opencv exe


【解决方案1】:

我找到了答案。 您必须打开原始 openCV 项目并在静态库模式下重新编译所有相关部分。

  • libjasper 开始并按字母顺序直到opencv_video 对每个项目执行此操作。对zlib 项目也这样做
  • 对于每个项目,转到项目属性\配置属性\常规\配置类型并将其设置为静态库
  • 现在重新编译这些项目。他们将创建许多大型 lib 文件(最大 10MB)。这些文件主要位于 opencv 的modules 目录中。当您在针对 dll 进行编译时保留常规 lib 文件时,您必须找到它们并复制到该目录中。现在是一个棘手的部分
  • 与针对 dll 编译时相比,您必须在代码中包含更多库
  • 这是一个示例,您的代码应如下所示。很抱歉,它的格式不正确。可能是这个页面的 html 错误:
#include "cv.h"
#include "highgui.h"

    using namespace std;
    using namespace cv;

    // Directives to linker to include openCV lib files.

    #ifndef STATIC_LIBRARY_LINK
        // Linking against DLL. For each 'lib' file that appears below, final EXE will need a DLL.

    // Core of openCV
    #pragma comment(lib, "opencv_core220.lib") 
    #pragma comment(lib, "opencv_highgui220.lib") 
    #pragma comment(lib, "opencv_imgproc220.lib") 

    // Calibration and image matching
    #pragma comment(lib, "opencv_flann220.lib") 
    #pragma comment(lib, "opencv_features2d220.lib") 
    #pragma comment(lib, "opencv_calib3d220.lib") 

    // Other libs that might be needed
    /*#pragma comment(lib, "opencv_gpu220.lib") 
    #pragma comment(lib, "opencv_video220.lib") 
    #pragma comment(lib, "opencv_legacy220.lib") 

    #pragma comment(lib, "opencv_ml220.lib") 
    #pragma comment(lib, "opencv_objdetect220.lib") 
    #pragma comment(lib, "opencv_ffmpeg220.lib") 
    #pragma comment(lib, "opencv_contrib220.lib") */
#else

    // Static linking. No DLL's would be required but EXE file will be bigger 
    // and linking in debug mode might produce many warnings since *.pdb are not always 
    // present with the lib files

    // Core of openCV. Must be compiled as lib and not as dll's
    #pragma comment(lib, "opencv_core.lib") 
    #pragma comment(lib, "opencv_highgui.lib") 
    #pragma comment(lib, "opencv_imgproc.lib") 

    // Calibration and image matching. Must be compiled as lib and not as dll's
    #pragma comment(lib, "opencv_flann.lib") 
    #pragma comment(lib, "opencv_features2d.lib") 
    #pragma comment(lib, "opencv_calib3d.lib") 

    // Image I/O auxillary libraries. Must be compiled as lib and not as dll's
    #pragma comment(lib, "libtiff.lib") 
    #pragma comment(lib, "libpng.lib")
    #pragma comment(lib, "zlib.lib")
    #pragma comment(lib, "libjasper.lib")
    #pragma comment(lib, "libjpeg.lib")

    // OpenCV linear algebra methods. Must be compiled as lib and not as dll's
    #pragma comment(lib, "opencv_lapack.lib")

    // Auxillary libs, found in visual studio microsoft sdk
    #pragma comment(lib, "vfw32.lib")
    #pragma comment( lib, "comctl32.lib" )
    //#pragma comment(lib, "window_w32.lib")  // Not needed
#endif


    int main(void){
        // Your code here
        return 0;
    }

【讨论】:

    【解决方案2】:

    您要查找的术语是静态链接。 “DLL”代表“动态链接库”,与静态相反。您不能静态链接动态链接库。为此,您需要一个“普通”库。

    【讨论】:

    • 我知道什么是静态链接。但我想将 exe 和 dll 缝合在一起,然后在使用 dll 函数之前,将 dll 的源从 exe 复制并加载到程序内存中,就像它是磁盘上的外部文件一样
    • 好吧,那行不通。 LoadLibrary 期望磁盘上有一个 DLL 文件,带有正确的 PE 头、地址表等。只需使用静态链接即可。这是有原因的。
    • 你说得对,我会重新提出我的问题。如何进行 openCV 的静态链接?
    【解决方案3】:

    转到您正在构建的项目的属性(右键单击解决方案资源管理器中的项目并选择属性)。现在展开 Configuration properties->Linker 选项并在 General 下,设置静态链接库的路径(即,带有 .lib 扩展名)。现在选择 Configuration properties->Linker->Input 选项并输入您希望它静态链接到的所有库的名称。现在重建项目,它们应该链接到可执行文件。如果文件的路径不正确,它会警告您。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-01
      相关资源
      最近更新 更多