NetBeans IDE was recently donated by Oracle to Apache and continues to be one of the most commonly used Java IDEs. NetBeans is more than just a Java IDE; it also provides a C/C++ edition. In this tutorial, we shall discuss using C/C++ in NetBeans. We shall discuss creating a C++ application, compiling the application, and subsequently running the application. The tutorial has the following sections:
- Setting the Environment
- Creating a C++ Application
- Setting Compiler Preferences
- Setting C++ Tooling Options
- Compiling the Application
- Testing the Application
- Setting Console Type
- Running the Application
- Providing Application Command Line Arguments
Setting the Environment
NetBeans IDE download bundles are available with support for various languages such as Java, PHP, HTML5/JavaScript, and C/C++. We need to download the C/C++ edition. Both x86 and x64 platform versions are available. Double-click the exe file netbeans-8.2-cpp-windows-x64.exe to start the installation. The NetBeans IDE Installer gets started. Click Next.
Figure 1: The Installer opening screen
In License Agreement, select the checkbox and click Next. Select an Installation folder and click Next.
Figure 2: The License Agreement screen
In Summary, click Install.
Figure 3: The Summary screen
First, the installation data is prepared, including extracting the installation data for the Base IDE. Next, the installation starts.
Figure 4: Starting the installation
Click Finish when the installation completes.
Figure 5: Completing the installation
An icon gets added to launch NetBeans IDE. If a previous version of NetBeans was uninstalled, when the NetBeans IDE is launched for the first time a dialog is displayed prompting if settings from a previous version are to be imported. Click No, regardless of the previous version/edition of NetBeans.
Figure 6: Choosing not to import a previous version of NetBeans
The NetBeans IDE gets started.
Figure 7: Starting the NetBeans IDE
Creating a C++ Application
To create a new C++ application, select File>New Project.
Figure 8: Starting a new project
In New Project, select C/C++ in Categories and C/C++ Application in Projects. Click Next.
Figure 9: Selecting a new application
Specify Project Name and Location. Other than adding a project name (HelloWorld, for example), the other settings may be kept as the default. Click Finish.
Figure 10: Clicking Finish
A new C++ application gets created, including a C++ code file titled main.cpp.
Figure 11: A new application begins
To create a HelloWorld application, copy the following listing to the main.cpp file.
#include <iostream> using namespace std; int main(int argc, char** argv) { cout << "!!!Hello world!!!" << endl; return 0; }
And, click File>Save.
Figure 12: Saving the file
Setting Compiler Preferences
The default C++ Compiler preferences should suffice for most applications, but if the compiler preferences need to be modified, right-click the project and select Project Properties. Select Build>C++ Compiler in Categories. The C++ compiler preferences include the Development Mode (default is Debug), the C++ Standard, and the compiler Tool (g++ by default). Click Apply and OK if any of the compiler preferences are modified.
Figure 13: Setting compiler preferences
Setting C++ Tooling Options
To set the tooling option, select Tools>Options.
Figure 14: Setting the tooling option
The Build Tools tab lists the GNU MinGW tool collection, including the Base Directory, the g++.exe C++ Compiler, and the Make and Debugger commands. Keep the default Build Tools settings.
Figure 15: Keeping the default settings
The Project Options tab provides settings for the project, such as the File Path Mode (Always Relative is the default), whether dependency checking in generated makefiles is to be enabled, and whether warning dialogs about missing makefiles are to be displayed. The option to rebuild the entire project if project properties are modified is also provided.
Figure 16: Viewing the Project Options tab
The Code Assistance tab lists the directories to include. The Debugging Options tab lists the debug options, such as whether to finish the debug session when a program exits. The Other tab lists the File Type associations with the File Extensions. The default file extensions for C++ files are C, c++, cc, cpp, cxx, and mm. The default standard for C++ File is C++11.
Compiling the Application
A C++ application automatically gets built when it is run, but an application also may be built before running the application. Select Run>Build Project to build (compile) the HelloWorld application.
Figure 17: Building the project
If the applications gets compiled without any error, a BUILD SUCCESSFUL message should get displayed.
Figure 18: A successful build
Testing the Application
Testing an application is also optional, but it is a best practice to test an application before running it because an application could still generate a runtime error after being compiled successfully. Click Run>Test Application to test the HelloWorld application.
Figure 19: Testing the build
If the build tests fail, a BUILD TESTS FAILED message gets displayed.
Figure 20: A failed build
The application may be debugged to find the reason of a build test failing.
Setting Console Type
The default setting for the Console type is Internal Terminal.
Figure 21: Checking the default Console Type
Because of a bug in NetBeans 8.2, the default Console Type could generate a runtime error, such as “Unable to start pty process.”
Figure 22: Seeing the NetBeans 8.2 bug
To avoid the runtime error, set Console Type to External Terminal.
Figure 23: Setting the Console Type to External Terminal
Subsequently, click Apply and OK.
Figure 24: Clicking Apply
Running the Application
If an application has been found to build and test without any issues, the application may be run by selecting Run>Run Project.
Figure 25: Running the Project
If the application runs without any error, a RUN SUCCESSFUL message gets displayed.
Figure 26: A successful run
Because we selected External Terminal for Console Type, the application output is generated in an external terminal.
Figure 27: The application is generated in an external terminal
Providing Application Command Line Arguments
The HelloWorld application we built, tested, and ran does not make use of any runtime command line arguments. In this section, we shall modify the main.cpp file to add some application command line args. Modify the application as follows to include command line arguments argv[1] and argv[2].
#include <iostream> using namespace std; int main(int argc, char** argv) { cout << "Hello world" << endl; cout << "Hello " << argv[1] << endl; cout << "Hello also to " << argv[2] << endl; return 0; }
Click File>Save to save the modified main.cpp file.
Figure 28: Saving the modified file
Next, we need to supply a value for the command line args. Right-click the project and select Project Properties. In the Project Properties, select Run in Categories. In the Run Command field, the default setting is "${OUTPUT_PATH}". For argv[1] and argv[2], add two command line arg values, for example “C++” and “C”. Click Apply and subsequently click OK.
Figure 29: Clicking Apply
Select Run>Build Project and Run>Run Project to compile and run the modified application. The application output is displayed in an external terminal. Three “Hello” messages are output, two of which are for the command line args.
Figure 30: The application running in an external terminal
In this tutorial, we introduced using C++ in NetBeans IDE.