Sunday, October 14, 2012

Compile with Notepad++ (Any language)

Sometimes you wish you had a compiler with Notepad++, so you don't have to start another program just to see the results of your work. Well, there is a solution to this too.
You probably have noticed that Notepad++ comes with few plugins included since the installation. One of them is called NppExec.

NppExec menu

This plugin lets you create command macro withing a built-in console of Notepad++, and prints the results in that console. It's really useful if you want to see the output immediately. For example, this is how a merge-sort algorithm looks in the built-in console window.
Merge-sort algorithm
So, how to compile the file?

First, we need to add the compiler to the PATH variable in our computer. You can see here (after the image of a console) how to do that. If you don't want to mess with the system environment variables, you will need the whole path of the compiler.
For example:
C - C:\MinGW\bin\gcc.exe
Python - C:\Python27\python.exe

Next, click on the Plugins menu, select NppExec, and press Execute. Now, we need to type some commands in here, so the plugin knows what we want. It's good to save the code so we can make it into a shortcut later. Just press Save when you have the script, and give it a name.
Copy paste variant:

cmd /c "$(CURRENT_DIRECTORY)\a.exe"

or if you prefer the full path, just change the second line with this line:

C:\MinGW\bin\gcc.exe "$(FULL_CURRENT_PATH)"

Now, we want a compile button shortcut, don't we?

Well, we need to have the script saved first. In order to do that, go to NppExec->Execute, and check if you have the script in the drop-down menu. If not, press Save when you have the script ready (Check the image above).
Now, go to NppExec->Advanced options. Select the script from the drop-down menu in the bottom-left corner, and press Add/Modify. You will see the script name added in the list above. Press OK, and restart Notepad++.

Next, we need to link our shortcut button, and our script. Go to Settings-> Shortcut mapper. Select the Plugin section, and scroll to the bottom of the list. There you will find the script you just made.

Creating a shortcut for your script.
Mine is Ctrl+Alt+C for C, and Ctrl+Alt+P for Python.

So, how does this work?

First, we tell Notepad++ to save our file, so we can compile the latest version of it. (npp_save)
Then, we compile the file. (gcc <file path>)
Finally, we execute the "a.exe" file which is generated after a successful compiling. (cmd /c <a.exe path>)

The only thing that may sometimes trick you, is that the "a.exe" file is executed even if the code won't compile. For example, you changed the code, it doesn't compile, but the "a.exe" file from the previous version is executed. You just need to notice the errors in the compiling, and you'll know it's not the latest version that is executed.

How to compile other languages?

The process is very similar to this one. We only need to change the second line, because we are using another compiler, and delete the third one if we are not using C/C++, because those languages create an .exe file which we need to executed.




java "$(NAME_PART)"

Note for Java: If your project contains more .java files, you will need to compile each one of them, and execute the main.
NAME_PART = "Main" from "". It takes the filename without the extension.

C++: Same as C.


Alejan_dro on February 13, 2013 at 8:27 PM said...

Good tip. There's also a notepad++ plugin that does this, it's called NCandE. I think it supports 7 languages natively, and more languages can be added in a similar method you described above

Raihan Tusher on March 20, 2014 at 7:03 AM said...

You can compile and run java program in notepad++ without using command prompt

Harshit Pathak on July 19, 2014 at 10:17 AM said...

Nice Article

Atul Vaibhav on August 23, 2014 at 8:00 AM said...

We can abort the execution of "a.exe" if there are syntax errors in our file by modifying the script:
cmd /c g++ $(FILE_NAME) & IF ERRORLEVEL 1 (echo.) ELSE (a.exe)

Anonymous said...

It says this:
CreateProcess() failed with the error code 2:
The system cannot find the file specified.
What do I do?

Anonymous said...

Change the address of the compiler.

SET g++ = C:\Program Files (x86)\CodeBlocks\MinGW\bin\g++.exe
"$(g++)" "$(FILE_NAME)" -o "$(NAME_PART)" -march=native -O3
NPP_RUN cmd /C "$(NAME_PART)" && pause

Anonymous said...

To prevent window from closing after execution in cmd, add "&& pause"
Example for compiling c++

SET g++ = C:\Program Files (x86)\CodeBlocks\MinGW\bin\g++.exe
"$(g++)" "$(FILE_NAME)" -o "$(NAME_PART)" -march=native -O3
NPP_RUN cmd /C "$(NAME_PART)" && pause

Anonymous said...

before, i have many problem as "error code 87, no such user variable , n error code 2". that's all execute's problems...
i don't know i can't solved problems with any alternative above, but i found at

and found simply solution, may if u have same problems, u can try it!

4.7.1. Compiling simple C-programs ---

You can edit, compile & run simple C-programs directly from Notepad++. All you need is the following:
1) download tcc (tcc is a freeware C-compiler, about 1 MB in archive)
2) unpack tcc to "C:\tools\tcc" or create "tcc" subfolder in your Notepad++ folder (for example, "C:\Program Files\Notepad++\tcc") and unpack tcc there
3) run Notepad++ and call NppExec's "Execute..." dialog (Plugins -> NppExec -> Execute... - or just press F6).
4) type these commands in the "Execute..." dialog:

4.1) if tcc is placed in C:\tools\tcc

// save current file
// compile & run with tcc
C:\tools\tcc\tcc.exe "$(FULL_CURRENT_PATH)" -run

4.2) if tcc is placed in $(NPP_DIRECTORY)\tcc

// save current file
// compile & run with tcc
"$(NPP_DIRECTORY)\tcc\tcc.exe" "$(FULL_CURRENT_PATH)" -run

Post a Comment


WindowsBro | Windows tips, fixes, help.. Copyright 2008 All Rights Reserved Revolution Two Church theme by Brian Gardner Converted into Blogger Template by Bloganol dot com