-1

I have a dependency like this in a large project:

MyEXE --> MyDLL --> MyStaticLib

Which means that my executable (MyEXE) is dependent on a dll (MyDLL) and the dll uses an static lib (MyStaticLib). The problem is that I can call the MyStaticLib methods from MyEXE and it compiles and links without any issues. I checked the Linker/Input page on Visual Studio (2015) of MyEXE project, but I cannot find MyStaticLib as one of the dependencies of MyEXE.

Now the questions are:

  1. How it is possible for MyEXEto call MyStaticLib methods without any explicit dependancies?
  2. Is there any tools on Windows to list all of the static libs used by an executable? (here is a solution for Linux)

Update 1: There is no #pragma comment(lib, ...) directive in the headers of the MyStaticLib. So, the static lib cannot be linked to the .exe using this directive.

TonySalimi
  • 8,257
  • 4
  • 33
  • 62
  • After compiling the dll and without the .exe built did you try deleting the static library from your drive and see if the executable is still produced? – drescherjm Mar 30 '20 at 15:41
  • 2
    If it's a *static* library it will have been included wholesale in the other (dynamic) library that uses it. No runtime dynamic dependency. That's the whole *point* of static libraries. – Jesper Juhl Mar 30 '20 at 15:41
  • @drescherjm I will check. – TonySalimi Mar 30 '20 at 15:43
  • @JesperJuhl So, you mean if I link to a DLL, I can use ALL of the methods in static libs that are used by that DLL? – TonySalimi Mar 30 '20 at 15:44
  • 2
    A dll will include the static library. However I don't think it should expose that to the executable meaning the executable should also need to link to the static library if it needs to use it directly. – drescherjm Mar 30 '20 at 15:46
  • @Gupta when the DLL was linked, all code it needed from any static libraries it was linked to was added to the dll. Doesn't mean that you can use the static lib transiently though, just that the dll doesn't have a runtime dependency on the static lib. – Jesper Juhl Mar 30 '20 at 15:46
  • @JesperJuhl, yes, the static libs were added to the dll. But are they exposed to the dll clients (e.g. an exe that uses the .dll). I do not think so? – TonySalimi Mar 30 '20 at 15:48
  • @Gupta No. It doesn't mean you can use the static library transiently. The executable must link with it itself if it wants to use it. – Jesper Juhl Mar 30 '20 at 15:53
  • 1
    https://learn.microsoft.com/en-us/cpp/build/reference/verbose-print-progress-messages?view=vs-2019 – Hans Passant Mar 30 '20 at 16:15

1 Answers1

3

MSVC at least has a mechanism for implicit dependencies, done through the #pragma comment(lib, ...) directive. Check the headers for the static library and make sure that there is no such.

Also, if using a static library provided through 'vcpkg' and you have done "vcpkg integrate install" an MSBuild file is added to the project build system that automatically imports everything that vcpkg generates.

Also, link.exe has a /VERBOSE:LIB option that will print out the libraries that are searched, though it won't tell you why that particular library was added to the build.

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
  • There is one option in Linker property page called, `Link Library Dependencies`. Does it means that when this option is set to `Yes`, the .exe will automatically linked to the dependent .lib files? – TonySalimi Mar 31 '20 at 09:14
  • The link library dependencies option causes the project to link any referred-to project (at least so long as the source project does not have the "Ignore Import Library" option set), but there are other ways to specify libraries (the "additional dependencies" line on the Linker->Input page, and #pragma comment(lib, ...) being the usual methods of specifying .lib files directly). Because of #pragma comment(lib, ...) and the way the build system can import additional files it is not always possible to determine from the property pages what is going to be linked against. – SoronelHaetir Mar 31 '20 at 20:17