After an exhausting search for a good IDE for C++, I finally stopped at Netbeans. I appreciate its convenient zoom-in/out feature, auto-completion, user-friendly interface, etc.
The first challenge I met was to set up the project to work with CMake, as I decided to think beyond the GNU make. I use a MacBook, therefore many programmer tools have to be “manually” installed, including CMake. In this journal, I’ll demonstrate how to set up a project in Netbeans to work with CMake on Mac OS X.
You need the following softwares to work with:
After macports is installed, install CMake and tree on the command line in a Terminal:
sudo port install cmake tree
macports will find the pre-requisites for cmake and complete the installation. You can now test CMake by:
This will give you the version number of your CMake. Test tree program by just typing tree in the prompt and it will give you a hierarchy of the current folder. Now you are ready to go.
Working with a small C++ project
Here we use a simple example of a C++ program that uses its own header for complex numbers (not even close to complete but enough for demonstration), called HelloComplex
Supposedly you have written the header and source files and want to start a Netbeans project. The file hierarchy starts with the following:
The contents of each file shouldn’t be of great importance. However, the bash file cmake_make.sh in this example is greatly useful for setting up Netbeans and also for even just CMake:
(P.S. you probably want to add execution permission to the bash file first, by
chmod +x ./cmake_make.sh
What it does is to limit the build from CMake in one directory in ./build/, so the Makefile and other CMake “junk files” will stay in ./build/ . When one wants to clean the build, just do
The shell script will first look for an existing ./build/Makefile and do the designated make clean process. Thereafter, the ./build/ directory is completely removed, and an empty ./build/ is created for the new build.
Letting NetBeans know where to find cmake
Before the project is created, go to NetBeans->Preferences->C/C++->Build Tools. You should be able to see GNU there, like the following window:
Click on $PATH, which tells NetBeans where to look for binaries. Note that this $PATH is not necessarily the same as that as a system environment variable. Add the directory that contains the cmake command. As an example, on my computer cmake is in /opt/local/bin/, so I added it:
Start a NetBeans project with the above file hierarchy
So, NetBeans can find cmake from your system, so the cmake_make.sh can be executed. Now go to File->New Project… The window below will pop out,
Click on Next, then choose the root folder, such as below. Choose the modified GNU Mac. Choose Custom Configuration mode.
Check the box on Pre-build step is required, and choose the root folder to run in. Use the custom command in shell to do the build. In our case, it’s just ./cmake_make.sh Be sure to change the accessibility to make it executable.
Hit Next, and then use the root directory as the working directory. Change the Clean Command and Build Command to ./cmake_make.sh clean and ./cmake_make.sh . Check the box for clean and build after finish.
Click on Next again, choose the source file location, which is the root directory in our case.
Click on Next, and choose Automatic Configuration (No figure for this step). The final step will be automatically completed by the software. Click on Finish, then the shell script will be executed automatically, as it was specified as the command to build the program. The following workstation should now be available, and the first pre-build should be done also.
You may run the executable in the ./bin/ directory in NetBeans, the output will be shown in the window.
Summary and reminders
This blog only describes how to start the NetBeans project from existing files, but does not include the details of CMake and C++ codes being used. If you are interested, please read my other blog on CMake. You should be able to click on Run from now on. Work on the source files including headers, re-build, and run!
Comments or questions, if any
Please leave them in the comments below. I’ll try to improve my blogs. Thanks!