Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are consequences of adding references to C++ project in Visual Studio?

I have been using Visual Studio for quite some time now, developing mainly for C++. I was often in need to create solutions, that contained multiple modules (projects) - for example utility library, that was consisted of couple .dll files.

When there is a need for one module (A) to use another (B), there is standard pattern for this:

  1. Include required header.
  2. Link output library file from B (for example, in VS: Project Config -> Linker -> Input -> Additional Dependencies -> 'B.lib').
  3. [Optional] Setup proper build order (so B is built before A).

Recently I started to play around with C#, because I decided to develop some GUI-based tools for my engine with it (it's much easier, than using C++ and external libraries like Qt or wxWidgets). I learned, that in C#, such dependencies are set using 'References':

enter image description here

I was very surprised, when I discovered, that this option is also applicable for C++ projects!

Indeed, after I created sample solution and set dependencies this way, everything was working fine, without any additional configuration like "Linker input" or something.

My question is: what does exactly this option do for C++ projects? I am interested in all profits and potential trade-offs.

I know already, that it causes linking output from other projects set as dependencies. Anything else? Perhaps some runtime dependencies between referenced modules? How does it affect generated output?

like image 801
Mateusz Grzejek Avatar asked Apr 11 '15 13:04

Mateusz Grzejek


Video Answer


1 Answers

It was originally meant to only be used in C++/CLI projects. And did the exact same thing that adding references to a C# project did, you pick .NET reference assemblies that you need to get the project to compile.

But this confused a great many C++ programmers, they thought it should contain something generally useful. Probably because it is under the "Common Properties" heading. Lots of questions about it.

Fast forward to VS2010, a version that was unfinished. One of the few cases where a Microsoft project overshot its intended shipping date. They got an extra 6 weeks to work down the bug-list. But that wasn't enough, the feature that was supposed to make it easier to link dependencies was not actually implemented or disabled.

So at VS2012 they decided to do it a different way and make Add Reference useful to a native C/C++ project as well. You always pick a project reference, it needs to be a static library or a DLL project. One that produces a .lib file. And it automagically tells the linker to link that .lib file. Nothing else, it simply adds the .lib file to the linker command line. Works well.

Update: changed again for VS2015, it now has a References node. Right-click it to add references to another project.

like image 80
Hans Passant Avatar answered Oct 07 '22 17:10

Hans Passant