Building From the Command Line

 

Introduction

Most of the programs created in MS Visual C++ are also easily built, compiled, and executed inside of MS Visual C++. Sometimes, you will need to execute a program from the DOS prompt. This exercise simply shows how to compile a program from the command line.

By default, the command line is not ready to compile programs after MSVC has been installed. You must explicitly make this available.

If you haven't allowed done so, follow the following step to allow command lines.

  1. Open the DOS prompt (Start -> (All) Programs -> Accessories -> Command Prompt)
  2. At the DOS prompt, type the whole path to the bin directory. By default, as it is on mine, it should be: C:\Program Files\Microsoft Visual Studio\VC98\Bin. Therefore, you should type C:\>CD Program Files\Microsoft Visual Studio\VC98\Bin
  3. Then, you must execute the VCVARS32.BAT file by simply typing VCVARS32
    C:\Program Files\Microsoft Visual Studio\VC98\Bin>VCVARS32
     
  4. You should receive a confirmation message as follows:
    C:\Program Files\Microsoft Visual Studio\VC98\Bin>VCVARS32
    Setting environment for using Microsoft Visual C++ tools.
    Change back to the root directory

Once the command line utility is setup, you can create your projects.

  1. Start Microsoft Visual C++ and create a Win32 Console Application from the Projects property page of the New dialog box. Name the project however you want. I will call mine Exercise1 and located in the C:\Programs\MSVC
     
    New
  2. Click OK and create a C++ Source file called Main.cpp.
  3. Type the following in the empty file:
     
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	cout << "\nC++ is Fun!!!\n";
    	return 0;
    }
  4. Close the project and accept to save each file.
  5. On the command prompt, change to the directory in which the file was saved. For me, that would be C:\Programs\MSVC\Exercise1
  6. To build the project, type CL main.cpp and press Enter (CL stands for Compile and Link)
    You should receive a series of lines:
     
    Microsoft Windows XP [Version 5.1.2600]
    (C) Copyright 1985-2001 Microsoft Corp.
    
    C:\>CD Program Files\Microsoft Visual Studio\VC98\Bin
    
    C:\Program Files\Microsoft Visual Studio\VC98\Bin>VCVARS32
    Setting environment for using Microsoft Visual C++ tools.
    C:\Program Files\Microsoft Visual Studio\VC98\Bin>CD\
    
    C:\>CD Programs\MSVC\Exercise1
    
    C:\Programs\MSVC\Exercise1>CL main.cpp
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
    Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
    
    main.cpp
    C:\PROGRA~1\MIAF9D~1\VC98\INCLUDE\istream(547) : warning C4530: C++ exception ha
    ndler used, but unwind semantics are not enabled. Specify -GX
    C:\PROGRA~1\MIAF9D~1\VC98\INCLUDE\ostream(234) : warning C4530: C++ exception ha
    ndler used, but unwind semantics are not enabled. Specify -GX
            C:\PROGRA~1\MIAF9D~1\VC98\INCLUDE\ostream(229) : while compiling class-t
    emplate member function 'class std::basic_ostream<char,struct std::char_traits<c
    har> > &__thiscall std::basic_ostream<char,struct std::char_traits<char> >::put(
    char)'
    C:\PROGRA~1\MIAF9D~1\VC98\INCLUDE\ostream(234) : warning C4530: C++ exception ha
    ndler used, but unwind semantics are not enabled. Specify -GX
            C:\PROGRA~1\MIAF9D~1\VC98\INCLUDE\ostream(229) : while compiling class-t
    emplate member function 'class std::basic_ostream<unsigned short,struct std::cha
    r_traits<unsigned short> > &__thiscall std::basic_ostream<unsigned short,struct
    std::char_traits<unsigned short> >::put(unsigned short)'
    C:\PROGRA~1\MIAF9D~1\VC98\INCLUDE\istream(46) : warning C4530: C++ exception han
    dler used, but unwind semantics are not enabled. Specify -GX
            C:\PROGRA~1\MIAF9D~1\VC98\INCLUDE\istream(41) : while compiling class-te
    mplate member function 'bool __thiscall std::basic_istream<char,struct std::char
    _traits<char> >::ipfx(bool)'
    C:\PROGRA~1\MIAF9D~1\VC98\INCLUDE\istream(46) : warning C4530: C++ exception han
    dler used, but unwind semantics are not enabled. Specify -GX
            C:\PROGRA~1\MIAF9D~1\VC98\INCLUDE\istream(41) : while compiling class-te
    mplate member function 'bool __thiscall std::basic_istream<unsigned short,struct
     std::char_traits<unsigned short> >::ipfx(bool)'
    C:\PROGRA~1\MIAF9D~1\VC98\INCLUDE\xstring(525) : warning C4530: C++ exception ha
    ndler used, but unwind semantics are not enabled. Specify -GX
            C:\PROGRA~1\MIAF9D~1\VC98\INCLUDE\xstring(521) : while compiling class-t
    emplate member function 'void __thiscall std::basic_string<char,struct std::char
    _traits<char>,class std::allocator<char> >::_Copy(unsigned int)'
    C:\PROGRA~1\MIAF9D~1\VC98\INCLUDE\ostream(296) : warning C4530: C++ exception ha
    ndler used, but unwind semantics are not enabled. Specify -GX
            main.cpp(6) : see reference to function template instantiation 'class st
    d::basic_ostream<char,struct std::char_traits<char> > &__cdecl std::operator <<(
    class std::basic_ostream<char,struct std::char_traits<char> > &,const char *)' b
    eing compiled
    Microsoft (R) Incremental Linker Version 6.00.8447
    Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
    
    /out:main.exe
    main.obj
    
    C:\Programs\MSVC\Exercise1>
  7. Once the project has been built and an executable has been created (notice the main.exe line), to execute it, type the name of the executable. In this case, type main and press Enter
     
 

The main() Function and Arguments

The C++ Standard allows the main function to have or not have arguments. This allows the programmer to have some control on the behavior of the main() function and even the program as a whole.

You can use one or more arguments on the main function. Here is the general formula used:

int main(int argc, char *argv[])

The first argument, argc, specifies how many arguments will be passed to the main() function. The actual arguments are passed as an array of strings. Each string represents its own value. Because this argument is made of strings, you are partly in control of what the arguments can be.

In this syntax above, keeping in mind that argc represents the number of arguments, the main() function itself is the first argument at index 0. Therefore, argc is always at least 1. Its total number is from the elements of the argv array. This means that the value of argv[0] is main. If the user types anything after the name of the program on the command prompt, what follows the name of the program is argv[1]. For example, if the program is called Exercise1. Imagine that the program has been compiled. If the user types:

C:\MyPrograms\Exercise1 68

the value 68 would be argv[1]

Depending on how you write your program, the user can also be allowed to type more than one value. An example would be:

C:\MyPrograms\Exercise1 68 1250.50

Because the members of the argv argument are strings, it is your responsibility to get them and convert them to the desired value. This means that, just because the user types

C:\MyPrograms\Exercise1 68 1250.50

doesn't mean that 68 is an integer and that 1250.50 is a double. Of course, the C\C++ compiler provides you with conversion functions. To convert a value such as 68 to an integer, you can use the atoi() function. In the same way, you can convert a value such as 1250.50 to double using atof().

If the user would type a character, the value should be provided in single-quotes. If the value is a string, the user must type it in double-quotes.

Using these techniques, the user can be allowed to provide numeric, characters, or string values.

  1. To use arguments on the main() function, change the file as follows:
     
    #include <iostream>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    	double Operand1, Operand2, Addition;
    
    	Operand1 = atoi( argv[1] );
    	Operand2 = atoi( argv[2] );
    	Addition = Operand1 + Operand2;
    
    	cout << "\nFirst Number:  " << Operand1;
    	cout << "\nSecond Number: " << Operand2 << endl;
    	cout << Operand1 << " + " << Operand2 << " = " << Addition << endl;
    
    	return 0;
    }
  2. Save the file
  3. On the command prompt, recompile the file by typing CL main.cpp
  4. This time, when executing the program, type main 48.52 1205 and press Enter
  5. To compile with a string argument, change the program as follows:
     
    #include <iostream>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    	char Sentence[40];
    	strcpy(Sentence, argv[1]);
    
    	cout << "You typed " << Sentence<< endl;
    	return 0;
    }
  6. Save the file and compile it by typing CL main.cpp
  7. Execute the program by typing main "Santiago du Chili" and press Enter
  8. To use a combination of strings and numeric values, change the file as follows:
     
    #include <iostream>
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    	char FullName[40], strGender[20];
    	char cGender[10];
    	float Salary;
    
    	strcpy(FullName, argv[1]);
    	Salary = atof(argv[2]);
    	strcpy(cGender, argv[3]);
    
    	if( cGender == "m" || cGender == "M" )
    		strcpy(strGender, "Male");
    	else if( cGender == "f" || cGender == "F" )
    		strcpy(strGender, "Female");
    	else
    		strcpy(strGender, "Unknown Gender");
    
    	cout << "Employee Information";
    	cout << "\nFull Name: " << FullName;
    	cout << "\nGender:    " << strGender;
    	cout << "\nSalary:    " << Salary << endl;
    	return 0;
    }
  9. Save the file and compile it by typing CL main.cpp
  10. Execute the program by typing
     
    C:\Programs\MSVC\Exercise1>main "Patrick Mendoza" 18.52 "M"
  11. Press Enter
     
    MS DOS Prompt
  12. To dismiss the DOS prompt, type exit and press Enter.

Home Copyright 2003-2004 FunctionX, Inc.