75

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?

MLM
  • 3,660
  • 3
  • 41
  • 66
Ali Ahmed
  • 1,749
  • 6
  • 20
  • 29

5 Answers5

191

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).

eckes
  • 64,417
  • 29
  • 168
  • 201
  • 7
    Note that these are **round brackets ( )**, _not_ curvy braces { }. I've been erroneously trying to use macros with the latter for fifteen minutes now. – hauzer Jan 22 '16 at 06:27
  • 1
    @hauzer: thanks for the hint. I incorporated your comment into my answer. – eckes Jan 22 '16 at 06:31
  • Is there a C# project equivalent? – Chiramisu Feb 22 '20 at 00:21
  • 1
    @Chiramisu I guess the variables are the same there? See https://stackoverflow.com/a/830307. – eckes Feb 22 '20 at 06:09
  • Is there any coding guideline from MS recommending include path to visual studio rather than including relative path. – Mayur Aug 06 '21 at 16:46
  • This answer helped me solve the issue than the accepted answer. Somehow relative path didn't work for me, but using predefined macros seems more logical as you have better reference point! – ZoomIn Aug 17 '22 at 04:52
46

If I get you right, you need ..\..\src

MByD
  • 135,866
  • 28
  • 264
  • 277
28

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.

MLM
  • 3,660
  • 3
  • 41
  • 66
yvan vander sanden
  • 955
  • 1
  • 12
  • 13
8

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.

Michael Haephrati
  • 3,660
  • 1
  • 33
  • 56
0

There are a couple of hints you need to know.

consider your app is running under c:\MyRepository\MyApp

a single dot on your path means the folder where your app runs. So if you like to reach some folder or file under MyApp folder (imagine c:\MyRepository\MyApp\Resources\someText.txt) you can do it like var bla = File.Exists(./Resources/someText.txt)

and you can go one level up with double dots (..) think about a folder under c:\MyRepository\SomeFolder\sometext.txt for MyApp, it will be like var bla = File.Exists(../SomeFolder/someText.txt)

and it is possible to go 2,3,4.. levels up like

../../SomeFolder (2 levels up)

../../../SomeFolder (3 levels up)

and path starting with no dots means the drive root. var bla = File.Exists(/SomeFolder/someText.txt) will look for the c:\SomeFolder\someText.txt in our scenario.

Arthur Cam
  • 549
  • 6
  • 18