Mar 05 2011

Writing NXC code in Notepad++

Category: Lego,Mindstorms,NXC,SoftwareSpiller @ 19:21

When writing programs in NXC I prefer using Notepad++ instead of BricxCC as the text editor. Notepad++ isn’t a IDE though so you can’t compile it directly in the program. However Notepad++ comes with a plug-in called NppExec that can execute other programs and we can use this to start the command line compiler (nbc.exe) directly in Notepad++.

Compiling files with NppExec

NppExec is a rather powerful plug-in which can do a lot for you. To start it up, simply press F6 (or find it in the menu, Plugins -> NppExec -> Execute…).

The NppExec main window

The big multi line textedit field will contain all the commands you want to execute. Each line is a separate command which will be executed after each other, starting from the top. To get started, we need to find nbc.exe’s location. It is in the same folder as BrixcCC which is normally:

"C:\Program Files\BricxCC\nbc.exe"

(The quote marks are necessary if  there are spaces in the path, they are NOT optional!)

First of all, as this is a command line compiler (because that is the reason we are using it), we need to know which parameters it takes. To do this, enter the following command in NppExec:

"C:\Program Files\BricxCC\nbc.exe" -help

(To get a command line program’s supported parameters, you often write “-help”, “-h” or “/?”. (To get a list for NppExec, simply write “help”.)) The result should look something like this:

The NppExec command line window

Notice the syntax: nbc.exe options filename options

So after nbc.exe you can place parameters (options), then the filename and after that more parameters. (I prefer having all the parameters together though.) The most important parameters are:
-d, which will compile and transfer (download) the program to the NXT.
-r, the same as -d, but will also start (run) the program when done.
-Z[1|2], this will turn compiler optimizations on, -Z2 is recommended.
-EF, if you are using Enhanced Firmware, remember to add this one.
-v=n, this specifies the version of your firmware. This apparently defaults to NXT 2.0 firmware now, so you most likely doesn’t need to specify it. (Otherwise write -v=128)

(I haven’t tried downloading over Bluetooth so don’t know how this is done. You might need to use the -S=portname parameter…)

So in order to compile your NXC file and download it, you write this in NppExec:

"C:\Program Files\BricxCC\nbc.exe" path_to_your_file -d

where “path_to_your_file” is the file path to the NXC file you want to compile. If you want to use Enhanced Firmware and compiler optimizations you can do it like this:

 "C:\Program Files\BricxCC\nbc.exe" path_to_your_file -d -EF -Z2

Everything we have done here is possible to do in the command prompt, if you try to run the commands in the command prompt it will work as you expect. However there isn’t much point of doing it in NppExec if you just use it as if it was a command prompt…

Taking advantage of NppExec

The first thing we want to avoid is to having to change “path_to_your_file” every time you need to compile another file. NppExec contains some macros that can help us with this which take the form: $(NAME_OF_MACRO) where “NAME_OF_MACRO” is the macro you want to use. The one we want to use is FULL_CURRENT_PATH which will be replaced by the file path of the document you are currently viewing. We simply needs to use this instead of “”:

"C:\Program Files\BricxCC\nbc.exe" "$(FULL_CURRENT_PATH)" -d

Notice that there are quotes around the macro. These aren’t necessarily needed, however as said before, if there are spaces in the file path you need to have them. Add quotes every time you automatically inserts something that can contain spaces, better safe than worry.

Saving files

One thing you need to be aware of is that it uses the file saved on the hard disk, so if you have made changes to the document and have not saved it yet, those changes will not be known by the compiler. So you will either have to remember to save or use the NppExec “NPP_SAVE” command which automatically saves the current document. You place this command on its separate line before your call nbc.exe:

"C:\Program Files\BricxCC\nbc.exe" "$(FULL_CURRENT_PATH)" -d

Improving NXC integration

Turning off compiler status messages

nbc.exe writes quite a bit of status messages in the console window which I aren’t really interested in normally, however you can turn those of easily by adding the parameter “-sm-“:

"C:\Program Files\BricxCC\nbc.exe" "$(FULL_CURRENT_PATH)" -d -sm-

Reducing the shown amount of compile errors

The command window autoscrolls so if you get 50 errors you will have to scroll all the way up again. You can set a limit on how many you want to be displayed with the -ER=n parameter (where n is the maximal amount).

"C:\Program Files\BricxCC\nbc.exe" "$(FULL_CURRENT_PATH)" -d -sm- -ER=5

(It seems it that nbc.exe didn’t output the error messages to the console in some of the older versions, but you can store the error messages to a file with the -E=filename parameter and then display this file in the console.)

Making the compile errors easily readable and add goto line

Right now the compile errors is a bit hard to read, however you can add coloring rules to make it more clear. Go to “Plugins -> NppExec -> Console Output Filters…” and a dialog window with three tabs appear which you can filter some of the lines out or only allow certain ones. You can replace text and the last tab, which we are going to use, highlight lines.

NppExec highlight dialog window

Take a closer look at the image to see the changed I made. The column of checkboxes turns the highlight marks on and off, the second column is the rule, the three next ones is the RGB color in hex (which nearly impossible to change, just use “0” and “ff”). The last three colomns with check boxes add Italics, Bold and Underline text decoration.

I will not go in dept with how to make rules though, look in the documentation or just read the text at the bottom if you want to learn how  to make your own. I will however go through the first line:

File "%ABSFILE%" ; line %LINE%

The %ABSFILE% part tells that this is an absolute file path and %LINE% tells that it is a line number. The cool thing is that if you double click on that line, it will automatically go the specified line in that document. Jumping to a specific line specified in the console

Make Notepad++ automatically add syntax highlighting

If you open a .nxc file in Notepad++ it will treat it as a text file. To make it recognize the file extension, go into “Settings -> Style Configurator” and select the language you want to use, most likely C or C++. Then in the “User ext. :” box, add “nxc”. (If you want multiple extension, separate each with a space.) Style Configurator dialog window

If you want to use a User Defined language instead, go into “View -> User-Defined dialogue…” and add the extension in the “Ext. :” box.

Function auto-complete and highlighting?

I found a User Defined language once that did this, but its C support wasn’t too good as User Defined languages are rather limited in that aspect. (Which is why I’m just using the standard C syntax highlighting.) Autocomplete worked great though.

I don’t have the link anymore and I think it was posted on… (And I will not redistribute it without permission from the author.) Does anyone know of a good alternative?

Other stuff…

Saving the compiled .rxe file

It can be done with the -O=filename parameter:


Notice the two new macros and how it is used to create a new file path.

Windows intergration

I have recently studied how to implement your own filetype in the Windows shell, so I’m going to try it out and write it in another post. Adding stuff like being able to compile .nxc files just by right clicking on them in Explorer and such.

It is going to involve manual editing in the Registry using regedit though.