C++ & Fortran development in Windows using the MinGW-W64 GCC and NetBeans

A page dedicated to the setup and the usage of the C++ and Fortran GCC binary compilation workchains in the Netbeans IDE

Preconditions

  1. The MinGW-W64 GCC toolchain (including MSYS2) has been installed as per the instructions provided in our page on “How to install the GCC for Windows“.
  2. NetBeans (version 8.2 or higher) has been properly installed. Although the following instructions are likely to work with NetBeans versions older than 8.2, it is recommended to update either to such version or to the current version at the time of reading. This is important if you also want to use NetBeans to develop Fortran source code and to compile it using the GCC. Detailed installation instructions are available from the corresponding Netbeans website page. Please note that is important to run the NetBeans installer as an Administrator, just to avoid early termination issues linked to the lack of administrative priviledges at some point during the installation process.
  3. The GCC C/C++/Fortran plugin for NetBeans (ver 8.xx) has been properly installed. See the “Configuring Netbeans IDE 8.0 for C/C++/Fortran” instructable. Note that you are expected to have already installed MinGW-W64 and MSYS2 as per point 1 above. Hence, most of the instructions in the said instructable are not relevant. Only consider sections “Enabling C/C++/Fortran in the IDE” at the beginning and “Verifying the Installation” at the end of the instructable.
  4. NetBeans has detected all the installed components of the MinGW-W64 GCC toolchain. To check that is the case, go to menu Tools > Options and select tab C/C++. This should show the right path to the relevant binaries, e.g.
  5. NetbeansGNUSetup

    Paths to the various C/C++ toolchain executables detected by Netbeans

    Note that QMake (an alternative to make and CMake) is not needed if you are not using the Qt application framework to create GUIs for your C++ programs. All toolchain components have been automatically detected by NetBeans from the Windows Path environment variable. Different toolchains can be selected (e.g., corresponding to different GCC versions, architectures, or compilers).

    Finally, note that during its installation, NetBeans uses the Windows Path environment variable and other setups to find the path of the various executables it needs for its compilation toolchains. So, for instance, Java binaries are detected by looking in the directory C:\ProgramData\Oracle\Java\javapath. Conversely, MinGW GCC toolchain executables as the one shown in the above picture are detected using the Windows path (and thus the various MINGW_HOME and MSYS_HOME specific system environment variables. Their setup is described in the “MinGW toolchain installation instructable “). Hence, if  environment variables heve not been (correctly) set at Netbeans installation time, the corresponding executables will not be found by the IDE. If you expect NetBeans to automatically find and record them correctly at startup after  the (missing) program path and the specific environment variables have been correctly set in Windows, you will be wrong. This is because NetBeans records the paths to the needed toolchain binaries at installation time only, and keeps complaiing later on if it does not find them in the path recorded there and then, either because the latter was wrong or because it had been left blank at NetBeans installation time . Consequently, you would need to set the correct path to the needed toolchain executables manually in NetBeans. As far as Java is concerned, this can be done by opening the NetBeans configuration file netbeans.conf  (typically in the directory C:\Program Files\NetBeans 8.2\etc or equivalent) and modifyiong the line that specifies the JDK default home location, which looks like

    netbeans_jdkhome="C:\Program Files\Java\jdk1.8.0_101"

    However, as C/C++/Fortran source code editing compilation is managed through a plugin (as opposed to Java), the path to each specific GCC toolchain executable needs to be edited directly in the Tools > Options > C/C++ tab.

Further points to consider

  • This page only focuses on how to set up C++ and Fortran projects in NetBeans for executables to be generated using MinGW. However, adapting the GCC C++ MinGW toolchain setup in Netbeans under Windows  to the generation of C executables is pretty straightforward anyway.
  • Note that although often referred to as the “C/C++ plugin”, the NetBeans plugin for the GCC also includes support for Fortran programming by default. Unfortunately, this is not very clear and beginners may struggle to find a way of creating Fortran programs using the “C/C++” plugin. In reality, once you properly install the GCC plugin, Fortran support in NetBeans is automatically included and ready to be used. It’s just that plugin developers did not implement ways to make Fortran support explicitly visible upfront. In other words, you must first create a Fortran project as if it were a C/C++ project (it is not possible to select a “Fortran Project” as such) and then tell NetBeans that such project is actually a Fortran one. To know more, see section “Setting up a Fortran project” below.

Setting up a C++ project in NetBeans

Selecting File > New Project > C/C++ presents a list of choices that depend on the specific task at hand, i.e.

CppProjectKinds

Different kinds of C++ projects that Netbeans can create

Very likely, you may want to create a new project from scratch (i.e., a C/C++ Application) rather than a project from existing sources or a library.
To create an empty project with still-to-be-written source files, select “C/C++ Application“. Netbeans will suggest a project setup configuration like this:

CppProjectSetupConfiguration

Project setup configuration in Netbeans

It is possible to select different toolchains if more than one is installed and detected by NetBeans. It is not strictly necessary to create a Main file; in all those cases where you may want to use existing source code, you should avoid creating a Main file unless you need it to correctly compile and run the resulting executable. Press the Finish button to complete the setup and to let NetBeans create an empty project.

You now have two options:

  1. Create new source code files to contain your functions or classes.
  2. Include existing source code files in your project.

In both cases, right-click on the “Source Files” in the Projects hierarchy tree:

ProjectHierarchy

Netbeans project hierarchy tree

and select one of the following three options:

  • New file – you are presented with a list of different types to choose from
  • Add existing items – use this option if you want to add a single file or a collection of files distributed across different file system directories.
  • Add existing items from folders – use this option if you want to add multiple files from one or more specific directories, which can be selected and linked to the file directory hierarchy of your project.

Setting up a Fortran project in NetBeans

As hinted, it is not possible to select “Fortran project” from File > New Project. However, things are not as difficult as they appear. Just proceed as follows.

  1. Create a new Fortran project as if it were a C/C++ project, selecting one of the following options depending on what you may want to do:

    CppProjectKinds

    Select your kind of Fortran project as if it were a C/C++ one

  2. Once an option has been selected (e.g., a C++ Application, with the understanding that this will later become a Fortran Application), Netbeans will suggest a project setup configuration like this:
    FortranProjectSetupConfiguration

    How to tell Netbeans that your C/C++ project is actually a Fortran project

    Simply go to the line that deals with the Main File and select a Fortran versions from the drop-down menu. We use the latest “Fortran 2008” version, which allows free-format source code.

    Even if you don’t create a main file at this point in time, you can always add new source code files later on. In this case, NetBeans allows you to specify source code in your project as Fortran files (either in the old Fortran77 “fixed format” if you have legacy issues or in the Modern Fortran “free format” if you don’t).

Job done – you now have a Fortran project up and running. The GCC NetBeans plugin will look at the extension of source code files associated to your project to decide whether to use a C++ or a Fortran toolchain.

Setting the GCC-related project properties

You can now either create a simple C++/Fortran main file to generate an executable that displays the usual “Hello world” message or a much more sophisticated set of files for a real application. In both cases, you will need to make sure that the right toolchain compilation options, libraries, and directories to be included have been all set up correctly for your specific project. To do so, right-click on the project root folder in the NetBeans project hierarchy tree (see the corresponding picture above) and select the “Properties” item located at the bottom of the drop-down menu. By default, the Project Properties window will appear and the General category of properties will be shown as below.

NetbeansProjectProperties-general

Netbeans Project Properties windows showing the General category

It is worth going through the various categories, as setting these properly makes the difference between your software compiling (and running) and a series of compile-time or run-time errors that may drive you crazy because – despite such errors – your source code is actually correct.

Warning: We will only focus on some core project properties whose setup is essential for correct compilation, linkage and execution. Several other properties, which are not critical to begin C++/Fortran software development with the MinGW port of the GCC, are out of the scope of this instructable and are thus not covered. To know more on project property setups in NetBeans, please refer to the NetBeans help pages.

General category of project properties

  • Source Folders – You can check that the IDE shows the right source code (root) folder. If this is not right, the GCC compiler will look elsewhere, will not find anything and it will either (rightly) complain or simply report that there was no actual compilation task to do.
  • Manage Configurations – This menu allow you to choose whether your GCC toolchain configuration is set up for Debug or for Release.
    • Choosing Debug enables the various NetBeans (and GCC) debugging option but produces slightly less optimised code. To be used as a default util when your code is ready to be released to its users.
    • Choosing Release optimises the GCC compilation process and produces a slightly more performant executable code.

    In each case, you may want to use different GCC compilation and/or linkage and/or execution options. See the GCC Manual for further information on this.

Build category of project properties

This allows you to specify both compiler and linker setups. The GCC NetBeans plugin provides support for C, C++, Fortran, and Assembler. We will explicitly discuss C++ and Fortran setups in the following. To begin with, NetBeans has the possibility of setting up global Build category features, i.e.

NetbeansBuildprojectSetup

General Build category project setup options

At this configuration level, you can choose

  1. Which toolchain (tool collection) to use, in case you use different compilers or, more likely, different GCC versions. In most cases, however, you will have installed and/or enabled just one such version, so this setting may actually be left as it is.
  2. What kind of configuration you want to build, i.e., whether it is an Application, a Dynamic Library, or a Static Library. In most cases, it will be an Application, i.e. a standalone executable that can be run on its own.

The build category allows the specification of compilation setups for C++ and Fortran as well as for the linkage of  C++/Fortran-compiled programs.

C++ Compiler

The C++ Compiler setup window (see below) should be thoroughly checked to make sure that a C++ program can actually compile, link, and run.

NetbeansBuildCppSetup

C++ Compiler setup options

Apart from some basic options, most default compiler options are left blank.  While this is fine for a Hello World program, more substantial developments generally require a careful setup of at least some of these C++ compilation options, namely:

  • Include Directories – These are directories that contain header files needed for compilation purposes. For instance, compiling C++ software that uses the OpenCV library requires the inclusion of few directories that contain a range of OpenCV header files. Avoiding to include required header directories would result in a compilation failure.
  • Include Headers – If just a few specific header files (located in different directoris) are required, these can be indicated here rather than including whole directories in the above slot.
  • Development Mode – Debug is the default mode (and it is suggested to leave it as it is, unless something else is specifically required. Different modes can be selected if needed (e.g., Performance Release, Test Coverage).
  • Warning Level – This specifies how verbose the compiler can be; range goes from No Warnings to More Warnings (with Some Warnings being the default). You can also render Warnings as Errors if you need to enforce a strict approach. Increasing warnings may be useful at the beginning, during debugging and any time you may have compilation problems.
  • Architecture – This instruct the compiler to use 64 bits (the default) or 32 bits (to cross-compile for an i686 machine or for an x86_64 machine running Win32).
  • C++ Standard – By default, this is the latest-ish version, i.e. C++11, but it could be an earlier version in case of legacy code.

Fortran Compiler

The Fortran Compiler setup window (see below) is similar to the C++ one.

NetbeansBuildFortranSetup

Fortran compiler setup options

The relevant options are the same already discussed in the C++ compilation case, so they will not be repeated here.

Linker

The Linker setup window (see below) is another critical window that needs to be checked to guarantee that a C++ or Fortran program can actually compile, link, and run.

NetbeansBuildLinkerSetup

Linker options

The linker basically associates code from existing external libraries to your own code, enabling the latter to call the corresponding functions/objects contained in the former. Hence, the following two options should be carefully checked:

  • Additional Library Directories – This allows the specification of a list of directories where to find the libraries that need to be linked to the executable to be produced.
  • Libraries – This allows the specification of the actual libraries that need to be linked to  the executable to be produced. It should be noted that MinGW accepts either static libraries (with extension .a) and dynamic libraries (with extension .so). Note that the GCC requires that static libraries are prefixed with the keyword lib and have extension .a, e.g., libmyownlib.a. The lib prefix is required by the GCC linker to find the static library. Although it is possible to give a shared library the .dll extension, this is not recommended as the GCC uses the ELF (Executable Linkable Format), while the Microsoft Linker uses the older COFF (Common Object File Format). By giving a shared library the extension .so, you are indicating that this is a GCC-shared library rather than a Windows-specific one. In the past, there were issues with MinGW GCC linking  dynamic libraries with extension .dll directly. Hence, it was recommended to create a static library lib.a with exported symbols from .dll and link against it instead. However, since 2013 at least it appears that it should be fine to link directly against .dll. Users reported success with the MinGW-w64 distribution from 2013 onwards and had no issues with that.

Run

The Run setup window (see below) is another critical window that should be checked to make sure that a C++ or Fortran executable can actually run.

NetbeansRunSetup

Run setup options

The only relevant thing to note here is making sure to select either “Standard Output” or “External terminal” as the Console Type, otherwise the executable will not run and Netbeans will throw an error. Selecting the Standard Output option, NetBeans will show executable results in one of the IDE panes. Conversely, selecting the Esternal Terminal  option a Windows terminal (command prompt) will be opened and all executable output will be redirected there.

Compiling and running a Hello World test project

C++ Hello World project

The following should work straightforwardly:

#include <iostream>

int main()
{
std::cout << "Hello World from Cpp!\n";
return 0;
}

Fortran Hello World project

The following should work straightforwardly:

PRINT*, ‘Hello World from Fortran’
END


Page last updated by ECOMPAT on 13/10/2016