CLion 2020.3 Help

Path variables

Path variables are placeholders that stand for absolute paths to resources that are linked to your project but are stored outside it. If you are working in a team, these absolute paths on the computers of your teammates may differ. With path variables, you can flexibly share your code so all the references to the linked resources are resolved properly as the path variables accept the values according to the configuration on each specific computer.

There are two pre-defined variables in CLion: $USER_HOME$ for your home directory and $PROJECT_DIR$ for the directory where your project is stored.

You can refer to path variables in project settings (for example, when configuring external tools) and run/debug configurations of the following types:

Create and use a path variable

For example, when your program takes input data from a file, you can create a path variable for the file location, and then refer to this variable in a run/debug configuration which you share through VCS with your teammates.

  1. In the Settings/Preferences dialog Ctrl+Alt+S, go to Appearance & Behavior | Path Variables, and click the the Add button button.

  2. Enter the name of the new variable (for example, DATA_PATH) and its value that points to the target directory with the data file on your disk:

    Creating a new path variable

  3. In the configuration settings, click Icons general add dark in the required field (Redirect Input in our example). Select the variable in the Macros dialog:

    Using a path variable in configurations

  4. Set the Store as project file checkbox and specify the location where the file with your configuration will be stored:

    Saving a configuration into a file

  5. You can check the contents of this file (which is <project directory>.idea/runConfiguration/<configuration name>.xml by default):

    Path variable in the .xml file for a configuration

  6. Now if you share this .xml file through VCS, your teammates will get the configuration with $DATA_PATH$ in it, and they will able to change the value so that it points to their location of the input data file.

Ignored path variables

When you open a project, CLion checks if there are any unresolved path variables. If the IDE detects any, it will ask you to define values for them. If for some reason you don’t want to do that (for example, if you are not going to use files or directories with the unresolved path variables), you can add them to the list of ignored variables.

You can also use the list of ignored variables if, for example, a program argument passed to the JVM in a run/debug configuration has the same format as an internal ($SOME_STRING$) path variable. In this case, you can add this parameter to the list of ignored variables in order to avoid confusion. Enter SOME_STRING to the Ignored Variables field in the Path Variables dialog.

Last modified: 08 March 2021