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 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 220.127.116.1103 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.
- 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!
- 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.
- 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.)
- 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 interpreter, 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!
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.