<< Back

How To Setup Notepad++ for Writing C++ Programs

On This Page


Introduction

This document describes how to install and configure Notepad++ for use with Cygwin on a Windows computer for the course CS-11: Introduction to Programming Concepts and Methodology, C++. Notepad++ is free software and is already installed on CTC and lab machines. You can download a copy of Notepad++ for use at home for free.

Note that Notepad++ only runs on computers using Windows. Usually both Linux and OS X (Apple Macintosh) systems have good text editors already installed. For more information about Notepad++ see https://notepad-plus-plus.org.

Before You Begin

These instructions assume that you have already installed Cygwin as described in the document How to Install Cygwin or How to Install Cygwin Using Babun. If you have not installed Cygwin, do so now.

Setting the System Path

To configure Notepad++ to compile C++ using Cygwin, you must add the Cygwin bin directory to your computer's PATH environment variable. The PATH variable stores the directories where executable code is located.

Caution: Setting the path on Windows is a potentially dangerous operation and could temporarily disable your computer if done incorrectly. If you follow these instructions carefully, you and your computer will be fine. If this warning makes you too nervous, then get someone to double-check you as you perform these steps.

  1. Locate the path to the Cygwin bin directory on your computer, using Windows Explorer or another tool, and record the path. Some usual paths are:
    • Cygwin: ;C:\cygwin\bin
    • Babun Cygwin: ;C:\Users\username\.babun\cygwin\home\username

    For Babun Cygwin, the username is the login name for your computer.

  2. Open the Computer or System window by one of the following methods:
    • In the Search field, type System and select System from the list. If more than one System appears, select the one related to Control Panel.
    • Right-click on My Computer and click Properties.
    • Windows 10: Open the Control Panel, click System and Security and then click System.
    • Older Windows: Open the Control Panel and then click System

    For more help search for Windows System Properties.

  3. Open the System Properties dialog by clicking on the Advanced system settings link.
  4. In the System Properties dialog, click the Environment Variables button.
  5. In the Environment Variables dialog locate the Systems Variables section (bottom half), scroll through the list and highlight the Path variable, and then click the Edit button.

    You will see the Edit environment variable dialog or, on older Windows, the Edit System Variable dialog shown below.

  6. Add the path to the Cygwin bin to the end of the Variable list using one of the following:
    • Windows 10 (click) Click to show instructions
    • Older Windows (click) Click to show instructions

Installing Notepad++

Download the 32-bit Notepad++ installer to a convenient location. Download the 32-bit version because most of the plugins are not yet available for the 64-bit version. At this time, install version 7.4.2, instead of the most recent, and then allow NotePad++ to update after installation.

To install, start (open) the downloaded installer program and follow the installation instructions. Once the installation is complete, configure Notepad++ as described below. Completing the following steps will make your programming easier.

Compiling and Running C++ Programs

After you have set the system path, you can set up Notepad++ to compile programs.

Installation

  1. Open the Plugin Manager.
    Plugins > Plugin Manger > Show Plugin Manager
    
  2. Find the NppExec plugin in the list and install it.
    1. Check the box
    2. Press the Install button
  3. Open the NppExec Execute dialog.
    NppExec > Execute...
    
  4. Copy and paste the following script into the Commands box.
    SET G++ = C:\cygwin\bin\g++.exe
    NPP_SAVE // save current file
    cd $(CURRENT_DIRECTORY) // go to directory of the current file
    "$(G++)" -Wall -Wextra -Wpedantic -std=c++11 -o "$(NAME_PART)" "$(FILE_NAME)"
  5. Change the path to the compiler in the above script to match your installation. For example, the default Babun path is:
    SET G++ = C:\Users\username\.babun\cygwin\home\username
    

    where the username is the login name for your computer.

  6. Save the script with a name like: C++ compile.
    1. Press Save... button
    2. Enter the script name
    3. Press the Save button
  7. Copy and paste the following script into the Commands box.
    SET G++ = C:\cygwin\bin\g++.exe
    NPP_SAVE // save current file
    cd $(CURRENT_DIRECTORY) // go to directory of the current file
    "$(G++)" -Wall -Wextra -Wpedantic -std=c++11 -o "$(NAME_PART)" "$(FILE_NAME)"
    if $(EXITCODE) != 0 goto Done
    NPP_RUN cmd /C "$(NAME_PART)" && pause
    :Done
    
  8. As in step 5, change the path to the compiler in the above script to match your installation.
  9. Save the script with a name like: C++ compile and run.
    1. Press Save... button
    2. Enter the script name
    3. Press the Save button
    4. Close the Execute... dialog
  10. Open the NppExec Advance Options... dialog.
    NppExec > Advance Options...
    
  11. Under the Menu item section, type in the name C++ compile and select the Associated script "C++ compile" created in step 6.
  12. Press the Add/Modify button to create a menu item.
  13. Under the Menu item section, type in the name C++ compile and run and select the Associated script "C++ compile and run" created in step 9.
  14. Press the Add/Modify button to create a menu item.
  15. Open the Shortcut Mapper.
    Settings > Shortcut Mapper
    
  16. Select the Plugin commands tab.
  17. Scroll down until you find the script names created above and click on the row.
  18. Press the Modify button to open the Shortcut dialog.
  19. Set the shortcuts for the scripts as follows.
    • C++ compile: CTRL + 6
    • C++ compile and run: CTRL + 7
  20. Press the Close button.

Compiling and Running Programs

You can now compile and run C++ programs from within Notepad++. To test the set up, copy the following program into a new Notepad++ tab and save the file as hello.cpp to a convenient location like the Desktop.

#include <iostream>
using namespace std;

int main() {
    cout << "Hello, World!\n";
    return 0;
}

To only compile, press the keys Ctrl-6 together or use the following set of menu commands:

  1. Follow the menu to the NppExec Execute dialog
    Plugins > NppExec > Execute
    
  2. Select C++ compile script from the dropdown list.
  3. Press the OK button.

To both compile and run a program, press the keys Ctrl-7 together or use the following set of menu commands:

  1. Follow the menu to the NppExec Execute dialog
    Plugins > NppExec > Execute
    
  2. Select C++ compile and run script from the dropdown list.
  3. Press the OK button.

Styling Code with Astyle

Astyle is a tool for formatting the style of your code. Install Astyle through the use of the Artistic Style Plugin For Notepad++.

Installation

  1. Download NppAStyle.7z (which includes the NppAStyle.dll) from Release NppAStyle.
  2. Copy the NppAStyle.dll to (Notepad++ installation directory)\plugins.

    The (Notepad++ installation directory) is the folder in which you installed Notepad++. The default location is:

    C:\Program Files (x86)\Notepad++
    
  3. Restart Notepad++.

    The NppAStyle menu will appear in the Plugins menu.

Applying Settings

  1. Download the style setting file to an easy to find place like the Desktop or Downloads folder.
    1. Right-click the link: astyle-options.ini
    2. Select Save link as...
    3. Save to the Desktop of Downloads folder if prompted
  2. Open the NppAStyle Options dialog.
    Plugins > NppAStyle > Options
    
  3. Press the Import button.
  4. In the file dialog, find the downloaded file and press the Open button.
  5. Press the OK button

Running AStyle

You can now automatically style your code using the AStyle plugin.

  1. Open the file to style.
  2. Use the following set of menu commands or press the keys Alt-Z.
    Plugins > NppAStyle > Format Code
    

<< Back