Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio: project reference vs link static lib

In Visual Studio when I want use existing project in my solution I can either add it as a reference, or add it as a statically linked lib in other project properties. I'm confused when I should use with option and which benefits those two approached has. PS: talking about C++ projects.

like image 467
Bruice Avatar asked Oct 21 '25 20:10

Bruice


1 Answers

In my opinion, when a project needs to use a library, you have to do at least two things:

  1. Include header files corresponding to the library
  2. When linking, add the corresponding .lib in VS

File When there are multiple projects in your solution, for example, Project A is exe and Project B is static lib. If Project A depends on Project B, you need to do three things:

  1. Include B's header file in A project
  2. Add b.lib to the link option of Project A, and pay attention to the difference between debug/relase
  3. Whenever B is updated, you need to compile B first and then recompile A.

Since the above actions are standard actions, VS provides a function called Add Reference, which automatically completes these two actions:

  1. When linking A, automatically bring b.lib, debug/release can automatically distinguish
  2. When project B changes, if project B is compiled before project A is compiled

If you want to konw more information, you could refer to Microsoft Docs about Create and use a static library and Manage references in a project.

like image 170
Barrnet Chou Avatar answered Oct 23 '25 11:10

Barrnet Chou



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!