5

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.

Community
  • 1
  • 1
KJ6BWB
  • 751
  • 2
  • 6
  • 10
  • possible duplicate of [How to compile and run C files from within Notepad++ using NppExec plugin?](http://stackoverflow.com/questions/2506400/how-to-compile-and-run-c-files-from-within-notepad-using-nppexec-plugin) – gbjbaanb Jan 16 '15 at 08:54
  • Seems you want to either update the question with the problem concerning the missing library, or go though the linked questions' answers - C, C++, perl they're all the same as far as how NP++ builds and executes them. – gbjbaanb Jan 16 '15 at 08:56
  • I don't have a missing library. I saw one answer on that linked post which described how to run a C++ program in Notepad's console screen, at the bottom of Notepad++'s window, but I'm on a widescreen laptop (which means a short screen) and I don't have room to have two windows, one vertically above the other, and show several lines in both. I'd like it to popup a console window, as it would if I ran the compiled executable directly with Notepad++'s Run command (F5 by default). – KJ6BWB Jan 16 '15 at 09:07

5 Answers5

9

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.

Sk8erPeter
  • 6,899
  • 9
  • 48
  • 67
  • For me it didn't work until I replaced the last line for the very similar but different: **cmd /c ""$(CURRENT_DIRECTORY)\$(NAME_PART).exe""** , only difference is that it has 2 double quotes on each side instead of yours which has 1. – Santropedro Aug 03 '19 at 04:36
2

As a big Notepad++ fan and a C++ newbie, it took me a bit time to understand and follow the instructions.

There is one part missing here, and if you are like me, starting fresh and having no knowledge of MinGW compiler. You may want to set that one up first.

If the MinGW compiler is not in your environment path (or prefer it not to be: portable versions/policy limitations) then you can follow this.


  1. Install mingw-get-setup.exe

  2. Choose core packages, make sure they all are on the same version otherwise it splashes weird errors, like missing stddef.h files when I compile/run. I picked these ones from MinGW Installation Manager:

MinGW Installation Manager C++ components

  1. Create a script in Notepad++:
npp_save
VCD C:\MinGW\bin\
g++ "$(FULL_CURRENT_PATH)" -o 
"$(CURRENT_DIRECTORY)\$(NAME_PART).exe"
cmd /c "$(CURRENT_DIRECTORY)\$(NAME_PART).exe"
  1. Compile and Execute the program within Notepad++: Execute C++ code in Notepad++

  2. You can add direct execute button or shortcut F5 From nppexec > Advanced Options. Execute button is tiny button at the end of the toolbar: Adding Execute button and keyboard shortcut

Alternatively, you can add MinGW to your environment path if you want to run your compiled program from any location, a good explanation can be found on their website That way you can skip pointing in MinGW directory:

cd C:\MinGW\bin\
Ed The ''Pro''
  • 875
  • 10
  • 22
Gunay Anach
  • 1,193
  • 1
  • 13
  • 19
0

Concerning your last question how to get a.exe started in a window of its own instead of in the notepad++ console: try to replace the last line a.exe with something like:

NPP_RUN cmd /k a

npp_run is a command of npp_exec, here it starts the command "cmd /k a", cmd /k executes the command following the "/k" and it keeps the window open after a.exe has terminated.

Lars Fischer
  • 9,135
  • 3
  • 26
  • 35
0

I wrote myself a working code that asks the user

  1. the output filename
  2. the directory of where it should go
  3. and additional compiler options, such as -m32 or anything else really

I've also taken care of empty directories and names, so that no bugs SHOULD occur. i'm looking forward to improve this but it works for now.

//save file
npp_saveall

//specify directory
//if blank set to src dir
inputbox "Specify Directory" : " save_directory" : $(CURRENT_DIRECTORY)
set DIR = $(INPUT)
if "$(INPUT)" == " " then
set DIR = $(CURRENT_DIRECTORY) 
endif
if "$(INPUT)" == "" then
set DIR = $(CURRENT_DIRECTORY) 
endif


//specify file name
//if blank set to a.exe
inputbox "Choose executable name" : " file_name" : a.exe
set NAME = $(INPUT)
if "$(INPUT)" == " " then
set NAME = a.exe
endif
if "$(INPUT)" == "" then
set NAME = a.exe
endif

//add compiling options
inputbox "Additional compiling options" : " add_options" : -m32 -I"C:\SDL2-2.0.12\i686-w64-mingw32 (x32)\include\SDL2" -L"C:\SDL2-2.0.12\i686-w64-mingw32 (x32)\lib"
set OPTIONS = $(INPUT)

//launch g++ compiler
g++.exe "$(FULL_CURRENT_PATH)" -o"$(DIR)\$(NAME)" $(OPTIONS)

//run compiled prgram
if $(EXITCODE) == 0 then
messagebox "Compilation successfull. Saved in '$(DIR)'. Running $(NAME)" : "MinGW g++ Compiler"
npp_run "$(DIR)\$(NAME)"
else
messagebox "Compilation not successfull."

Note that i add SDL files in my script. simply remove them. Hopefully it is helpful.

L_or_Enzo
  • 23
  • 1
  • 5
0

cd $(CURRENT_DIRECTORY)

"<Directory of g++>\GCC\bin\g++.exe" "$(FILE_NAME)" -o "$(NAME_PART)"

cmd /c "$(NAME_PART).exe"

Use this command

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 01 '22 at 08:27