Friday, July 18, 2014

NetBeans: Configuring NetBeans IDE for C/C++

In this article, we will demonstrate how to install NetBeans IDE 8.0 and configure it for C/C++.

Installation steps:
  1. NetBeans IDE 8.0
  2. C/C++ plugin module
  3. Compiler and Tools

     

    NetBeans IDE


    From [1], you can choose which bundle (based on supported technologies) to download.  The choices include:
    •  Java SE
    •  Java EE
    •  C/C++
    •  HTML5 & PHP
    •  All
    If you have downloaded either "C/C++" or "All" bundle, C/C++ plugin module (see next section) is already included.

    C/C++ plugin module


    The NetBeans IDE is a dynamic modular IDE, which means you can change it by adding and removing modules of the program. You can add functionality by installing plugin modules, or remove functionality by uninstalling plugin modules.

    If your NetBeans IDE does not show a C/C++ project category when you select File > New Project, complete the following steps to add the C/C++ plugin module to the IDE.[2]
    1. If your network uses a proxy, choose Tools > Options > General in the IDE, select Manual Proxy Settings, enter the HTTP Proxy and Port for your proxy, and click OK.
    2. Choose Tools > Plugins.
    3. In the Plugins dialog box, click the Available Plugins tab, and scroll to the C/C++ category.
    4. Select the C/C++ checkbox and click Install to start the NetBeans IDE Installer.
    5. In the NetBeans IDE Installer, click Next.
    6. Read the license agreement, select the checkbox to accept the terms of the license agreement, and click Next.
    7. Click Install.
    8. After the installation completes, select either Restart IDE Now or Restart IDE Later and click Finish.

    Compiler and Tools


    The Netbeans C/C++ module requires a C compiler, C++ compiler, make utility, and gdb debugger. See [3] on the instructions for the platform of your development system.  Here we will use Micorsoft Windows as our platform and use the tools provided in cygwin for the demonstration.

    The NetBeans C/C++ module has been tested with the following compilers and tools from Cygwin.com.  However, if your version is newer, it seems to work fine too.


    Software or ResourceVersion TestedDescription
    cygwin1.dll1.7.7, 1.7.9 Cygwin Linux-like environment for Windows
    gcc4.3.4Cygwin C compiler
    g++4.3.4Cygwin C++ compiler
    gdb6.8Cygwin GNU debugger
    make3.81Cygwin make utility


    To download the compiler and tools you needed for C/C++ plugin, you can go to [4] and select one of the mirror sites.



    Note that you could select the appropriate package to download by specifying your compiler name or tool name.  If you select all packages to download, it will take hours.

    In the figure, we have specified g++ in the Search field.  Cygwin Setup was able to identify which package containing the interested product.  For example, we have selected GNU Complier for the download.

    After you have downloaded all packages that are needed for the C/C++ plugin module.  You should follow the instructions in [2] to configure it.  If you didn't set them up correctly, you will run into this "No Compiler" error.



    Then just follow the provided instructions to fix the issue.  For example, here is the tool collection we have in the environment:


    As you can see, most of our tools are provided from the cygwin64 installation.  But, you don't need to specify everything out of your cygwin64 installation.  For example, we have chosen make.exe out of our MKS Toolkit installation (see [5]).

    References

    1. Download NetBeans IDE
    2. Configuring NetBeans IDE 8.0 for C/C++/Fortran
    3. Installing and Setting Up the Compilers and Tools
    4. cygwin.com
    5. Tool Collection Notes
      • Dependency checking requires support from your tool collection (make and compilers). 
        • It has been tested with the Oracle Solaris Studio tool collection and GNU tool collections including Cygwin and MinGW. 
        • Dependency checking works when Oracle Solaris Studio compilers are used together with Oracle Solaris make and when GNU compilers are used with GNU gmake. Mixing Oracle Solaris make with GNU compilers and vice versa is not supported.

    C++: Polymorphism Example with Declarations and Definitions Separated

    If you use Eclipse CDT[1] to develop C++ programs, you will get both .cpp and .h files when you create a new class.  Taking "Virtual Member" example from [2], we demonstrate what it will look like after separating declarations from definitions.


    Monolithic Module


    On the Internet, most of examples are given similar to the one below.  It's OK to put everything in one file and test it in a student's project.  More realistically, all large applications are modularized.  In C++ programming, you should separate objects into modules and object's declarations and definitions into header and cpp files.

    // virtual members
    #include <iostream>
    using namespace std;
    
    class Polygon {
      protected:
        int width, height;
      public:
        void set_values (int a, int b)
          { width=a; height=b; }
        virtual int area ()
          { return 0; }
    };
    
    class Rectangle: public Polygon {
      public:
        int area ()
          { return width * height; }
    };
    
    class Triangle: public Polygon {
      public:
        int area ()
          { return (width * height / 2); }
    };
    
    int main () {
      Rectangle rect;
      Triangle trgl;
      Polygon poly;
      Polygon * ppoly1 = &rect;
      Polygon * ppoly2 = &trgl;
      Polygon * ppoly3 = &poly;
      ppoly1->set_values (4,5);
      ppoly2->set_values (4,5);
      ppoly3->set_values (4,5);
      cout << ppoly1->area() << '\n';
      cout << ppoly2->area() << '\n';
      cout << ppoly3->area() << '\n';
      return 0;
    }

    One Definition Rule (ODR)


    Someone has posted this question on Stackoverflow:
    Is is a good practice to put the declaration of C++ classes into the header file?
    Here is the answer:[3]
    C++'s compilation model dates back to the days of C, and so its method of importing data from one source file into another is comparatively primitive. The #include directive literally copies the contents of the file you're including into the source file, then treats the result as though it was the file you had written all along. You need to be careful about this because of a C++ policy called the one definition rule (ODR) which states, unsurprisingly, that every function and class should have at most one definition. This means that if you declare a class somewhere, all of that class's member functions should be either not defined at all or defined exactly once in exactly one file. There are some exceptions (I'll get to them in a minute), but for now just treat this rule as if it's a hard-and-fast, no-exceptions rule. 
    When you separate an object's declarations from its definitions, here is what will be generated by CDT in the header file:

    
    #ifndef POLYGON_H_
    #define POLYGON_H_
    
    class Polygon {
      <snipped>
      ...
    };
    
    #endif /* POLYGON_H_ */

    Using the above convention, you can safeguard your codes to include any declaration only once directly or indirectly.

    Sample Codes


    Without much ado, here are the sample codes after the modularization and the separation of declarations and definitions:

    Polygon.h

    
    #ifndef POLYGON_H_
    #define POLYGON_H_
    
    class Polygon {
    protected:
        int width, height;
    public:
     Polygon();
     virtual ~Polygon();
     void set_values (int a, int b);
     virtual int area ();
    };
    
    #endif /* POLYGON_H_ */

    Polygon.cpp

    
    #include "Polygon.h"
    
    Polygon::Polygon() {
      // TODO Auto-generated constructor stub
    
    }
    
    Polygon::~Polygon() {
      // TODO Auto-generated destructor stub
    }
    
    
    void Polygon::set_values (int a, int b) {
      width=a; height=b; 
    }
    
    int Polygon::area (){ 
      return 0; 
    }

    Rectangle.h

    
    #ifndef RECTANGLE_H_
    #define RECTANGLE_H_
    
    #include "Polygon.h"
    
    class Rectangle: public Polygon {
    public:
     Rectangle();
     virtual ~Rectangle();
     virtual int area ();
    };
    
    #endif /* RECTANGLE_H_ */
     

    Rectangle.cpp

    
    #include "Rectangle.h"
    
    Rectangle::Rectangle() {
      // TODO Auto-generated constructor stub
    
    }
    
    Rectangle::~Rectangle() {
      // TODO Auto-generated destructor stub
    }
    
    int Rectangle::area ()  { 
      return width * height; 
    }
     

    Triangle.h

    
    #ifndef TRIANGLE_H_
    #define TRIANGLE_H_
    
    #include "Polygon.h"
    
    class Triangle: public Polygon {
    public:
     Triangle();
     virtual ~Triangle();
     virtual int area ();
    };
    
    #endif /* TRIANGLE_H_ */
     

    Triangle.cpp

    
    #include "Triangle.h"
    
    Triangle::Triangle() {
      // TODO Auto-generated constructor stub
    
    }
    
    Triangle::~Triangle() {
      // TODO Auto-generated destructor stub
    }
    
    int Triangle::area(){
      return (height * width / 2);
    }
      

    VirtualMember.cpp

     

    // virtual members
    #include
    #include "Polygon.h"
    #include "Rectangle.h"
    #include "Triangle.h"

    using namespace std;

    class VirtualMember {
        // TODO Auto-generated constructor stub

    };



    int main () {
      Rectangle rect;
      Triangle trgl;
      Polygon poly;
      Polygon * ppoly1 = &rect;
      Polygon * ppoly2 = &trgl;
      Polygon * ppoly3 = &poly;
      ppoly1->set_values (4,5);
      ppoly2->set_values (4,5);
      ppoly3->set_values (4,5);
      cout << ppoly1->area() << '\n';
      cout << ppoly2->area() << '\n';
      cout << ppoly3->area() << '\n';
      return 0;
    }


    References

      1. Eclipse CDT
      2. Polymorphism in C++ 
      3. Is is a good practice to put the declaration of C++ classes into the header file?