Which dlls to include with my compiled executable - Distinguish between system and other dlls

Viewed 168

I have a c++ project that uses CMake to generate the build files. This project depends on external libraries for which I downloaded the necessary files from the internet (header, .lib and .dll). My project can compile and if the DLLs of the libraries are included in the PATH, or the DLLs are copied to the same place as the .exe of my project, it runs without a problem. Now I want to be able to run this program on another computer. For that, I need to know which DLLs I have to include alongside my .exe file.

I have seen that the DLLs dependencies of any given dll or exe can be found using MVSC dumpbin. For example for this main:

#include <QApplication>
#include <QStyleFactory>
#include <QFileDialog>
#include <QStandardPaths>
#include <opencv2/highgui/highgui.hpp>

int main(int argc, char** argv)
{
  // Create application
  QApplication app(argc, argv);
  app.setStyle(QStyleFactory::create(QStringLiteral("Fusion")));

  // Get file name
  QString file_name = QFileDialog::getOpenFileName(nullptr, QObject::tr("Open File"),
                                                QStandardPaths::displayName(QStandardPaths::HomeLocation),
                                                QObject::tr("Images (*.png *.xpm *.jpg)"));

  // Read image
  cv::Mat mat = cv::imread(file_name.toStdString());

  // Show image in opencv
  cv::namedWindow("display",cv::WINDOW_KEEPRATIO |cv::WINDOW_GUI_NORMAL );
  cv::imshow("display",mat);
  cv::waitKey(0);

  // Run application
  return app.exec();
}

The output of the command dumpbin /dependents main.exe is:

Microsoft (R) COFF/PE Dumper Version 14.28.29912.0
Copyright (C) Microsoft Corporation.  All rights reserved.


Dump of file .\src\RelWithDebInfo\main.exe

File Type: EXECUTABLE IMAGE

  Image has the following dependencies:

    Qt5Widgets.dll
    opencv_world347.dll
    Qt5Core.dll
    VCRUNTIME140.dll
    VCRUNTIME140_1.dll
    api-ms-win-crt-runtime-l1-1-0.dll
    api-ms-win-crt-math-l1-1-0.dll
    api-ms-win-crt-stdio-l1-1-0.dll
    api-ms-win-crt-locale-l1-1-0.dll
    api-ms-win-crt-heap-l1-1-0.dll
    KERNEL32.dll
    SHELL32.dll

  Summary

        1000 .00cfg
        1000 .data
        2000 .idata
        1000 .pdata
        3000 .rdata
        1000 .reloc
        1000 .rsrc
        6000 .text

In this case, I need this list of DLLsfor my program to run. However, some are system DLLs (KERNEL32.dll, SHELL32.dll...) and some are libraries that are not system DLLs (Qt5Widgets.dll, opencv_world347.dll...).

Where should I decide to stop copying DLLsto ship my program? Should I ship it with KERNEL32.dll and SHELL32.dll, should I include api-ms-win*.dll or should I only include Qt5Widgets.dll, Qt5Core.dll, and opencv_world347.dll? How can I know from the list which are system DLLs and which are not?

PS2: I know that specifically for Qt there is windeployqt that will automatically handle the Qt DLLs part and that the VCRUNTIME140*.dll part is also handled by installing vc_redist

Note: The scope of this question is just to know where to stop. I am not interested in how to build an installer or how to automate that copy of the files or where to look for the dependent DLLs.

Note2: Questions Find dependent modules of dll and how do I include dll's such as kernel32.dll which my unmanaged dll needs did not provide an answer to my question.

1 Answers

General case :

In my experience, the question often occurs much earlier in the development process.

As I see it : when one start an empty project, there is no need to embed any dll with i. As you said : system dlls are not to be provided.

At some point, one decide to use opencv, QT, or any external dependency in the project. At this point one may add the required headers in the making file (or the project/solution file), and dll in the build script.

In fact adding the dll to the shipment is part of the decision to use the dependency.

So the short answer to your question would be : you know which dll to embed, because it is specified in the documentation of the dependency you are trying to add to your project. (doc sample for opencv)

Specific case :

We can imagine, that someone find a piece of code in some obscure repository, without any documentation, history information and no "build" script or equivalent. this guy wants to build and ship it, so he must decide which dll to embed.

This guy would do exactly what you did : use a dependency walker to get all dependencies list, then would decide with his own judgement which one he wants to provide.

I assume that, if the application runs on windows, this guy would embed everything but the windows specific dlls, or do an installer.

Related