Using C++ with NetBeans

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

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

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.

Bean01
Figure 1: The Installer opening screen

In License Agreement, select the checkbox and click Next. Select an Installation folder and click Next.

Bean02
Figure 2: The License Agreement screen

In Summary, click Install.

Bean03
Figure 3: The Summary screen

First, the installation data is prepared, including extracting the installation data for the Base IDE. Next, the installation starts.

Bean04
Figure 4: Starting the installation

Click Finish when the installation completes.

Bean05
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.

Bean06
Figure 6: Choosing not to import a previous version of NetBeans

The NetBeans IDE gets started.

Bean07
Figure 7: Starting the NetBeans IDE

Creating a C++ Application

To create a new C++ application, select File>New Project.

Bean08
Figure 8: Starting a new project

In New Project, select C/C++ in Categories and C/C++ Application in Projects. Click Next.

Bean09
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.

Bean10
Figure 10: Clicking Finish

A new C++ application gets created, including a C++ code file titled main.cpp.

Bean11
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.

Bean12
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.

Bean13
Figure 13: Setting compiler preferences

Setting C++ Tooling Options

To set the tooling option, select Tools>Options.

Bean14
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.

Bean15
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.

Bean16
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.

Bean17
Figure 17: Building the project

If the applications gets compiled without any error, a BUILD SUCCESSFUL message should get displayed.

Bean18
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.

Bean19
Figure 19: Testing the build

If the build tests fail, a BUILD TESTS FAILED message gets displayed.

Bean20
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.

Bean21
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."

Bean22
Figure 22: Seeing the NetBeans 8.2 bug

To avoid the runtime error, set Console Type to External Terminal.

Bean23
Figure 23: Setting the Console Type to External Terminal

Subsequently, click Apply and OK.

Bean24
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.

Bean25
Figure 25: Running the Project

If the application runs without any error, a RUN SUCCESSFUL message gets displayed.

Bean26
Figure 26: A successful run

Because we selected External Terminal for Console Type, the application output is generated in an external terminal.

Bean27
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.

Bean28
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.

Bean29
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.

Bean30
Figure 30: The application running in an external terminal

In this tutorial, we introduced using C++ in NetBeans IDE.



About the Author

Deepak Vohra

Deepak Vohra is an O'Reilly technical reviewer, who reviewed the book WebLogic: The Definitive Guide. He's also a NuBean consultant and web developer, and is a Sun Certified Java 1.4 Programmer and Sun Certified Web Component Developer. Deepak has published three books on Docker.

Related Articles

Comments

  • c++

    Posted by Sridharan on 08/18/2017 09:00pm

    Very Good. Thank you..

    Reply
  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date