C and C++ are popular programming languages that quickly provide access to making formula's and solving complex issues with minimal resources expended. The issue is finding a way to compile and make the program executable.
One of your options may be using Visual Studio, which is a text-editor, compiler in one. While useful, some may find it too complex or want a simpler way to modify and compile their code. That's where Minimalist GNU for Windows comes in, or MinGW for short. You use MinGW in your command line interface, the text only application you can quickly access by pressing the windows key and typing in "cmd".
By the end of this Instructable, you should have MinGW properly installed, and be able to compile any c/c++ program anywhere on your computer.
NOTE: This instructable is purely for Windows environments. Linux comes with GCC, a GNU collection of compilers. This is the desired result for our environment, to get GCC onto Windows.
You'll need a computer to download the software and a decent internet connection for a stable and swift installation. While not required, you may also want a thumbdrive, in case you want to have a physically portable copy of MinGW setup.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Download the Installer.
The first thing we need to do is get the installer.
To do that, we need to go to the MinGW website.
From there, you will want to go to the downloads link, located on the left side in the navigation section. In case you cannot find it or it's moved, this link will take you there
What we want is the mingw-get-setup.exe file. Once the file has downloaded, we can move on to the next step.
Step 2: Properly Install MinGW
The first thing it asks is where you want to install it. To keep it easy to access, I recommend leaving it in the default directory. Otherwise, you can set this up anywhere, even on thumb drives.
The next option talks about the User Interface for MinGW. We will work with a UI in this case, as it's better to see what we can do.
Step 3: Package Installation
Now that we've finished the installation of MinGW, we have to pick specific packages to install. In this instructable, we are going to keep it simple and work with the basic set up, as we can get the desired result without having to delve into all of the individual packages that MinGW offers.
In the left Table of Contents, make sure you are looking at Basic Setup. From there you should see 7 items in the right table. For C/C++ compilers, you will want to install the following packages: mingw32-base-bin and mingw32-gcc-g++-bin. GCC is used for c programs, and G++ is used for C++ programs.
To install a package, right click the package and then click "Mark for Installation". Once you've marked the packages you want, go to the top left and click on "Installation". You will then "Apply Changes," which will then show you a new window that shows the changes you will make. Click "Accept" and the installation will then begin. A download will begin, and then another window will appear, this time showing you if your installation is finished.
Step 4: Being Able to Use Your Compiler Anywhere With CMD
With your newly installed compiler, you'll notice that your command prompt doesn't recognize gcc or g++ as commands. In order to do this you must do the following:
- Press the Windows key, type in "Edit the system environment variables" and then hit enter
- A new window will pop up, called System Properties.
- If not already there, go to the Advanced tab.
- Look towards the bottom right, and click on Environment Variables.
- From there, you will see two separate lists of variables. Depending on your preference, you can either set these paths locally to one user, or system wide. Either way, locate the "Path" variable and click on Edit.
- From the new window, click on New on the right side of the window, and then type in the following: "C:\MinGW\bin". We have to direct it to the bin directory in our installation because that's where our compilers, gcc and g++, exist.
- From there, hit OK in both windows, and open a new command prompt. Type in either gcc or g++ and see if the command replies back to you. Further test it by making a C or C++ program and attempt to compile it
Now you have the ability to make programs and compile them anywhere on your system.
To compile a program, all you have to do is call gcc if its a c program, or g++ if it's a c++ program, and then type in the name of the program you want to compile. Example: gcc helloworld.c or g++ helloworld.cpp
The next step, while optional, does explain some useful options you can use with your compiler.
Step 5: Useful Options You Can Use While Compiling a Program.
While not required for compiling your programs, these options will help you in debugging your code.
The syntax for how you put these options in are as follows for either gcc or g++: g(cc/++) -option argument
- -o : this option allows you to name the program on compile. By default, your program will be called a.exe. So by doing : "gcc helloworld.c -o Hello", you'll instead get Hello.exe
- -g : this option allows for another program to be able to use your code. "gdb" or GNU Debugger needs that -g option in order to work. GNU Debugger is a very useful tool in that it allows you to see how your variables and code works line by line. It's a bit advanced for newcomers, so it's a good idea to look up the commands you can do in it.
- NOTE: While useful, if you don't plan on using gdb, then you shouldn't use -g, as the file size is much bigger than your standard exe file.
- -Wall, -Werror, -Wextra, and -pedantic are all warning related options. Wall Wextra and pedantic will catch most errors and warnings while Werror converts all warnings into errors. The difference between these two is that if your program has warnings, it will still compile. If it has errors, however; the program will not compile. Werror in this case is more of a practice than a tool, in that it enforces correct behaviors under the c standard, which brings me to the last option I want to discuss
- --std= : This option tells the compiler of which standard to use. While probably not important in most cases, if you receive older code that may not work in today's standard, this option will enforce the standard you choose. Some notable standards are c99, c89, gnu99, gnu 89, etc. I'll say normally you won't use this option unless you need to.
- NOTE: This option is only for c
- Example of the options together: gcc --std=c99 -Wall -Wextra -pedantic -Werror -g helloworld.c -o hello
- Example of the options together in c++: g++ -Wall -Wextra -pedantic -Werror -g helloworld.c -o hello