C语言 如何使用 NppExec 插件从 Notepad++ 中编译和运行 C 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2506400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:55:34  来源:igfitidea点击:

How to compile and run C files from within Notepad++ using NppExec plugin?

cnotepad++nppexec

提问by Chris Smullian

How can I configure the NppExec plugin for Notepad++?

如何为 Notepad++ 配置 NppExec 插件?

I would like NppExec to compile my C files, run them, and show their output, all within Notepad++.

我希望 NppExec 编译我的 C 文件,运行它们并显示它们的输出,所有这些都在 Notepad++ 中。

回答by David

Here's a procedure for Perl, just adapt it for C. Hope it helps.

这是 Perl 的程序,只需将其调整为 C。希望它有所帮助。

  • Open Notepad++
  • Type F6to open the execute window
  • Write the following commands:
    • npp_save<-- Saves the current document
    • CD $(CURRENT_DIRECTORY)<-- Moves to the current directory
    • perl.exe -c -w "$(FILE_NAME)"<-- executes the command perl.exe -c -w , example: perl.exe -c -w test.pl(-c = compile -w = warnings)
  • Click on Save
  • Type a name to save the script (e.g. "Perl Compile")
  • Go to Menu Plugins-> Nppexec-> advanced options-> Menu Item(Note: this is right BELOW 'Menu Items *')
  • In the combobox titled 'Associated Script' select the script recently created in its dropdown menu, select Add/Modifyand click OK-> OK
  • Restart Notepad++
  • Go to Settings -> Shortcut mapper -> Plugins -> search for the script name
  • Select the shortcut to use (ie Ctrl+ 1), click OK
  • Verify that you can now run the script created with the shortcut selected.
  • 打开记事本++
  • 键入F6以打开执行窗口
  • 编写以下命令:
    • npp_save<-- 保存当前文档
    • CD $(CURRENT_DIRECTORY)<-- 移动到当前目录
    • perl.exe -c -w "$(FILE_NAME)"<-- 执行命令 perl.exe -c -w ,例如:perl.exe -c -w test.pl(-c = compile -w = warnings)
  • 点击保存
  • 输入名称以保存脚本(例如“Perl Compile”)
  • 转到菜单插件-> Nppexec->高级选项->菜单项(注意:这是在“菜单项*”下方)
  • 在标题为“关联脚本”的组合框中,在其下拉菜单中选择最近创建的脚本,选择添加/修改并单击确定->确定
  • 重启记事本++
  • 转到设置 -> 快捷方式映射器 -> 插件 -> 搜索脚本名称
  • 选择要使用的快捷方式(即Ctrl+ 1),单击“确定”
  • 验证您现在可以运行使用所选快捷方式创建的脚本。

回答by Mayur Patil

For perl,

对于 perl,

To run perl script use this procedure

要运行 perl 脚本,请使用此过程

Requirement: You need to setup classpath variable.

要求:您需要设置类路径变量。

Go to plugins->NppExec->Execute

转到插件-> NppExec->执行

In command section, type this

在命令部分,输入这个

cmd /c cd "$(CURRENT_DIRECTORY)"&&"$(FULL_CURRENT_PATH)"

Save it and give name to it.(I give Perl).

保存并命名它。(我给 Perl)。

Press OK. If editor wants to restart, do it first.

OK。如果编辑器要重新启动,请先进行。

Now press F6and you will find your Perl script output on below side.

现在按下F6,你会在下面找到你的 Perl 脚本输出。

Note: Not required seperate config for seperate files.

注意:不需要单独文件的单独配置。

For java,

对于 java

Requirement: You need to setup JAVA_HOMEand classpathvariable.

要求:您需要设置JAVA_HOMEclasspath变量。

Go to plugins->NppExec->Execute

转到插件-> NppExec-> 执行

In command section, type this

在命令部分,输入这个

cmd /c cd "$(CURRENT_DIRECTORY)"&&"%JAVA_HOME%\bin\javac""$(FULL_CURRENT_PATH)"

your *.classwill generate on location of current folder; despite of programming error.

*.class将在当前文件夹的位置生成;尽管编程错误。

For Python,

对于 Python,

Use this Plugin Python Plugin

使用此插件Python 插件

Go to plugins->NppExec-> Run file in Python intercative

转到插件-> NppExec-> 在 Python 交互中运行文件

By using this you can run scripts within Notepad++.

通过使用它,您可以在 Notepad++ 中运行脚本。

For PHP,

对于 PHP,

No need for different configuration just download this plugin.

不需要不同的配置只需下载这个插件。

PHP Plugin and done.

PHP 插件并完成。

For C language,

对于 C 语言,

Requirement: You need to setup classpath variable.
I am using MinGW compiler.

要求:您需要设置类路径变量。
我正在使用 MinGW 编译器。

Go to plugins->NppExec->Execute

转到插件-> NppExec-> 执行

paste this into there

把这个粘贴到那里

   NPP_SAVE

   CD $(CURRENT_DIRECTORY)

   C:\MinGW32\bin\gcc.exe -g "$(FILE_NAME)" 

   a

(Remember to give above four lines separate lines.)

(记得把上面四行分开。)

Now, give name, save and ok.

现在,给出名称,保存并确定。

Restart Npp.

重启 NPP。

Go to plugins->NppExec->Advanced options.

转到插件-> NppExec-> 高级选项。

Menu Item->Item Name (I have C compiler)

菜单项-> 项目名称(我有 C 编译器)

Associated Script-> from combo box select the above name of script.

关联脚本-> 从组合框中选择上面的脚本名称。

Click on Add/modify and Ok.

单击添加/修改和确定。

Now assign shortcut key as given in first answer.

现在分配第一个答案中给出的快捷键。

Press F6and select script or just press shortcut(I assigned Ctrl+2).

F6并选择脚本或只需按快捷方式(我指定了Ctrl+ 2)。

For C++,

对于 C++,

Only change g++ instead of gccand *.cppinstead on *.c

唯一改变的G ++而不是gcc*.cpp替代上*.c

That's it!!

就是这样!!

回答by Pwdr

Here is the code for compling and running java source code: - Open Notepadd++ - Hit F6- Paste this code

下面是编译和运行 java 源代码的代码: - 打开 Notepadd++ -按 F6- 粘贴这段代码

npp_save <-- Saves the current document
CD $(CURRENT_DIRECTORY) <-- Moves to the current directory
javac "$(FILE_NAME)" <-- compiles your file named *.java
java "$(NAME_PART)" <-- executes the program

The Java Classpath variable has to be set for this...

必须为此设置 Java Classpath 变量...

Another useful site: http://www.scribd.com/doc/52238931/Notepad-Tutorial-Compile-and-Run-Java-Program

另一个有用的网站:http: //www.scribd.com/doc/52238931/Notepad-Tutorial-Compile-and-Run-Java-Program

回答by Jean-Francois T.

I personally use the following batch script that can be used on many types of files (C, makefile, Perl scripts, shell scripts, batch, ...).

我个人使用以下批处理脚本,该脚本可用于多种类型的文件(C、makefile、Perl 脚本、shell 脚本、批处理等)。

How to use it:

如何使用它:

  1. Install NppExecplugin
  2. Store this file in the Notepad++ user directory (%APPDATA%/Notepad++) under the name runNcompile.bat(but you can name it whatever you like).
  3. while checking the option "Follow $(CURRENT_DIRECTORY)" in NppExec menu
  4. Add a NppExeccommand "$(SYS.APPDATA)\Notepad++\runNcompile.bat" "$(FULL_CURRENT_PATH)"(optionally, you can put npp_saveon the first line to save the file before running it)
  5. Assign a special key (I reassigned F12) to launch the script.
  1. 安装NppExec插件
  2. 将此文件存储在 Notepad++ 用户目录 ( %APPDATA%/Notepad++) 下,名称为runNcompile.bat(但您可以随意命名)。
  3. 在 NppExec 菜单中选中“Follow $(CURRENT_DIRECTORY)”选项时
  4. 添加一个NppExec命令"$(SYS.APPDATA)\Notepad++\runNcompile.bat" "$(FULL_CURRENT_PATH)"(可选,您可以npp_save在运行之前放在第一行以保存文件)
  5. 分配一个特殊键(我重新分配了F12)来启动脚本。

This page explains quite clearly the global flow: https://www.thecrazyprogrammer.com/2015/08/configure-notepad-to-run-c-cpp-and-java-programs.html

此页面非常清楚地解释了全局流程:https: //www.thecrazyprogrammer.com/2015/08/configure-notepad-to-run-c-cpp-and-java-programs.html

Hope it can help

希望它可以帮助

  @echo off

REM ----------------------
REM ----- ARGUMENTS ------
REM ----------------------
set FPATH=%~1
set FILE=%~n1
set DIR=%~dp1
set EXTENSION=%~x1
REM ----------------------



REM ----------------------
REM ------- CONFIG -------
REM ----------------------
REM C Compiler (gcc.exe or cl.exe) + options + object extension
set CL_compilo=gcc.exe
set CFLAGS=-c "%FPATH%"
set OBJ_Ext=o
REM GNU make
set GNU_make=make.exe
REM ----------------------




IF /I "%FILE%"==Makefile GOTO _MAKEFILE
IF /I %EXTENSION%==.bat GOTO _BAT
IF /I %EXTENSION%==.sh GOTO _SH
IF /I %EXTENSION%==.pl GOTO _PL
IF /I %EXTENSION%==.tcl GOTO _TCL
IF /I %EXTENSION%==.c GOTO _C
IF /I %EXTENSION%==.mak GOTO _MAKEFILE
IF /I %EXTENSION%==.mk GOTO _MAKEFILE
IF /I %EXTENSION%==.html GOTO _HTML
echo Format of argument (%FPATH%) not supported!
GOTO END


REM Batch shell files (bat)
:_BAT
call "%FPATH%"
goto END


REM Linux shell scripts (sh)
:_SH
call sh.exe "%FPATH%"
goto END


REM Perl Script files (pl)
:_PL
call perl.exe "%FPATH%"
goto END


REM Tcl Script files (tcl)
:_TCL
call tclsh.exe "%FPATH%"
goto END


REM Compile C Source files (C)
:_C
REM MAKEFILES...
IF EXIST "%DIR%Makefile" ( cd "%DIR%" )
IF EXIST "%DIR%../Makefile" ( cd "%DIR%/.." )
IF EXIST "%DIR%../../Makefile" ( cd "%DIR%/../.." )
IF EXIST "Makefile" ( 
    call %GNU_make% all
    goto END
)
REM COMPIL...
echo -%CL_compilo% %CFLAGS%-
call %CL_compilo% %CFLAGS%
IF %ERRORLEVEL% EQU 0 (
    echo -%CL_compilo% -o"%DIR%%FILE%.exe" "%DIR%%FILE%.%OBJ_Ext%"-
    call %CL_compilo% -o"%DIR%%FILE%.exe" "%DIR%%FILE%.%OBJ_Ext%" 
)
IF %ERRORLEVEL% EQU 0 (del "%DIR%%FILE%.%OBJ_Ext%")
goto END


REM Open HTML files in web browser (html and htm)
:_HTML
start /max /wait %FPATH%
goto END

REM ... END ...
:END
echo.
IF /I "%2" == "-pause" pause

回答by JerryGoyal

I've made a single powerfull script that will:

我制作了一个强大的脚本,它将:

-Compile and run multi language code like C, C++, Java, Pythonand C#.
-Delete the old executable before compiling code.
-Only run the code if it's compiled successfully.

-compile和运行多语言代码一样CC++JavaPythonC#
- 在编译代码之前删除旧的可执行文件。
- 仅在编译成功后运行代码。

I've also made a very noob friendly tutorial Transform Notepad++ to Powerful Multi Languages IDEwhich contains some additional scripts like to only run or Compile the code, run code inside CMD etc.

我还制作了一个非常适合新手的教程将 Notepad++ 转换为强大的多语言 IDE,其中包含一些额外的脚本,例如仅运行或编译代码、在 CMD 中运行代码等。

npp_console 1               //open console
NPP_CONSOLE -               //disable output of commands
npe_console m-              //disable unnecessary output
con_colour bg= 191919 fg= F5F5F5    //set console colors
npp_save                //save the file
cd $(CURRENT_DIRECTORY)         //follow current directory
NPP_CONSOLE +               //enable output
IF $(EXT_PART)==.c GOTO C       //if .c file goto C label
IF $(EXT_PART)==.cpp GOTO CPP       //if .cpp file goto CPP label
IF $(EXT_PART)==.java GOTO JAVA     //if .java file goto JAVA label
IF $(EXT_PART)==.cs GOTO C#     //if .cs file goto C# label
IF $(EXT_PART)==.py GOTO PYTHON     //if .py file goto PYTHON label
echo FILE SAVED
GOTO EXITSCRIPT             // else treat it as a text file and goto EXITSCRIPT
//C label
:C                                                                  
cmd /C if exist "$(NAME_PART).exe" cmd /c del "$(NAME_PART).exe"//delete existing executable file if exists
gcc "$(FILE_NAME)" -o $(NAME_PART)              //compile file
IF $(EXITCODE) != 0 GOTO EXITSCRIPT             //if any compilation error then abort
echo C CODE COMPILED SUCCESSFULLY:              //print message on console
$(NAME_PART)                                            //run file in cmd, set color to green and pause cmd after output
GOTO EXITSCRIPT                         //finally exits

:CPP
cmd /C if exist "$(NAME_PART).exe" cmd /c del "$(NAME_PART).exe"
g++ "$(FILE_NAME)" -o $(NAME_PART)
IF $(EXITCODE) != 0 GOTO EXITSCRIPT
echo C++ CODE COMPILED SUCCESSFULLY:
$(NAME_PART)
GOTO EXITSCRIPT

:JAVA
cmd /C if exist "$(NAME_PART).class" cmd /c del "$(NAME_PART).class"
javac $(FILE_NAME) -Xlint
IF $(EXITCODE) != 0 GOTO EXITSCRIPT
echo JAVA CODE COMPILED SUCCESSFULLY:
java $(NAME_PART)
GOTO EXITSCRIPT

:C#
cmd /C if exist "$(NAME_PART).exe" cmd /c del "$(NAME_PART).exe"
csc $(FILE_NAME)
IF $(EXITCODE) != 0 GOTO EXITSCRIPT
echo C# CODE COMPILED SUCCESSFULLY:
$(NAME_PART)
GOTO EXITSCRIPT

:PYTHON
echo RUNNING PYTHON SCRIPT IN CMD:              //python is a script so no need to compile
python $(NAME_PART).py
GOTO EXITSCRIPT

:EXITSCRIPT
// that's all, folks!

回答by Yuukio

You can actually compile and run C code even without the use of nppexec plugins. If you use MingW32 C compiler, use g++ for C++ language and gcc for C language.

即使不使用 nppexec 插件,您实际上也可以编译和运行 C 代码。如果使用 MingW32 C 编译器,C++ 语言使用 g++,C 语言使用 gcc。

Paste this code into the notepad++ run section

将此代码粘贴到记事本++运行部分

cmd /k cd $(CURRENT_DIRECTORY) && gcc $(FILE_NAME) -o $(NAME_PART).exe  && $(NAME_PART).exe && pause

It will compile your C code into exe and run it immediately. It's like a build and run feature in CodeBlock. All these are done with some cmd knowledge.

它会将您的 C 代码编译为 exe 并立即运行。这就像 CodeBlock 中的构建和运行功能。所有这些都是通过一些cmd知识完成的。

Explanation:

解释:

  1. cmd /k is used for testing.
  2. cd $(CURRENT_DIRECTORY)
    • change directory to where file is located
  3. && operators
    • to chain your commands in a single line
  4. gcc $(FILE_NAME)
    • use GCC to compile File with its file extension.
  5. -o $(NAME_PART).exe
    • this flag allow you to choose your output filename. $(NAME_PART) does not include file extension.
  6. $(NAME_PART).exe
    • this alone runs your program
  7. pause
    • this command is used to keep your console open after file has been executed.
  1. cmd /k 用于测试。
  2. cd $(CURRENT_DIRECTORY)
    • 将目录更改为文件所在的位置
  3. && 运算符
    • 将您的命令链接在一行中
  4. gcc $(FILE_NAME)
    • 使用 GCC 编译文件及其文件扩展名。
  5. -o $(NAME_PART).exe
    • 此标志允许您选择输出文件名。$(NAME_PART) 不包括文件扩展名。
  6. $(NAME_PART).exe
    • 仅此一项即可运行您的程序
  7. 暂停
    • 此命令用于在文件执行后保持控制台打开。

For more info on notepad++ commands, go to

有关记事本++命令的更多信息,请访问

http://docs.notepad-plus-plus.org/index.php/External_Programs

http://docs.notepad-plus-plus.org/index.php/External_Programs

回答by dom free

I recommend my solution. My situation: g++(cygwin) on win10

我推荐我的解决方案。我的情况:win10 上的 g++(cygwin)

My solution: Write a .bat batch file and execute compiler in that batch. compileCpp.bat

我的解决方案:编写一个 .bat 批处理文件并在该批处理中执行编译器。编译Cpp.bat

@echo off  
set PATH=%PATH%;C:\cygwin64\bin\
rm %~n1.exe
c++.exe -g %~dpnx1 -o %~dpn1.exe
%~n1.exe  

Console:

安慰:

NPP_EXEC: "runCpp"
NPP_SAVE: E:\hw.cpp
CD: E:\
Current directory: E:\
cmd /c C:\cygwin64\bin\compileCpp.bat "hw.cpp"
Process started >>>
Hello World<<< Process finished. (Exit code 0)
================ READY ================

回答by Antonio Runcio

In windows if you are using a portable version of MinGW you must set path variable or you have error libintl-8.dll not found. My path is C:\Program Files (x86)\CodeBlocks\MinGW\bin

在 Windows 中,如果您使用的是 MinGW 的便携式版本,则必须设置路径变量,否则会出现错误 libintl-8.dll not found。我的路径是 C:\Program Files (x86)\CodeBlocks\MinGW\bin

回答by Fandehalodu09

Decompile with CMD:

用 CMD 反编译:

If those didn't work try this:

如果这些不起作用,请尝试以下操作:

cmd /K g++ "$(FULL_CURRENT_PATH)" -o "$(FULL_CURRENT_PATH).exe

It should save where you got the file (Example: If I got file from Desktop, it will be saved as .exe on the Desktop)

它应该保存您获取文件的位置(例如:如果我从桌面获取文件,它将在桌面上保存为 .exe)

I don't know if it works on 64 bits though so you can try it!

我不知道它是否适用于 64 位,所以您可以尝试一下!

回答by Clevemayer

I've written just this to execute compiling and run the file after, plus fileinputname = fileoutputname on windowsmashines, if your compilerpath is registred in the windows PATH-var:

如果您的编译器路径已在 Windows PATH-var 中注册,我只写了这个来执行编译并在之后运行文件,在 windowsmashine 上加上 fileinputname = fileoutputname:

NPP_SAVE
cd "$(CURRENT_DIRECTORY)"
set LEN~ strrfind $(FILE_NAME) .
set EXENAME ~ substr 0 $(LEN) $(FILE_NAME)
set $(EXENAME) = $(EXENAME).exe
c++.exe "$(FILE_NAME)" -o "$(EXENAME)"
"$(EXENAME)"

should work for any compiler if you change c++.exe to what you want

如果您将 c++.exe 更改为您想要的,则应该适用于任何编译器