How To Create a 64-Bit C/C++ Windows Program with NetBeans
Contributed By: Mark Wilmoth
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 C/C++ build tools for NetBeans to use. You can either download Cygwin or MinGW build tools, but I prefer MinGW because it is lightweight and far easier to install. There are two websites for obtaining MinGW. One is the old website http://www.mingw.org which leads to it's SourceForge project page for downloading the older 32-bit only installation and the other website is http://mingw-w64.sourceforge.net where you can install MinGW-w64, which is the 32-bit & 64-bit installation. For Windows binaries, download the MinGW-builds 7zip packages. For these instructions we will be using the 4.8.2 package for win32-seh threads. Download the MinGW-builds 4.8.2 7zip package here. The filename to download is x86_64-4.8.2-release-win32-seh-rt_v3-rev4.7z. (Note that you may also install MinGW-w64 using the MSYS2 package manager by running pacman -S mingw-w64-x86_64-toolchain or the 64-bit toolchain or pacman -S mingw-w64-i686-toolchain for the 32-bit toolchain.)
NetBeans only supports GNU Make, which is included in MSYS2, so you need to install MSYS2 along with MinGW. It is good to have MSYS2 because it includes some GNU tools that are useful for Windows, like md5sum and other command line tools that do things that can't be done with Windows. Download the latest MSYS2 tar archives here.
One last thing you will need, is to download 7zip and install it, because the packages from MinGW-builds are 7z format. 7zip will also extract a tar archive.
Installing MinGW-w64 & MSYS2
- When creating folders for installation, do not use folder names with spaces or deeply nested folder hierarchies.
- First, extract the contents of the MinGW-w64 7zip archive to C:\Developer\mingw64\.
- Now extract the MSYS2 tar archive. Extract everything in the msys64 folder to C:\Developer\msys64\. Navigate to this 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. The first time after installation you should run update-core. If one of the packages is updated during the update-core script run, you must restart the MSYS2 shell window again. Now run pacman -Su to update packages. Now run pacman -S make to install make.exe. See the MSYS2 Installation instructions for more information about using the package manager. (Note that you may also install MinGW-w64 using the MSYS2 package manager by running pacman -S mingw-w64-x86_64-toolchain or the 64-bit toolchain or pacman -S mingw-w64-i686-toolchain for the 32-bit toolchain.)
- 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.
- 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\mingw64 and MSYS_HOME is C:\Developer\msys64\usr Do not add a backslash at the end!
- Next, edit the PATH variable and add %MINGW_HOME%\bin;%MSYS_HOME%\bin at the end.
- 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!
- 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.
- Go to Tools >> Options >> C++ and you will see the build tools panel. If NetBeans has not already found the build tools automatically, 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.
- You will now see a tool collection in the Build Tools panel.
- 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.
- 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.
- 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.
- Press F6 on your keyboard to run the project. You should see the output Hello World! and a "successful" message below it.
- 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.
Now you are up and running!
To compile a 32-bit program, go to the projects context menu and choose Properties. Under Categories: Build, select the build tool of interest and change Architecture to 32-bit. To compile a 32-bit Assembly file, set both gcc.exe and as.exe architectures to 32-bit. (Note that this may be problematic due to the way MinGW and other toolchains may be assembled. It is best to download and use the 64-bit toolchain for 64-bit programs and the 32-bit toolchain for 32-bit programs.)
- MSYS2 – A Cygwin-derived software distro for Windows using Arch Linux's Pacman (This is an easy way to install MinGW builds too!)
- MSYS2 Wiki – installation, upgrade and other documentation
- MinGW-w64 – for 32 and 64 bit Windows