Where is the Project Directory in Microsoft Visual Studio? - c++

Where is the project directory in microsoft visual studio C++? How can you add files so that in your line of code you can write #include "myfile"? Thanks...

Add the directory containing your file to the include directories list:
Project -> Properties -> Configuration Properties -> C/C++ -> Additional Include Directories

Usually a Visual Studio project directory is a sub-directory of a solution directory. However, it doesn't need to be. E.g. you can add a project to multiple solutions.
For any given project, in the project settings the location of the project directory is available via the $(ProjectDir) macro expansion.
You can also use such macro expansions to access ordinary environment variables.
The difference between #include "blah" and #include <blah> is that the former adds a compiler-dependent additional search for the header, before (if that fails) doing the same search as the latter. With most (all?) modern compilers the additional search includes searching in the directory of the including file. Thus,
one way to add a file so that you can write just #include "myfile" in your code, is to place the file in the same directory as the including code.

Related

Visual Studio 2022 intellisense includePath errors

when using the intellisense prompt of VS2022 to automatically include the header file in the code in the Cpp file, the following error always occurs
#include "../Config/UGConfigManager.h"
Is there any way to replace the path "../" with a full path? Like this:
#include "Game/Config/UGConfigManager.h"
EDIT:
In UE5, you need to change NMake's IncludeSearchPath instead of VC++ Directories in Properties -> NMake
You need to add an include path to the "Game" folder.
To set an include path you now must right-click a project and go to:
Properties -> VC++ Directories -> General -> Include Directories
Then add the include directory like so:
C:/foobar/Game
First try using an absolute path. And if that works you will want to use a Macro. Macros allow users to define paths without being specific to their own computer (So other people can use it).
Perhaps what you need is $(ProjectDir) but I can't tell since I don't know where "Game" is relative to your project files.
But as an example:
$(ProjectDir)/Game
It's worth pointing out that what you are doing is interacting with the compiler option /I on the MSVC compiler. Visual Studio is just a gui abstracting it away for you.
Here are the docs on /I (Additional include directories)

How to configure addition library folders for Visual Studio 2013?

I have legacy, non Visual Studio project. Its structure is:
-root_folder
-root_folder/folder1
-root_folder/folder1/main.cpp
-root_folder/folder1/fun.cpp
-root_folder/folder2
-root_folder/folder2/bar.cpp
-root_folder/folder2/foo.h
.... many different folders
I work with project under folder1.
root_folder/folder1/main.cpp:
#include <folder2/foo.h>
....
I have the error: Error can not open source file "folder2/foo.h>".
Of course, I added the folder root_folder in Project -> properties -> c/c++ section -> additional include directories.
I cannot change this source code, so I need right configure project settings.
Also, I have one more problem. Visual studio does not show my folders structure. It just show folder Source Files, which contains all thousands files.
Visual Studio looks in "VC++ Directories" -> "Include Directories" for the include files specified in angle brackets.
You need to add you path there:
https://msdn.microsoft.com/query/dev12.query?appId=Dev12IDEF1&l=EN-US&k=k%28VS.ToolsOptionsPages.Projects.VCDirectories%29&rd=true
P.S. Just noticed that this answer was given earlier, and rejected.
However, it most certainly works; tested with VS2013.

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"

How to make the visual studio 2008 IDE look at the right place for header files. Compiling Qt/C++ program

I am building an app on Qt 4.6.2 using visual studio 2008. I need to include the header <QtGui\QDir>.
Problem : The QDir header includes several headers. Once of them is qfile.h. Now the ide/compiler is unable to include this file and the error I get is this
c:\devprojects\myprojects\nttoolkit\trunk\external\qt\include\qtcore../../src/corelib/io/qfile.h(45)
: fatal error C1083: Cannot open
include file:
'Qt/include/QtCore/qiodevice.h': No
such file or directory
I cannot change the path in the file qfile.h since it is an external file to my project. How do I get it working.
Thanks.
The various places the preprocessor searches for include files is described in the Remarks section here.
Typically for an "SDK" like Qt people will change their VC++ Directories, Projects, Options under Tools | Options | "Projects and Solutions" | "VC++ Directories" so that the Include Files and Library Files lists include suitable Qt directories. That way, when the preprocessor searches for Qt include files, it will look in the right places and when the linker looks for .LIB files it will find them also.
One downside to changing those lists is that they apply to all projects built from that version of Visual Studio. That can be a pain in the neck if you have different projects that use different versions of an SDK. In those sorts of situations one solution is to create environment variables called INCLUDE and LIBPATH and then launch devenv with the /useenv switch to override the VC++ Directories settings from Tools | Options.
Finally, a third option is to provide the additional include and library folders via the project properties. In Project | Properties | C++ | General the first property is "Additional Include Directories". Values placed there are passed to the preprocessor via the /I switch. Similarly the Project | Properties | Linker | General tab has an "Additional Library Directories" property which gets passed to the linker via the /LIBPATH switch.
This third option seems attractive because it lets you set these additional directories on a project by project basis. The downside is that it "hard-codes" some directory names in the project. That can be a real pain if you move the project to a new machine where the Qt files are in a different directory or when you have to move the Qt directories to a different hard-drive, etc.
In VS2010, I go to:
Project Properties -> VC++ Directories -> Include Directories
And set the location of directories containing headers I need to include. I don't currently have access to a VS2008 install, but I think there is a similar configuration option available.

Copying C++ API header files to a common directory

I have a Visual Studio 2008 solution comprised of several projects. Only some header files in each project represents API to the library built from the project.
Is there a way in Visual Studio to copy the files to a common directory prior compilation?
(I want to do it in order prevent including unintentionally header file I'm not supposed to)
Thanks
Dima
Yes, on the project menu, select properties->configuration properties->build events->pre-build event. In the command line section you can enter a copy command with your source and destination paths. You may find the $solutiondir macro useful when enterting your paths.
You can run a script before compilation to do this (see pre-build step) but I don't think this is what you are asking.
Do you mean how can you have header files in another directory used by several projectS? Simply add that directory to the "Additional Include Directories" field in project settings-C++ ->General
>