How to compile/execute C++ code from within Notepad++

Bart Humphries picture Bart Humphries · Jan 16, 2015 · Viewed 58.5k times · Source

I was reading http://daleswanson.blogspot.com/2012/07/how-to-compile-c-code-in-notepad-with.html and decided to try that, so that I can continue to write code in Notepad++ and have a shorter compile/run cycle.

When I tried to enter compilation/run code into NppExec, it's not working. The code I have now is:

npp_save
cd "$(C:\Users\Bart\Desktop\new delete me)"
g++ "$(test.cpp)" -o $(testme.exe) -march=native -O3
NPP_RUN $(testme.exe)

That was based off the first link I gave:

npp_save
cd "$(CURRENT_DIRECTORY)"
g++ "$(FILE_NAME)" -o $(NAME_PART) -march=native -O3
NPP_RUN $(NAME_PART)

Notepad++ gives me the following information in its Console:

NPP_SAVE: C:\Users\Bart\Desktop\new delete me\test.cpp
CD: 
Current directory: C:\Program Files (x86)\Notepad++
g++ "" -o  -march=native -O3
CreateProcess() failed with error code 2:
The system cannot find the file specified.

NPP_RUN:
- empty command

From other pages it seemed as though I just needed to paste that code in, that the all caps words aren't meant to be replaced but are variables. So I used this code:

npp_save
cd "$(CURRENT_DIRECTORY)"
g++ "$(FILE_NAME)" -o $(NAME_PART) -march=native -O3
NPP_RUN $(NAME_PART)

Which gave the following in the Notepad++ Console:

NPP_SAVE: C:\Users\Bart\Desktop\new delete me\test.cpp
CD: C:\Users\Bart\Desktop\new delete me
Current directory: C:\Users\Bart\Desktop\new delete me
g++ "test.cpp" -o test -march=native -O3
CreateProcess() failed with error code 2:
The system cannot find the file specified.

NPP_RUN: test
- the specified file was not found

Here's what I've done to get things set up:

I downloaded mingw-get-setup.exe from http://sourceforge.net/projects/mingw/files/ which installed the MinGW Installation Manager. I then used it to install the mingw32-gcc-++ package, as well as the mingw32-libz.dll and mingw32-libz.dev packages

In Notepad++ I used the Plugin Manager to install the NppExec plugin.

I can get my code to run by first manually compiling it in a command window. Notepad++ complains that it's missing a library, so I'm using the following flags when I compile: g++ test.cpp -static-libgcc -static-libstdc++

If I hit F5 in Notepad++ (or click Run in the Run menu), I can choose the a.exe file that's created from my command line compilation, and it will popup a command window and run that code, so that works fine.

But it seems as though my change directory command isn't working for some reason in NppExec when I try to automate the compile/run.

Here are some other stackoverflow posts I've found that address similar problems, but which don't seem applicable to me. I don't have any points, so I can't reply to any of them:

Well, it looks like the first post I linked to has a partial solution -- it looked like (despite the mention of c files in the post name) that it was summarizing how to compile perl scripts. It said to put the following in the NppExec window:

NPP_SAVE
CD $(CURRENT_DIRECTORY)
C:\MinGW32\bin\gcc.exe -g "$(FILE_NAME)"
a.exe

It just had "a" on the final line, but that's the same as "a.exe" and it's more human readable this way. That being said, this is not a full solution. This just runs the file in Notepad++'s internal console, at the bottom of the screen and I'd like it to popup a window, as would happen if I used Notepad++'s F5 to run my compiled program from its directoy.

Answer

Just a Wannabe picture Just a Wannabe · Feb 25, 2015

I've got a working code (that I edited a bit) from here:
http://windowsbro.blogspot.hu/2012/10/compile-with-notepad-any-language.html

npp_save
g++ "$(FULL_CURRENT_PATH)" -o "$(CURRENT_DIRECTORY)\$(NAME_PART).exe"
npp_run $(CURRENT_DIRECTORY)\$(NAME_PART).exe

In this form it compiles the binary next to the source file. If you change the last line to this:

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

then the program runs in the NppExec console with running cmd.exe.