CLion 2022.1 Help

Extract define

The Extract Define refactoring method defines the selected set of tokens as macro using #define directive and replaces the set of tokens with macro call in the code; macro definition can be located in the current file or moved to the related header file.

Example

Before

After

int main() { int maxValue = 65536 * 65536; return 0; }
#define MAX 65536 int main() { int maxValue = MAX * MAX; return 0; }

Extract a define in-place

  1. In the editor, select the expression to be replaced with a macro call. You can do that yourself or use the smart expression selection feature to let CLion help you. So, do one of the following:

    • Highlight the expression. Then choose Refactor | Extract | Extract Define from the main or context menu.Alternatively, press Ctrl+Alt+D.

    • Position the caret before or within the expression, choose Refactor | Extract | Extract Define from the main or context menu, or press Ctrl+Alt+D. In the Expression popup menu, select the expression by clicking the required expression. Alternatively, use the Up and Down arrow keys to navigate to the expression of interest, and then press Enter to select it.

  2. If more than one occurrence of the selected expression is found, select Replace this occurrence only or Replace all occurrences in the Multiple occurrences found popup menu.

    Cl extract define multiple

    To select the required option, just click it. Alternatively, use the Up and Down arrow keys to navigate to the option of interest, and press Enter to select it.

  3. Select Put to header checkbox if you want to move macros definition to the header file.

  4. Specify the name of the macro. Do one of the following:

    • Select one of the suggested names from the list. To do that, double-click the suitable name. Alternatively, use the Up and Down arrow keys to navigate to the name of interest, and Enter to select it.

    • Edit the name by typing. The name is shown in the box with red borders and changes as you type. When finished, press Enter.

      Cl extract define macro name
Last modified: 25 July 2022