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

前端 未结 10 1392
北荒
北荒 2020-11-30 00:45

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

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

相关标签:
10条回答
  • 2020-11-30 00:56

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

    My solution: Write a .bat batch file and execute compiler in that batch. compileCpp.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 ================
    
    0 讨论(0)
  • 2020-11-30 00:58

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

    How to use it:

    1. Install NppExec plugin
    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 NppExec command "$(SYS.APPDATA)\Notepad++\runNcompile.bat" "$(FULL_CURRENT_PATH)" (optionally, you can put npp_save on the first line to save the file before running it)
    5. Assign a special key (I reassigned F12) to launch the script.

    This page explains quite clearly the global flow: 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
    
    0 讨论(0)
  • 2020-11-30 01:07

    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:

    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

    0 讨论(0)
  • 2020-11-30 01:08

    For perl,

    To run perl script use this procedure

    Requirement: You need to setup classpath variable.

    Go to plugins->NppExec->Execute

    In command section, type this

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

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

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

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

    Note: Not required seperate config for seperate files.

    For java,

    Requirement: You need to setup JAVA_HOME and classpath variable.

    Go to plugins->NppExec->Execute

    In command section, type this

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

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

    For Python,

    Use this Plugin Python Plugin

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

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

    For PHP,

    No need for different configuration just download this plugin.

    PHP Plugin and done.

    For C language,

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

    Go to plugins->NppExec->Execute

    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.

    Go to plugins->NppExec->Advanced options.

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

    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 F6 and select script or just press shortcut(I assigned Ctrl+2).

    For C++,

    Only change g++ instead of gcc and *.cpp instead on *.c

    That's it!!

    0 讨论(0)
  • 2020-11-30 01:08

    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

    0 讨论(0)
  • 2020-11-30 01:09

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

    • Open Notepad++
    • Type F6 to 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/Modify and 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.
    0 讨论(0)
提交回复
热议问题