Search code examples
c++pathrelative-path

How to set a relative path/How to use $(SolutionDir)


I'm trying to make a project compile no matter where it is cloned, but I don't know how to write the relative path in relation to the solution location, for now this is how it looks

enter image description here

I tried something with $(SolutionDir) but i don't know how to go one step back from it and into the libraries folder as shown in the current absolute paths. Can someone explain what should i do or show an example ?


Solution

  • To go one step back from a $(SolutionDir) you can write $(SolutionDir)\..\.

    You also can go deeper and create a property sheet for your library, so that if you need to use this library in another project, you would need to include only one .prop-file into your .vcxproj.

    Assuming the library name is cereal and property sheet is located in libraries, the .prop-file would look like this:

    <?xml version="1.0" encoding="utf-8"?>
    <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <ItemDefinitionGroup>
        <ClCompile>
          <AdditionalIncludeDirectories>$(MSBuildThisFileDirectory)\cereal\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
        </ClCompile>
        <Link>
          <AdditionalLibraryDirectories>$(MSBuildThisFileDirectory)\cereal\$(Platform)\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
          <AdditionalDependencies>cereal.lib;%(AdditionalDependencies)</AdditionalDependencies>
        </Link>
      </ItemDefinitionGroup>
    </Project>
    

    Then to use it in a project, you add this in your .vcxproj (you can also use gui for this):

    <ImportGroup Condition="..." Label="PropertySheets">
      ...
      <Import Project="..\..\..\libraries\cereal.props" />
    </ImportGroup>
    

    You can also greatly ease your life by using some package manager, namely vcpkg because of it integration with Visual Studio.