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

这一生的挚爱 提交于 2019-12-19 08:08:10

问题


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:

  • How to compile and run C files from within Notepad++ using NppExec plugin?
  • Final Setup of gcc in Notepad++ [using nppexec]
  • Notepad++, NppExec, CreateProcess() failed with error code 2, Windows 8.1

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.


回答1:


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.




回答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:

  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++:

  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:

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\



回答3:


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.



来源:https://stackoverflow.com/questions/27980134/how-to-compile-execute-c-code-from-within-notepad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!