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. 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 7 update 25 or newer and NetBeans 7.4. 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 more 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 the Windows binaries installer, use the MinGW-builds installer.
NetBeans only supports GNU Make, which is included in MSYS, so you need to install MSYS along with MinGW. It is good to have MSYS 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. MSYS is a bit difficult to find from the MinGW-w64 website. Here is the direct link to download MSYS.
Update: If you would like a newer version of MSYS GNU Make, there is a newer version of MSYS, including Make v3.82.90, in these MinGW-builds MSYS packages at sourceforge.net.
Once you have downloaded MSYS, you can just simply unzip it to a directory on your system, no installation is needed. Let's create a folder C:\Developer\MSYS\ to put MSYS in.
Now you need to download the MinGW-builds binaries for Windows which can be downloaded here: http://sourceforge.net/projects/mingwbuilds. It is an executable installer that will non-evasively download and install the binaries to your system.
Installing MinGW-w64 with MinGW-builds
- Run mingw-builds-install.exe and you will be prompted to choose different versions and settings for the type of binaries you desire. It is best to select the latest version and build revision. Although you can choose any of the Thread and Exception types offered, it is best to stick with Win32 threads and Windows Structured Exception Handling (SEH) if you don't know much about it.
- Click the Next button and choose a folder to install MinGW-w64 in. We will choose C:\Developer\mingw-builds\x64-4.8.1-win32-seh-rev4.
- Once MinGW-w64 is installed, we need to add the path to the MSYS and MinGW-w64 bin folder. 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\mingw-builds\x64-4.8.1-win32-seh-rev4\mingw64 and MSYS_HOME is C:\Developer\MSYS 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 MSYS 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, 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.