简体   繁体   中英

Cmake, .lib, dll and avoiding multiple .lib copies inside binary

All in all, what I want is to avoid duplicate .lib files in several .dlls since the generated .dlls will be used together in a .exe file and this makes global state duplicated in the .lib file duplicated.

Concrete Problem :

I have a project with 3 libraries and one .exe:

  • libDependentA.dll
  • libDependentB.dll
  • libIndependent.lib -- static library

The dependencies are as follow:

  • libDependentA.dll -- depends on -> libIndependent.lib
  • libDependentB.dll -- depends on -> libIndependent.lib

I have an .exe. Depends on both libDependentA.dll and libDependentB.dll

This makes two copies of my .lib into the .exe, which is what is causing problems.

Solutions?

  • Preferred solution. Don't know if possible, when googling no info.

Make a .dll out of the libIndependent.lib . How can I do this? I couldn't make a dll out of .lib files in cmake through add_libary(newdllfromlib SHARED) . It has no source files dependencies and I tried to target_link_libraries into newdllfromlib . Is there an easy way without using empty source files or other tricks?

  • Make libDependentB.dll depend only in libDependentA.dll and remove libIndependent.lib from libDependentB.dll . How can I do that in cmake? The problem here seems to be that cmake transitively propagates to all the other places libIndependent.lib , including the .exe, if it appears in target_link_libraries(libDependentA... . If I add LINK_PRIVATE , then, I cannot set up the configuration I want in cmake, because I must link libIndependent.lib again into the libDependentB.dll . Anyway to do it?

Duplication of symbols in .dlls from linked static libraries is what static libraries are supposed to do. If the static library libIndependent is 3rd party code, then the developers of libIndependent probably have some reason why they chosen the static library and not a dynamic library.

If you use symbols from libIndependent in both libDependentA and libDependentB and you do not have source code of libIndependent , the short answer is that you cannot do it directly. Static libraries are usually build with different set of flags/defines than shared libraries (most obvious is exporting the symbols dllexport / dllimport as you noted). You can export some libIndependent symbols from libDependentA by def file . But usually only symbols that are used in dll are present in the library ( libDependentA ) and you can run into various other problems.

However you can create a shared library wrapper libIndependentWrapper.dll , where you create a wrapper function for every function from libIndependent you need. The wrapper functions would have new header with dllexport / dllimport .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM