How to combine all dlls from subproject into one dll?

Now, I have several subprojects, which will export dll files. Let’s say, I will have 10 dlls.

Is there any method to combine all 10 dll files into one dll file?

In opencv, if we set BUILD_opencv_world to on, it will finally export one dll file. If the BUILD_opencv_world is off, the opencv project would export many dll files.

How can I combine the 10 dll files into one dll file?

This isn’t generally possible. Windows executables have different calls for symbols in the library from those outside the library. Recompilation is the only reliable way.

I can recompile it. After recompiling, how can I combine exported many dll files into one dll file?

The project needs to support it. BUILD_opencv_world=ON is what I’d do.

Do you mean that I need to set(BUILD_opencv_world ON) in my CMakeLists.txt? Why it is "opencv"_word? Should I modify it as my project name? For example set(BUILD_helloworld_world ON). Can CMake recognize the BUILD_helloworld_world variable?

You mentioned the variable in the original post. Or was this a hypothetical switch you would use to do this? OpenCV needs to support this natively as an option; it’s not trivial to “just combine DLLs”, especially on Windows.

When opencv builds with BUILD_opencv_world set to on, its basically using a single link line to generate the shared library vs multiple link lines.

The easiest way to accomplish what you are doing, would be to follow the same pattern.

Typically, the easiest way to generate a DLL on windows, is you create a an “EXPORT/IMPORT” macro that precedes each symbol being exported

When your library generation is being compiled and reads the header for the library function, the macro is set to declspec(export), when the executable or another library is reading the header its set to declspec(import)

You likely have multiple of these macros, one for each shared library. You will have to modify the macro logic so its defined as external for the logic of “Am I building the unified DLL or my single DLL then export, otherwise import”

Then from a CMake POV, you need to do a couple of things when this flag is turned on.

  • set the compiler flag
  • Generate static libraries where previously shared libraries were created
  • Create a new target shared library, that linked in all the static libraries

VTK does this with its “kits” logic:

If so, how to define the name of the single dll file?

If there are multiple dll files, the name of each dll file is the name of project.

If there is only one dll file, what’s the name of this single dll file?

You need to create a new project with the unified name