Tuesday, August 27, 2013

How to Create a 64-bit C/C++ Windows Program with NetBeans 7.4, 8.0 or Higher

Updated: 04/18/2016

NetBeans IDE 8.0.1 C++ Project
I will show you how to setup NetBeans to create 64-bit C/C++ Windows executables with NetBeans 7.4, 8.0 or higher. This will also allow you to create and compile 64-bit GNU Assembly (GAS) programs.

The first thing you need to do is install Oracle JDK 8 update 20 or newer and NetBeans 7.4, 8.0 or newer. I will not go over this as it is clearly documented.

Next you will need to download the MSYS2 installer here. Make sure you download the 64-bit installer. It is good to have MSYS2 because it makes it easy to install and update MinGW-w64, GNU Make and other tools to build on Windows.

Optionally, you can download Rapid Environment Editor here. It allows easier editing of system environment variables on Windows.

Installing MSYS2 & MinGW-w64

  • When creating folders for installation, do not use folder names with spaces or deeply nested folder hierarchies.

  • Run the MSYS2 installer and install it to a convenient place on your system drive. I will use C:\Developer\msys64\ for these instructions. Navigate to the MSYS2 base directory and run msys2_shell.bat. An MSYS2 shell window will open and you will see some setup activity going on. Once the setup activity is done, you must close the shell window and run is again. Now run update-core. If one of the packages is updated during script run you must restart MSYS2. Keep running update-core and restarting MSYS2 until update-core cannot be found. At this point you have been updated to pacman and update-core is obsolete. To update everything now, you simply keep running pacman -Syuu and follow the instructions. Repeat this step until it says there are no packages to update.

  • Now you need to install the build packages. The first one is pacman -S base-devel. Then install pacman -S mingw-w64-x86_64-toolchain for building mingw64 packages. Optionally you can install pacman -S mingw-w64-i686-toolchain for building mingw32 packages.

  • See the MSYS2 Installation instructions for more information about updating the core packages, updating other packages and using the package manager.

  • See this page for more information about installing and using MSYS2 build tools.

  • Next we need to add the MinGW-w64 and MSYS2 bin folder paths to the Windows system path. To do this in Windows 7, go to the Start Menu and search "advanced system" and click on "View Advanced System Settings". Then click on the "Environment Variables" button.

Windows 7 Environment Variables

  • Now you can enter the folder paths directly in the system path, but I like to create two variables MINGW_HOME and MSYS_HOME for these paths (omitting the bin folder) and then adding those variables to the path string. In our case MINGW_HOME will be C:\Developer\msys64\mingw64 and MSYS_HOME is C:\Developer\msys64\usr. Do not add a backslash at the end!

Windows 7 Edit System Variable

  • Next, edit the PATH variable and add %MINGW_HOME%\bin;%MSYS_HOME%\bin at the end.

Windows 7 Edit System Path

  • Check to see if the MINGW-w64 and MSYS2 paths are valid by entering path in the Windows command prompt interpreter cmd.exe. Do not include any other paths to build tools of the same name before these new entries or your builds will fail!

Windows 7 cmd.exe

  • Check to see if make and g++ is accessible by typing make --version and g++ --version in the Windows command prompt. You may continue to do the same for the rest of the tools gcc.exe, gfortan.exe, as.exe and gdb.exe.

Setting up the Build Tools in NetBeans

  • Now you should already have NetBeans installed and have installed the full package—in which you can just activate C/C++ through Options—or you will have to install the C/C++ plugin with the NetBeans plugin manager.

  • Go to Tools >> Options >> C++ and you will see the build tools panel. If NetBeans has not already found the standard build tools automatically, try clicking the "Restore Defaults" button. If this does not work, you will need to add them manually. To add the build tools, click the "Add..." button and then add the path to the bin folder—in which you can just activate C/C++ through Options—or you will have to install the C/C++ plugin with the NetBeans plugin manager.

NetBeans IDE 8.0.1 Add New Tool Collection

  • You will now see a tool collection in the Build Tools panel.

NetBeans IDE 8.0.1 C++ Build Tools

  • To test this, we will create a new C++ project. Go to File >> New Project >> Categories: C/C++, Project: C/C++ Application. Click the Next button and accept all the default settings by clicking the Finish button.

NetBeans IDE 8.0.1 New C/C++ Application

  • In the new projects Source Files folder, open main.cpp and add the library #include <iostream> before the namespace declaration and add cout << "Hello World!"; before the return statement in main.
  • Next, open the Project Properties and under Categories: Run, change the Console Type to either "Standard Output" or "External Terminal", otherwise the project will fail to build. (The "Internal Terminal" option will only work if you have Cygwin installed.)

NetBeans IDE 8.0.1 C/C++ Project Properties

  • Press F11 on your keyboard to build the project. The project should have built successfully and you will see a "successful" message in the NetBeans output window.

NetBeans IDE 8.0.1 Build Output Window

  • Press F6 on your keyboard to run the project. You should see the output Hello World! and a "successful" message below it.

NetBeans IDE 8.0.1 C++ Run Output Window

  • Now test regular C builds. Create a new project as before, but when you are at the last dialog panel during project creation, change the main type from C++ to C before clicking the Finish button. 

  • Type printf("Hello World!"); inside main and build/run the project as you did before. You should see the same sort of output, but gcc.exe is used to compile the project instead of g++.exe.

  • To test 64-bit assembly capabilities, the easiest thing to do is generate an assembly language file from the C program you just created. Using the Windows command prompt interpretor, browse to the directory that contains main.c source file and run gcc -S main.c and a 64-bit main.s assembly source file will be created.

  • Right click on main.c in the project and choose "Remove From Project". Right click on the Source Files folder and choose "Add Existing Item..." and add main.s to the project.

  • Now build/run the project as before. This time the file is complied with as.exe and the output will be identical as before.

NetBeans IDE 8.0.1 Compiling and Running a GAS Assembly Language Program

Now you are up and running!

Note that if you want to build on the command line, you must run C:\Developer\msys64\msys64_shell.bat. Optionally, if you have installed MinGW-w32, run C:\Developer\msys64\msys32_shell.bat to build 32-bit programs on the command line.

Additional Information


  1. These is a common question for programmers in C++. Personally, I prefer C++ as a programming language. I find it easier to code in this language than in C. Anyway, I think that you have a good collection of images and they make it easier to understand the process, from installing Oracle to running the project. Thanks for sharing.

  2. Your excellent instructions worked well for me and I thank you for taking the time to present them!

  3. Your MSYS link (to the tar file) doesn't actually include a bin directory (any more?). There's no windows executable install or configure either. Installing MSYS per http://genome.sph.umich.edu/wiki/Installing_MinGW_%26_MSYS_on_Windows has let NetBeans find a make tool.

    1. You downloaded the sources file, not the binary file. The binary file is under it with a zip extension named MSYS-20111123.zip

    2. I agree that I downloaded the sources.

      I was suggesting that you might perhaps clarify which of the seven links above leads to the executable download, not least because I clicked all of them and read carefully, but I still can't see a link that leads to the executable you talk about. mingw-builds-install.exe installed the MINGW executables, but I had to visit a different set of instructions to find MSYS-1.0.11.exe. It's your call, of course, but your instructions did not work for me and I thought you might find it helpful to know that and why.

  4. Hi
    I think there is one installation missing. It is recommended in the link:https://sourceforge.net/p/msys2/wiki/Contributing%20to%20MSYS2/

    pacman -S msys2-devel

    Regards, Harald

    1. I think that is only needed if you are going to develop msys2 packages. The link is really about building msys2 packages, not building general programs. It's probably not and idea link for me to use, as it contains to much information for general program builds and could confuse a novice.