I’m learning C right now, from “C for Dummies” by Dan Gookin. I…actually intended to read “C++ for Dummies” but I couldn’t find it at my library. So C is pretty similar, I guess it will help me learn C++ eventually, and it does mention C++. But anyway, in order to follow the book smoothly, I found myself setting up GCC for Command Prompt so I can compile my code in it, which isn’t that hard, I have discovered. Here’s how you do it:

  1. Download MinGW, the best way to do this, I think, is with Dev C++. You can download Dev C++ from Sourceforge: http://prdownloads.sourceforge.net/dev-cpp/devcpp-4.9.9.2_setup.exe (9MB)
  2. Dev C++ is a complete IDE for working with C and C++. Install it using…the installer which you just downloaded. There should be a Dev-Cpp folder on your C: drive.
  3. Now make a folder for all your C files. Don’t delve deep into your documents looking for a place to put this folder. I should think that the smaller the path, the less annoying it is. I recommend a folder in the C: drive.
  4. OK. Now open command prompt. This is in the start menu under accessories, at least it is under Windows XP. If you can’t find it in the start menu, you can always open Run, and type “cmd”. If you can’t find run, you should be able to hold down the Windows Key (the one on your keyboard with the windows symbol that might say “start”) and press R. If you can’t find the windows key or this doesn’t work, seek help.
  5. Now in command prompt, type “cd \windows” (wtq) and press enter. This makes the current directory the windows folder, on your C: drive. Your windows folder might be called something else, maybe in older versions of windows. Change accordingly.
  6. OK. Slowly type “copy con gcc.bat” (wtq) and press enter. This creates a batch file which you can refer to when compiling code.
  7. Even slowerly type this line:
    • @c:\Dev-cpp\bin\gcc %1 %2 %3 %4 %5 %6
  8. Check it. If it is wrong you will probably have to start all over, as I had to when I noticed there was no gcc folder in the bin folder, but there was one in the lib folder. So I changed it to lib, which was wrong, and I was forced to start all over. I was wrong, because, I have noticed, command prompt never uses .exe file extentions, so it puts no file extention. There is now a gcc.exe file in my bin folder. By the way, the circle in front of the line is wordpress formatting, it is a bullet, which isn’t included on most standard keyboards anyway, so there is no need to type the bullet.
  9. Press enter emphatically, with great drama and an audience of 2000.
  10. Press F6. It is a button usually found above the normal 6 key. You will notice “^Z” appearing.
  11. Press enter. You will see “1 file(s) copied”. You have successfully created gcc.bat. The only problem is, it may not be where you need it to be. If it isn’t, it will not work.
  12. Change the current directory to the folder you created earlier by typing “cd \the name of you folder or the path of your folder from the c: drive” (wtq) and pressing enter.
  13. You should notice the prompt changing to the path of the folder you created, such as C:\CPrograms>.
  14. Now type “gcc -v” (wtq) and press enter. If a lot of stuff appears on the screen and it doesn’t imply that the computer is angry at you, you have succeeded. If an error message appears saying something like ” ‘gcc’ is not recognized as an internal or external command, operable program or batch file” you have not succeeded. This is probably the result of you failing to properly read this tutorial, or me failing to properly write it. It would be much more convenient if it was the former.

Now, how to compile C code using the compiler. It is much more easy.

  1. Create a source file. something.c. Good. If it is properly coded, you should be able to complete the next steps. Make sure you are currently in the directory with your source code file. This should be the directory you made earlier in the tutorial.
  2. type “gcc something.c -o something” (wtq). The second something is the name of the .exe file the compiler creates. Press enter.
  3. If you get an error message, your code is wrong. If you get nothing, it has compiled successfully.
  4. Now type the second something to open the exe.
  5. With luck, it will do something. If you’re just using a text printf function or something similar if will appear inline with the command prompt.

Congratulations. You have just compiled some c source code in command prompt using the GNU Compiler Collection.

SCREENSHOT!

The Epic Life & Death Story of Setting up GCC.

You should be feeling good about yourself. Thank you for reading this tutorial. If anything is wrong let me know.

Advertisements