How to manually compile a C++ program.

Hi, I'd like to know how to manually compile a C++ program, without the use of an IDE or auto-compiler. I've been told I can do so with windows Command Prompt and Notepad. How exactly does this work?
Glad you asked. I think I'm the man for the job of explaining it, as its mostly how I work. You really aren't a coder until you've mastered this, in my opinion.

The first thing you need to realize is that your compiler is part of what's known as a 'compiler toolchain'. The toolchain contains all the various command line programs necessary to translate your source code into the binary output you want, be it exe file, dll, lib, or whatever.

Typically, your toolchain is located in the bin subdirectory of either your Visual Studio installation, or, if you haven't installed Visual Studio, in the Platform SDK installation. And yes, you can obtain the whole working toolchain without a Visual Studio installation by simply downloading the SDK.

Next thing you have to realize is that if you are working in a command prompt window and you attempt to invoke the compiler, which, since this question is in the Windows sub-forum, I'm assuming you want to use 'cl' - the Microsoft compiler, then your computer is going to have to have some way of knowing where cl.exe is, i.e., the Microsoft compiler. Otherwise, you'll just get a cryptic message something to the effect that cl is not recignized as an operable program, batch file, or whatever. I'm sure you must have seen that message.

So the way you tell the operating system where the compiler toolchain is located is by executing a batch file Microsoft provides with every SDK or Visual Studio install named vcvarsall.bat.

Rather than executing that directly I'd recommend you look for shortcuts to it which Microsoft always puts on your Start Menu when the SDK or Visual Studio is installed. Right now I'm typing this on a box with Win 7 Pro installed which has Visual Studio 2008 Pro on it. On my Start Menu is a shortcut named this under one of the Visual Studio 2008 entries...

Visual Studio 2008 x64 Win64 Command Prompt

There are quite a few there actually. And they can be named differently depending on which version of the SDK or Visual Studio you have. In any case, I'd recommend you right click on it and send a copy of the shortcut to your desktop. That's easier to find than searching on the extensive Start menu when you want it.

Next issue is this. When you click on that it will open a command prompt Window to some folder/directory you don't want. You really need to make a directory for your code project.

continued...

Last edited on
Here I am after having lost 20 minutes of work replying to you because this stupid site lost my post and locked up or something. Now I'll type this first in Word then paste it. I hate losing my work. My fault really. I've lost tons of work replying on this site.

Anyway, you need to make a folder/directory for you project, then navigate there with the command prompt and change directory commands, because the Visual Studio or SDK command prompt will likely open up to some nasty place within which you might not want to code. I'll tell you what I always do. I don't think its the recommended thing to do, but I do it anyway, because I'm old fashioned and don't mind doing thigs my way.

I make a directory right off my C:\ drive like so...

C:\Code

Then I create subdirectories for all the various programming languages and tools I use. In the case of Visual Studio I have this...

C:\Code\VStudio\VC++9\x64

etc.

So if we want to make a test program to get you started command line compiling lets make this folder...

C:\Code\VStudio\VC++9\x64\Test

So you'll want to navigate to there with your Visual Studio or SDK command prompt using Change Directory commands like so....

C:\.....\......\.....\:\>CD [ENTER]

C:\>

C:\>CD C:\Code\VStudio\VC++9\x64\Test [ENTER]

So it should look something like this...

C:\Code\VStudio\VC++9\x64\Test:\>

Now try to see if cl.exe is working for you to make sure vcvarsall has set your paths right. Execute this...

C:\Code\VStudio\VC++9\x64\Test\:>cl /? [ENTER]

That's the command to output command line compiling switches for cl.exe. You'll need to press [ENTER] a few times to get through it.
If you don't get the correct output then something is wrong with your paths. Make sure you have the right shortcut you are executing from your Start Menu. Assuming cl.exe is working for you, our next step is to simply type in some source code in a *.cpp file in our working directory as set up above. So let's do Dennis Ritchie's original Hello, World! program. Open up Notepad and save this file to Hello.cpp in the directory we've set up above...

1
2
3
4
5
6
7
8
9
#include <stdio.h>

int main()
{
 printf("Hello, World!\n");
 getchar();

 return 0;
}


Having done whatever is necessary to save that to Hello.cpp execute this at the command prompt...

cl Hello.cpp [ENTER]

That's it. The compiler will grind for a few seconds, spit out some output, and you should end up with Hello.obj and Hello.exe in your working directory. Let me know how it goes.

Last edited on
Also, I've written up and posted two tutorial lessons on this involving GUI apps at this site, but they use GCC instead of VC. If you want bto try them with VC let me know and I'll help you. Its easy!!!

http://www.jose.it-berater.org/smfforum/index.php?topic=4611.0
Awesome!!!!!!!! Thanks man! I really appreciate it! I'm not using Visual studio I was using a different IDE (Code::Blocks) and it uses GCC so the link you provided is probably going to help me a lot! I haven't read everything you've posted but I'm sure it'll help me. Thanks again, :D
Let me know if you need any help.
Thank you for sharing this useful information. This can be helpful for me to learn more.
Topic archived. No new replies allowed.