简体   繁体   中英

How do you add a C++ project reference in Visual Studio?

In C# it's pretty simple to add a project reference that will build the dependency, put the resulting assembly in the original's Debug/ directory and properly compile against that assembly.

So at the moment, I have a project with the Main() and a static library project in one solution. However, when I compile the Main() project and I look in the bin/Debug/ directory I don't find either the Static.lib file or the .obj files, which I think would need to be there, or something... I'm getting linker errors.

I think maybe I have to Configure the project to find the .obj and the .lib file produced by the static library project (which compiles fine, and actually produces those files.)

I'm missing something, and I'm not very well versed in Visual Studio with C++.

How do I add the reference in the Main project to use the library produced by the static library project?

The first thing you'll have to unterstand is, that static libraries are nothing like .NET assemblies. They are linked into the .exe and are not distributed as separate entity.

Your linker errors are most likely a result of you not linking to the library.

There are several ways to define libraries that have to be linked. One is int the project settings under linker -> input -> additional dependencies , the other would be the cheap route via #pragma comment(lib, "path to library")

You can add the name of the .lib files you need by going in project property->Linker->Input->Additional Dependencies

Then you will have to give the folder where your library is in VC++ Directories->Library Directerories.

Here is a very comprehensive answer: visual c++: #include files from other projects in the same solution

It describes the settings for the linker but also other essentials when referencing another C++ project. (especially when coming from C# but not being too versed in C++)

In .NET, one of design goals was due make this process a lot easier and automatic. Mission accomplished there.

In the native C++ world, the process is much more manual. Here is roughly my process for hooking up different modules together.

  • Include all relevant modules in the solution.
  • Set the output directory of each project to the same directory. Either Right click on each project, choose properties and in general, set the output directory to: $(SolutionDir)\\Bin\\$(Configuration)\\$(PlatformTarget) . For reduced headaches, Set this for all configurations and platforms. Then all the files will be placed in somewhere like \\your-solution\\bin\\Debug\\x64 .
  • Set the project dependencies - Right click on each project that will be linking to another -> Choose Build Dependencies and select the referenced projects.
  • Set up the linking process, by Right clicking on the calling project and choosing properties. Go to linker -> Input and add: $(SolutionDir)\\Bin\\$(Configuration)\\$(PlatformTarget)\\MyLibrary.lib
  • For the actual functions that are going to linked to, I set the function declarations to something like (There are a lot of variations on the function declaration, a bit outside of the scope here):

#define DllExport __declspec( dllexport )

extern "C" void DllExport WINAPI` Testing();

  • In actual cpp file of the calling function, I add something like the following:

    #include "..\\MyLibrary\\mylibrary.h"

  • Then in the actual calling function. simply add called function:

    Testing();

If you are building multiple projects in the same solution, use a project reference to automatically link your libraries and executables together:

https://docs.microsoft.com/en-us/visualstudio/msbuild/common-msbuild-project-items?view=vs-2019#projectreference

which is explained pretty well here:

https://milania.de/blog/Project_references_in_Visual_Studio_and_C%2B%2B

The advantage to this approach, is that the build order will be correct. Using Additional Dependencies, as suggested in the other answers, will not maintain the proper build order.

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