How to define relative paths in Visual Studio Project?

205,326

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:

project properties

In the upcoming dialog, hit Macros to see the macros that are predefined by the Studio (consult MSDN for their meaning):

additional include directories

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:

  1. 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.
  2. 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.

Share:
205,326

Related videos on Youtube

Ali Ahmed
Author by

Ali Ahmed

Software Developer

Updated on July 05, 2022

Comments

  • Ali Ahmed
    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
    Ali Ahmed almost 13 years
    I 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
    MByD almost 13 years
    Maybe the project directory is not what you think it is? maybe it is one step deeper?
  • Ali Ahmed
    Ali Ahmed almost 13 years
    if this is the case then what should I do? what should be the path if you are right about project directory.?
  • MByD
    MByD almost 13 years
    ..\..\..\src every .. goes one directory back.
  • iFreilicht
    iFreilicht over 9 years
    It seems to work just fine in VS2013 Update 3 again, not sure about U2 and U1.
  • Anti-Distinctlyminty
    Anti-Distinctlyminty over 9 years
    Shouldn't ($ProjectDir) be $(ProjectDir)?
  • hauzer
    hauzer over 8 years
    Note that these are round brackets ( ), not curvy braces { }. I've been erroneously trying to use macros with the latter for fifteen minutes now.
  • eckes
    eckes over 8 years
    @hauzer: thanks for the hint. I incorporated your comment into my answer.
  • Joma
    Joma almost 6 years
    Nice answer. Tested on Microsoft Visual Studio Community 2017 .
  • Anton Andreev
    Anton Andreev about 5 years
    Click "Apply" and then verify in the 'Linker->Command Line' that the generated absolute paths are correct.
  • ejectamenta
    ejectamenta almost 5 years
    Doesn't work with mapped network drives, then it seems you have to hard code the path!
  • ejectamenta
    ejectamenta almost 5 years
    For network drives you have to move the directory into the project folder and use eg. $(SolutionDir)/path/to/includes
  • Chiramisu
    Chiramisu about 4 years
    Is there a C# project equivalent?
  • eckes
    eckes about 4 years
    @Chiramisu I guess the variables are the same there? See stackoverflow.com/a/830307.
  • Aaron Franke
    Aaron Franke about 3 years
    Where does one put this? What flags do I pass to the link command for the linker?
  • Mayur
    Mayur almost 3 years
    Is there any coding guideline from MS recommending include path to visual studio rather than including relative path.