Linking 2 projects together in Visual Studio 19 - c++

I have 2 projects in 1 solution both projects contain cpp/h files I am trying to have a "Base" project where the "Main" project will contain the files from the "Base".
"Base" configured as Utility project hence does not actually compile anything, of course since it does contain cpp files that even if I use the VS reference tool to link the projects together VS still refuses to compile the cpp files from "Base" hence I get linker errors when trying to compile the solution, one would simply suggest that I reconfigure "Base" to be statically linked however since I would prefer to use different config files (header files) on each "Main" project that I use in the future, static linking is not viable.
Another way of asking is can I have 2 projects that behave like a single project?
I would happy to hear suggestions without using CMake.

Solved using a “Shared Items Project” more info here https://devblogs.microsoft.com/cppblog/cross-platform-code-sharing-with-visual-c/

Related

Share C++ source files across solution projects for tempalte use

I have three projects in a visual studio solution in which the code consists of lots of template code. This therefore means I can't compile the code into a DLL or static library because then I can't use the template classes with new types anymore. One of the projects at the moments acts as the 'core' and the other two need to use the headers (by additional include directories) and source files of that core project and use its template classes. I want to find a way to automatically pass the source code files of that core project to the linker in the other projects so they can use the source code. I could copy the files but then im going to be updating the core lots and I do not want to keep copying and adding files to the other two projects. How could I do this? Im not using C-Make, only visual studio and I wish to keep it that way.

Shared Precompiled Headers

How do I share a precompiled header across multiple projects in the same solution in Visual Studio 2019?
As a requirement, I do not want to create a project just to compile the precompiled header, which then is included and referenced by other projects.
I know the feature simply didn't exist in the past and you'd have to use a pre-build event to copy the actual output file for it to each other project. Since VS has changed a lot over the years, I figure it's time to reask this question because there's no information immediately available on google.
I was looking at this question but it seems that it isn't viable in a production environment.
Or is there something better using C++20 modules?
As far as I concerned, you should need a SharedPCH, it is building the pch and the static library. When ConsoleApplication projects reference the SharedPCH one, the build will automatically link the SharedPCH’s static lib, but several project properties need to be changed as well. As those properties need to be changed similarly for all projects, I suggest you could create the SharedPCH.props and CustomBuildStep.props files and imported them to your projects using the Property Manager tool window.For more details I suggest you could refer to the Blog

How can I build a C++ project in Visual Studio 2015 with circular dependencies?

Firstly, this is old code that I inherited and I would love to clean it up and make it perfect someday, but I cannot do that today. Therefore, I ask that answers like "rewrite the whole thing so that there are no circular dependencies" not be given.
I have a solution with 44 projects. Most of the projects build DLLs and a few build executables. There are multiple projects that depend on another project that, in one way or another, requires the first. Some are as simple as A requires B.lib and B requires A.lib and others are longer chains that loop back around.
When I inherited the code, it was not clean and there were old build files still amongst the source, namely .LIB files. Without removing those .LIB files all 44 projects are able to build and all of the old .LIB files are replaced with newly built files. This tells me that if I build a second time, none of the old .LIBs are being used and that all of my deliverables are now entirely generated from current source.
This leaves me with a problem: I wanted to clean out all none source files from version control, but if I do so, then I can no longer build the projects.
To further clearify, the projects that build DLLs create a .LIB file as an intermidiate step and these files are copied into a common folder that is listed in the "Additional Library Directories" property of each project and the individual .LIB files that are needed for a give project are in the "Additional Dependencies" property for that project.
Edit: I am not looking to do a major overhaul of the code. I know that all of the object files that all of the projects need are created. I also know that this structure is not optimal. I do, however, believe that if the linker knew where to look, it could find everything that is needed.
Extract just the parts from A that B requires into a new DLL 'C'. Then make both A depend on both B & C, whereas B just depends on C.
It's not going to be easy, but just keep extracting little bits at a time.

Create a Second C++ Project in Visual Studio 2010

I am looking for detailed steps to create a second Static Lib Project in Visual Studio 2010
that my first project will reference.
This project will be in source control and used by others so the referencing needs to be able to work on all folder structures. (if possible)
I have done this before but have had problems recently. I mostly end up adding random references to everything and every folder in my project until it works as I do not know the correct steps to accomplish it.
This will be my projects folder structure
<Whatever Structure>/MyProject/MainProject
<Whatever Structure>/MyProject/SecondProject
<Whatever Structure>/MyProject/MyProject.sln
I need my SecondProject to be built as a Static Lib library.
Inside my FirstProject I would like to reference files from my SecondProject as
#include <SecondProject/<filename or class or namespace>
As I said above Detailed Steps to accomplish this would be greatly appreciated.
I have searched many other posts but most just pertain to Header Files or they are half the steps.
Thank you.
#include is solely used for headers. This is parsed at compile time. Since you want to use headers from <Whatever>/MyProject/SecondProject as just SecondProject/, obviously <Whatever>/MyProject/ must be among the include directories. Probably the best way to specify it would be as just ../, because that means you don't have to hardcode <Whatever>
After compiling, the next step is linking. The easiest solution here is to go the the property pages of MainProject, Common Properties > Frameworks & References, and use [Add New Reference...] button. Linking will make the compiled functions inside the .lib available.

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://learn.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.