Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the "Link Library Dependency" linker option actually do in Visual Studio 2010 - 2015 and upwards?

Up to VS2008, you set your native C++ project dependencies up in the solution file (Project Dependencies ...) and if (by default) the Linker Option

Properties -> Linker -> General : Link Library Dependencies = Yes 

is set, the Visual Studio Build will automatically link in the .lib files of all projects (DLLs, LIBs) that this project is dependent on will be "statically" linked in.


Side Note: Microsoft changed how the dependencies worked in VS2010 and you are now supposed to add the dependency directly to the project

Common Properties -> Framework and References : (List of depenencies)       (each lib/dll has a separate option:       Project Reference Properties -> Link Library Dependencies : True|False 

I'm fine with that. This is not what this question is about.

(One explanation here: Flexible Project-to-Project References.)


It is still possible however to define project dependencies on the Solution level and the General Linker option is also still there. However it doesn't work. See:

  • Link Library Dependencies not working?
  • Did Visual Studio 2010 break "Project Dependencies" between C++ projects?
  • Visual Studio 2010 not autolinking static libraries from projects that are dependencies as it should be supposed to
    • "but oddly enough, without removing the old UI, or in any way indicating that it no longer works"

and especially see here (acutal question follows)

  • MS Connect Bug 586113: "Link Library Dependencies" does not work

Where Microsoft confirms that the Linker Option doesn't do what the rest of the world's population expects it to do, and adds the following explanation:

Thanks for reporting this feedback. The issue you are experiencing is by design. "Link Library Dependency" is a flag that only dictates whether or not to pass the library as an input to the linker. It does not find the dependency automatically. As a customer you will have to define the depedency manually as you suggest.

Can anyone explain what that means, or more to the point: What does the "Link Library Dependency" linker option actually do in Visual Studio 2010?

What is an "input to the linker" that isn't actually linked supposed to be?

like image 651
Martin Ba Avatar asked Oct 07 '11 06:10

Martin Ba


People also ask

What is Linker Visual Studio?

The Microsoft Visual C++ suite includes a LINK. EXE program that works primarily with object files as input, to produce an executable as output, but also is capable of much other work concerning these and related files. The different kinds of work are separated by having LINK.

How do I change external dependencies in Visual Studio?

Just right click on your project and select properties. There you will get another set of options under 'Configuration Properties' . Go to C/C++ and under that -> General -> Additional Include Directories ( where all the header files of third party is there ).


2 Answers

You have to give the setting the proper value to bring clarity:

enter image description here

like image 53
Hans Passant Avatar answered Oct 02 '22 02:10

Hans Passant


2017 Re-Run. Yay.

TL;DR

This Option sets the default value(a) for the actual Link Library Dependecies on each project reference. If each project reference has LinkLibraryDependecies set, then it is in effect meaningless.

However, when adding a new reference, by default (in VS2010 and 2015) the new <ProjectReference> element in the vcxproj file does not have the setting set, so this option is relevant in that it provides the default for all newly added references, as long as their value isn't modified.

(a): It really should be the same for all Configurations (Debug/Release) and Platforms (Win32/x64) or things get really complicated.

Gory details

Hans pointed out that it appears to not do anything in VS2010 as such. However, this doesn't mean that it actually ain't used by VS/MSBuild.

The crux is how this option is inserted into the vcxprj file and how the defaults work for the <ProjectReference> setting in the msbuild file.

The setting on the Linker dialog as shown above is inserted as:

Not actually implemented

<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> ...   <ItemDefinitionGroup>     <ClCompile> ...     </ClCompile>     <Link> ...     </Link>     <ProjectReference>       <LinkLibraryDependencies>This option is not used by VS 2010!</LinkLibraryDependencies>     </ProjectReference> ...   </ItemDefinitionGroup> </Project> 

And while it appears to be somehow grouped together with the Link Option, that's just there to confuse you.

What this actually does in a given vcxproj file (or when coming from a .propsfile), is to set the default value of the Link Library Dependencies Value for each project dependency on the Frameworks and References section in a VS2010 VC settings dialog --

Link Lib in the References 2010

-- or in the subtree of the VS2015 References --

Link Lib in the References 2015

And this is relevant, because when you add a new project reference, the default entry in your vcxproj file will look like this:

...   <ItemGroup>     <ProjectReference Include="..\W32DynLib1\W32DynLib1.vcxproj">       <Project>{96be134d-acb5-....-....-....bb6fe4a7}</Project>     </ProjectReference>   </ItemGroup> 

You'll notice that the <LinkLibraryDependecies>true|false</..> sub element is missing here: This means you "global" setting will actually be used to set the default value.

If your global setting is false (or No), the project reference won't link in anything. If it's true, it will link in.

What's more:

  • If this setting, LinkLibraryDependency, is completely missing from your settings, it will default to true (from the Microsoft.Cpp[.Common].propsfile in the MSBuild folder).
  • If you happen to have the value This is not used in your global setting, this will be interpreted as true.
  • If you have the value False is the new truth!, or maybe No way in this setting, it will also be interpreted as true by the build.
  • The VS2015 GUI will display a warning if it cannot interpret the string here: String value 'False is the new truth!' cannot be translated to any value from type Boolean.
  • The VS2010 GUI will display False for ALL values, except false, even though this is then interpreted as true when building the project.

What's even more:

It seems that when converting old Solutions with vcproj files, the converter will take the old dependencies that were specified in the sln and the value of the vcproj project's Linker option, and actually set the LinkLibraryDependency for each ProjectReference it inserts into the new vcxproj - thats one reason I thought that this is a dead option for so long - most of our projects have a conversion history dating back to VS2005.

like image 42
Martin Ba Avatar answered Oct 02 '22 02:10

Martin Ba