How to define relative paths in Visual Studio Project?
Solution 1
If I get you right, you need ..\..\src
Solution 2
Instead of using relative paths, you could also use the predefined macros of VS to achieve this.
$(ProjectDir)
points to the directory of your .vcproj
file, $(SolutionDir)
is the directory of the .sln
file.
You get a list of available macros when opening a project, go to
Properties → Configuration Properties → C/C++ → General
and hit the three dots:
In the upcoming dialog, hit Macros to see the macros that are predefined by the Studio (consult MSDN for their meaning):
You can use the Macros by typing $(MACRO_NAME)
(note the $
and the round brackets).
Solution 3
I have used a syntax like this before:
$(ProjectDir)..\headers
or
..\headers
As other have pointed out, the starting directory is the one your project file is in(vcproj
or vcxproj
), not where your main code is located.
Solution 4
By default, all paths you define will be relative. The question is: relative to what? There are several options:
- Specifying a file or a path with nothing before it. For example: "mylib.lib". In that case, the file will be searched at the Output Directory.
- If you add "..\", the path will be calculated from the actual path where the .sln file resides.
Please note that following a macro such as $(SolutionDir) there is no need to add a backward slash "\". Just use $(SolutionDir)mylibdir\mylib.lib. In case you just can't get it to work, open the project file externally from Notepad and check it.
Related videos on Youtube
Comments
-
Ali Ahmed almost 2 years
I have a library and a console application that uses a library. The library has a folder with source and header files.
My project is in a child/inner directory but that library directory that I want to include is in a parent/upper directory.
My project directory:
H:\Gmail_04\gsasl-1.0\lib\libgsaslMain
Includes files are here:
H:\Gmail_04\gsasl-1.0\src
How can I use paths relative to the project directory, to include folders that are in a parent/upper directory?
-
Ali Ahmed almost 13 yearsI tried this...but it says can not find include file that is in this folder. but when I give the complete path it works fine.
-
MByD almost 13 yearsMaybe the project directory is not what you think it is? maybe it is one step deeper?
-
Ali Ahmed almost 13 yearsif this is the case then what should I do? what should be the path if you are right about project directory.?
-
MByD almost 13 years
..\..\..\src
every..
goes one directory back. -
iFreilicht over 9 yearsIt seems to work just fine in VS2013 Update 3 again, not sure about U2 and U1.
-
Anti-Distinctlyminty over 9 yearsShouldn't ($ProjectDir) be $(ProjectDir)?
-
hauzer over 8 yearsNote that these are round brackets ( ), not curvy braces { }. I've been erroneously trying to use macros with the latter for fifteen minutes now.
-
eckes over 8 years@hauzer: thanks for the hint. I incorporated your comment into my answer.
-
Joma almost 6 yearsNice answer. Tested on Microsoft Visual Studio Community 2017 .
-
Anton Andreev about 5 yearsClick "Apply" and then verify in the 'Linker->Command Line' that the generated absolute paths are correct.
-
ejectamenta almost 5 yearsDoesn't work with mapped network drives, then it seems you have to hard code the path!
-
ejectamenta almost 5 yearsFor network drives you have to move the directory into the project folder and use eg. $(SolutionDir)/path/to/includes
-
Chiramisu about 4 yearsIs there a C# project equivalent?
-
eckes about 4 years@Chiramisu I guess the variables are the same there? See stackoverflow.com/a/830307.
-
Aaron Franke about 3 yearsWhere does one put this? What flags do I pass to the
link
command for the linker? -
Mayur almost 3 yearsIs there any coding guideline from MS recommending include path to visual studio rather than including relative path.