NetBeans for C in ECE 209 & CSCI 255

Getting a C compiler

Before you can use C with NetBeans you must have a C compiler. We are going to specificly recommend that you use gcc. Do not go to the next section until you have gcc on your computer.

C for Windows

If you have Windows, I suggest you download and install enough of Cygwin to allow you run gcc. Cygwin provides a way of running many Linux applications under Windows, and the biggest problem with the install is figuring out just how many of those applications, or packages in Cygwin parlance, you need to install.

Start your install by going to the Cygwin site and pressing the icon to download the Cygwin setup utility. The Cygwin setup program is pretty small, only about half a megabyte. That's because it doesn't include any Cygwin packages. You select those during the install.

Go ahead and start the install. Select the defaults for a while. Soon you'll be asked to choose a download site. I chose Georgia Tech because it's pretty close to North Carolina.

Then you have to select the packages. Go to the Devel category and expand it by clicking on the plus sign (+) on the right.
Package selection

Now you can choose from the many packages listed on the right side of the setup window. You will find many discussions on the internet about what packages are needed to run C under NetBeans. I choose the following and they seem to be working for me.

The interface for choosing packages is a little odd You click on the words Skip (or on that little circle with two arrowheads next to Skip) to indicate that you wish to select a package.
Selecting gcc
You will often find that selecting one package will cause other dependent packages to be automatically included.

You may wish to include more than the packages listed above. You should consider selecting something from the Editors category so at you can edit programs from the Cygwin shell, which is the "command prompt" of Cygwin. The easist editor is nano. If you've been using emacs or vim for decades, they are also available.

Many download Cygwin for its X server. If this interests you, consult the Cygwin/X guide. Students enrolled at NCSU should consult the Virtual Computing Lab site for other free X servers.

Once you've confirmed your packages selections, the Cygwin installation program will start to download and install them. This could take a while.

Cygwin is installed in a directory with lots of files and deep subdirectories. NetBeans will store your work outside of this environment and will organize your files and compile and run your programs. Do not expect to execute your C programs from the Windows Command Prompt.

Another prerequiste for NetBeans

Because NetBeans is written in Java, you must have the Java Runtime Environment installed on your computer. Most likely you already do. If you are unsure about your Java installation, connect to Sun's Java download site which will test your computer's Java environment and provide you with an opportunity to download Java if it is needed.

If you believe you'll want to also write Java programs in the future, you should just connect to the Java SE Download page and download the latest Java SE Development Kit.

Installing NetBeans for C

If NetBeans is already installed

If you have previous installed NetBeans in order to write Java programs, you need to download and install the C plugin. Do this by going through NetBeans' Tools ⇒ Plugins menu choices and then choosing the Available Plugins tab.
Plugins window
Now select and install the C/C++ plugin.

If NetBeans is not installed

NetBeans can be downloaded from the NetBeans community web site. When you reach the NetBeans IDE download site, you'll be given a choice between various download "bundles". The C/C++ bundle is actually rather small. If you believe you want to write Java programs along with C, you'd probably be better off downloading the C/C++ bundle first and later installing the Java plugin.

First run of NetBeans

After staring NetBeans make sure that NetBeans knows where to find gcc. Go through the Tools ⇒ Options menu choices and then select the C/C++ tab. If you are viewing the Build Tools panel, it should look something like the screen capture below:
Options window

If the Build Tools panel has lots of blank fields or if NetBeans won't even display a Build Tools panel, it is likely that your PATH isn't set to include the Cygwin base directory (which is usually C:\cygwin\bin on a Windows computer). The details of setting the PATH vary among operating systems, so I suggest you Google for a solution to that problem.

It is quite likely that your instructor will want you to use a particular set of options when compiling your program to make sure your code conforms to pushced C standards. For example, you might be asked to use the gcc options -ansi, -pedantic, and -Wall for conformance with standard ANSI C.

To set compiler options, press the Project Options tab. The C compilation options are set by changing the CFLAGS variable for the Make, the program that controls the compilation and linking process. Do this typing the following line in the box to the right of Make Options:
     CFLAGS="-ansi -pedantic -Wall"
It is very important there there be no spaces after the hyphens!
Options window

Creating your first project

To build your first C program under NetBeans, start with the File ⇒ New Project menu choices. In the New Project window choose C/C++ under Categories and C/C++ application under Projects.
New Projects window

This will bring up the New C/C++ Application window. Here you can set the name of your project and its location. If you don't set the location, be sure to note project folder. You'll need this if your instructor asked you to submit a copy of your program. One thing you must do if to go to the little pull-down menu in the lower right corner and make sure C rather than C++. Expect to find C++ as the default when you start your first project. Your choice at this stage will become the default for your later projects.
New C/C++ Application window

Go ahead and press Finsh to start writing your program.

Creating your first program

Now you'll have a much more compliated display. Look in the Projects panel at the upper left corner. Press the + expander next to Source. You should now see an entry with the name of the containing your main program.
Project panel
Double click on this entry.

On the upper right, you should see a tab with the name of your program. Go ahead and click that tab and start editing your code to contain the classic "Hello World" program.
main.c panel
All you have to do is add one line.

Building your first program

Notice the row of icons above your program.
tool icons
Slowly move, without pressing, your mouse over this icons to see what they do.

Push the hammer icon. It compiles your program and builds your application. If you typed in your program correctly, you will be told that your build was successful. This message will appear at the bottom right in the Output panel.
Output panel

If the build isn't successful, fix your code. The output panel will tell the location of the lines in need of repair.

Runing your first program

Now press the play button to run your application. This will bring up a new window in which you interact with your program. (Oddly enough, the output of your program is not displayed in the Output panel.) This new window will be displayed until you press the enter key.
Hello World run

What can go wrong

If you have a syntatic error on your code, NetBeans will display a little red circle containing an exclamation point to the right of the line where the error was discovered. If you place your mouse on top of this circle, you'll be told what the error is.

While these error messages are useful to experienced programmers, they can be very confusing to beginers. For example, if you delete the semi-colon at the end of the statement containing the printf, you be told that return is an "unexpected token" rather than that a semi-colon was the expected token.

When you first start programming in C, be very careful. If you get a whole string of red circles, try to fix the first one. If you don't see anything wrong in the line with the red circle, look at the preceding line.

Mismatched and missing parentheses and braces can result in screenfuls of red circles. NetBeans tries very hard to keep you parentheses and braces in good order. If you type a left parenthesis, NetBeans will automatically add a right parenthesis for you. If you type a left brace and follow it with a new line (as you always should), NetBeans automatically inserts an appropriately intended right brace and puts the cursor on an appropriate intendation level for the statement block between the braces.

Also, if you place your cursor on a brace or parenthesis, NetBeans will highlight both it and its partner in yellow. Don't be one of those people who ignore NetBeans attempt to indent your code and balance your braces and parentheses!

Finally, if you always called store your main program in main.c, you may find many tabs with that label cluttering your NetBeans window. The easiest way to prevent this problem is to also close your old NetBeans' projects before starting a new one.

For more information

This web page is just a start. The best it can do if get you through one program. Before you get too involved read the much more complete C/C++ Projects Quick Start Tutorial by Ann Rice. (No, not the one who wrote the vampire books. She's Anne Rice.)