How to auto add dependent projects to include path in Visual Studio - c++

So I've always had to use the project's "Additional Include Directories" property to set relative paths to other (library) projects that I'm dependent on for a given solution.
So the solution structure is essentially:
My App.sln
-> My App.vcxproj
-> My Lib.vcxproj
What I want, is for My App project to automatically have the path to My Lib project files added as an entry in its "additional include directories" simply because My App is dependent upon My Lib.
I set the library / linkage dependency by adding it under "References" for My App, which beautifully means that My App will automatically link against My Lib's output .lib. PERFECT!
But... I don't see how to get the .h files from My Lib to be included into My App without manually specifying its path (or relative path) to My App project's "additional include directories" property.
I have lived with this for years, but I had to solve the linkage issue for a dependent dependent and found a very slick way to do that without having to touch project settings.
Which makes me think - surely there is a way to do the same for "project A is dependent on project B and hence should automatically search project B for #include references"
Am I out on wishful thinking limb? Or am I missing some great facility in VS?

Related

vs 2017 static library "No Such File or Directory"

I'm having problems importing my custom static library into my project. I have my library in the same solution as the project that I am trying to include it in.
In the VC++ Directory section of the project properties I have put "$(SolutionDir);" in the Include Directories and "$(SolutionDir)Debug\;" in the Library Directories. It is my understanding that the "$(SolutionDir);" is a macro that defines the root directory right? As the library shares the same solution as the project it also shares the same root directory and the .lib file that is created is in the Debug folder of the root directory.
In the Linker/Input section, I have put "GameEngine.lib;" in the Additional Dependencies. "GameEngine.lib" is the file name of the .lib that is in the Debug folder of the root directory.
When I'm trying to include anything from the library it just doesn't even recognise it as existing when writing:
#include <GameEngine/Rendering/Model.h>
Doing it the way below shows that it can actually find the file, meaning the path exists and is correct but I get linker errors doing it this way.
#include "../GameEngine/Rendering/Model.h"
I am using Visual Studios 2017.
Link to GitHub repo: https://github.com/TheHoester/OpenGLGameEngine.git
Thank you
You need to make sure you set the directories for all build configurations. You have only set the include directories for the "x64" platform so building the "Win32" platform won't work. After adding the directories to the "Win32" platform your project builds correctly.
Note that it is more usual to use "VC++ Directories" for system includes and libraries and to use "C/C++/General/Additional Include Directories" and "C/C++/Linker/General/Additional Library Directories" for linking to your own libraries. There isn't any problem that I'm aware of for using the "VC++ Directories" settings though, just make sure not to modify any of the existing paths in there.

Is there a way to link classes from different solutions together in Visual Studio?

I have multiple solutions for different project I have worked on but the project I am currently working on rely on a class from a previous solution. I was wondering if there was an easy way to link these solutions together so I have access to all the previous classes.
The most maintainable way to do this is to add the source project's header file directory to your target project's include list.
In the project's configuration properties page, go to:
C/C++ -> General -> Additional Include Directories.
If the source project is compiled into a library, dynamic or otherwise, you will need to link with the corresponding .lib file.
To do this, go to "VC++ Directories" and add to the "Library Directories" field the path containing the .lib file.
Note that you can also add your source project's header directory in this page as well.
After that, go to Linker->Input and add your library's .lib file name.
You will now be able to #include<your_header_file.h>.
Note that you will need to be careful about the choice of directory, as files with the same names as standard or platform-specific headers will cause problems.
EDIT:
I do not know of a way to avoid editing the VC++ Directories page in the project's property page to add a library directory. Why is this a problem? Things like Intel's C++ composer like to step all over these fields (you have to let it make a mess, then you can clean it up).

What is the difference between "Include Directories" and "Additional Include Directories"

In configuration properties of my project, under the "VC++ directories" there is an entry for "Include Directories". But under "C/C++" option, there is another entry called "Additional Include Directories". Same thing happens with library directories.
What are the difference between these two entries?
This is awkwardness that got introduced in VS2010. The VC++ Directories settings used to be located in Tools + Options, Projects and Solutions, VC++ Directories. Global settings that applied to every project that was built on the machine. It is still there but points out that you should now change it in your project settings. A side-effect of the build engine overhaul in VS2010, enabling building with msbuild. Removing the per-project settings would have been logical but that would break too many existing projects.
As such, it is probably best to treat the VC++ Directories settings as the machine default. It is automatically preset by the VS installer. Tinker with it only if you need an unusual directory search order, putting the custom locations last. Very unusual to do so.
It does work however. And it did get taken advantage of eventually. Empowering the Platform Toolset setting in VS2012 and up. Different compiler, different linker, different #include directories, different linker search path. Modified with just one setting, nice.
CONFIGURING INCLUDE PATHS
VC++ Directories: Include Directories
this value is inherited from the INCLUDE Windows environment variable which is defined outside of Visual Studio
environment variables can be: global to the computer or have user level scope
The INCLUDE and LIB environment variables are created when the Microsoft Windows SDK is installed with Visual Studio.
C/C++: Additional Include Directories
is a project level setting... you will have to define this value for every project in your solution
this value can be persisted to source control
ADDITIONAL NOTES
Which one should I use?
The decision to use Include Directories or Additional Include Directories will depend on your organization's development process. In my opinion, it is more important:
that you are able to consistently and reliably re-create the development environment (think: handing off source code to another developer)
for developers within an organization use a consistent approach
A Note About Macros
The C++ project configuration macros (not to be confused with C++ pre-processor #define directive) inherit content from different sources. Macros like...
$(Include) inherit their values from Windows environment variables
$(OutDir) inherit their values from Visual Studio IDE
REFERENCES
Environment Variables (general introduction)
How to set the path and environment variables in Windows
The Include Directories corresponds to the environment variable INCLUDE.
Directory settings displayed in the window are the directories that
Visual Studio will search for include files referred to in your source
code files. Corresponds to environment variable INCLUDE.
While the Additional Include Directories are passed via a command line argument (i.e. the \I option).
What is the difference between #include <filename> and #include "filename"?
"Include Directories" -> #include <header>
"Additional Include Directories" -> #include "header"

Sharing files between projects in Visual C++ 10

I've got a relatively simple setup in Visual Studio 2010- a main application and a DLL it depends on. I have a header that defines it's interface- how can I set up the header to be included in both projects? They are both in the same solution.
See my answer to this question, but its just that you are using a dynamic library.
In summary, the main application project should reference the DLL project for the purposes of linking, and the main project should include folder references so the compiler can access header files.
In the VC different projects are placed in a different subdirectories of the solution dir.
If you want include files from the other project, you need to explicitly add it's dir to the include search path:
right click on the ".exe" project in the solution explorer. Choose properties.
In the property window go to the "C/C++" section.
There is the "Additional Include Directories" property. Add your "dll" project directory there.

DLL References in Visual C++

I have had C++ experience but not MSVC.
What I am trying to do is incorporate a .dll from an open source project into my project. The code is available and I have built it. I have the .dll as well as the .lib which as I understand it is required for C++ projects.
Now unfortunately there is no simple "Add Reference", drop my .dll into an include directory and add that to my solution. I have edited the project property pages, the C/C++ Additional Include Directories option as well as adding the .lib as an additional linker dependency. I have created an include directory for the dll and lib inside my solution tree.
My problem is when I try to include the header files from the documentation, VS output spits out error messages. Now I realize that I am using the dll/lib combo and that the .h files are not present in my solution so how do I add the proper includes? I am using QT toolkit also which is working but how I add the other header / dll from the open source library eludes me.
Can someone please point me in the right direction.
You need to do a couple of things to use the library:
Make sure that you have both the *.lib and the *.dll from the library you want to use. If you don't have the *.lib, skip #2
Put a reference to the *.lib in the project. Right click the project name in the Solution Explorer and then select Configuration Properties->Linker->Input and put the name of the lib in the Additional Dependencies property.
You have to make sure that VS can find the lib you just added so you have to go to the Tools menu and select Options... Then under Projects and Solutions select VC++ Directories,edit Library Directory option. From within here you can set the directory that contains your new lib by selecting the 'Library Files' in the 'Show Directories For:' drop down box. Just add the path to your lib file in the list of directories. If you dont have a lib you can omit this, but while your here you will also need to set the directory which contains your header files as well under the 'Include Files'. Do it the same way you added the lib.
After doing this you should be good to go and can use your library. If you dont have a lib file you can still use the dll by importing it yourself. During your applications startup you can explicitly load the dll by calling LoadLibrary (see: http://msdn.microsoft.com/en-us/library/ms684175(VS.85).aspx for more info)
Cheers!
EDIT
Remember to use #include < Foo.h > as opposed to #include "foo.h". The former searches the include path. The latter uses the local project files.
The additional include directories are relative to the project dir. This is normally the dir where your project file, *.vcproj, is located. I guess that in your case you have to add just "include" to your include and library directories.
If you want to be sure what your project dir is, you can check the value of the $(ProjectDir) macro. To do that go to "C/C++ -> Additional Include Directories", press the "..." button and in the pop-up dialog press "Macros>>".
You mention adding the additional include directory (C/C++|General) and additional lib dependency (Linker|Input), but have you also added the additional library directory (Linker|General)?
Including a sample error message might also help people answer the question since it's not even clear if the error is during compilation or linking.