Use C/C ++ compiler in console mode

Source: Internet
Author: User
Tags vc9

First of all: the articles that reference jjhou are well written, as follows:

 

--------------------------------------------------------------------------------

Use C/C ++ compiler in console mode
Hou Jie

--------------------------------------------------------------------------------

I always encourage C/C ++ learners to first target the console mode (DOS-like) program when they are new to this programming language. In other words, you don't want to write GUI programs, open windows, and have a dazzling screen at the beginning-it's just not going to fly first.

The so-called console program is a program in the text mode. We can train the C/C ++ language well without being distracted by other unnecessary guis.

I always thought that this was a matter of course, but I still found that many colleges and universities have freshman C/C ++ courses, students must write a small writer, painter, and Abacus as their mid-term or final assignments.

Sure enough, the world cannot be the same.

I not only think that C/C ++ Program Development objects should be dominated by console mode at the initial stage, but I also think that the C/C ++ program development environment should also be dominated by console mode at the initial stage. In other words, do not start with the integrated environment (IDE ). The integration of so many windows, so many functions, and so many preset values in the environment will make new programmers confused and fail to master some valuable knowledge and experience during program compilation.

When we have a minimum understanding of the Compilation Program, and then use the integrated environment, I think this is the best.

Therefore, no matter in <simple introduction to MFC> or <multi-type and virtual> books, I will briefly describe the job methods in console mode. <Simple introduction to MFC> listed in p.m. and <multi-type and virtual> listed in p.233.

However, I still occasionally receive questions from netizens (whether or not I have read two books), asking about how to compile the console mode, or asking them about the problems they encountered.

I will sort out this question again. Then, I can mail the whole question to the questioner.

★★Note: The following are suitable for PC environments:★★

● Environment variable settings required by the C/C ++ Compiler

In the early days, the C compiler on PC requires two environment variables:

LIB: This environment variable tells the compiler where libraries is necessary (under which disk directory)
INCLUDE: Tell the compiler where the necessary header files are (under which disk directory)

In addition, in order for us to get the compiler in any working directory, we must set the PATH.

From the beginning to the present, C/C ++ compilers all need these three environment variables.

● Take Visual C ++ as an Example

Take Visual C ++ as an example. If the file layout after installation is as follows:

C:/MSDEV/VC98/BIN: the compiler CL. EXE is available here.
C:/MSDEV/VC98/INCLUDE: C/C ++ header files
C:/MSDEV/VC98/LIB: C/C ++ standard libraries

You can write a batch file as follows:

Set PATH = C:/MSDEV/VC98/BIN; C:/MSDEV/COMMON/MSDEV98/BIN
Set INCLUDE = C:/MSDEV/VC98/INCLUDE
Set LIB = C:/MSDEV/VC98/LIB

The reason for setting PATH = C:/MSDEV/COMMON/MSDEV98/BIN is that the compiler CL. MSPDB60.DLL is required for EXE execution, and it is installed in C:/MSDEV/COMMON/MSDEV98/BIN.

If the program you write is not just a C/C ++ program, but also an MFC program, you can compile it in console mode. At this time, your environment variables should be set as follows:

Set PATH = C:/MSDEV/VC98/BIN; C:/MSDEV/COMMON/MSDEV98/BIN
Set INCLUDE = C:/MSDEV/VC98/INCLUDE; C:/MSDEV/VC98/MFC/INCLUDE
Set LIB = C:/MSDEV/VC98/LIB; C:/MSDEV/VC98/MFC/LIB

If you specify MFC/INCLUDE and MFC/LIB, the compiler and the connector can find the header files and libraries of MFC. If you still need to Use ATL, you must add C:/MSDEV/VC98/ATL/INCLUDE to the INCLUDE environment variable.

● Taking Borland C ++ Builder as an Example

Taking Borland C ++ Builder as an example, if the file layout after installation is as follows:

C:/BORLAND/CBuilder3/BIN: the compiler BCC32.EXE is provided here.
C:/BORLAND/CBuilder3/INCLUDE: C/C ++ header files
C:/BORLAND/CBuilder3/LIB: C/C ++ standard libraries

You can write a batch file as follows:

Set PATH = C:/BORLAND/CBuilder3/BIN
Set INCLUDE = C:/BORLAND/CBuilder3/INCLUDE
Set LIB = C:/BORLAND/CBuilder3/LIB

● How to compile C/C ++ programs in the console

First, enable a DOS Box (DOS Prompt, dos vm), and then execute the preceding batch files in the DOS box to complete environment variable settings. You can enter the set command in the DOS prompt to check whether the environment variable settings are correct.

Then you can directly enter the name of the compiler under the DOS prompt to start compilation. If you use Visual C ++, do this:

C:/> CL test. cpp <Enter>

If you use C ++ Builder, do this:

C:/> BCC32 test. cpp <Enter>

In special cases, you must check the special options. Just execute CL /? Or BCC32 (without any reference), you can see all the compile options.

● Relationship between the compiler and the connector

The early compilation process is separated from the join process. In other words, we must do two actions:

C:/> Cl test. cpp
C:/> LINK test. obj xxx (xxx represents all necessary libraries)

Or:

C:/> BCC32 test. cpp
C:/> TLINK32 test. obj xxx (xxx represents all necessary libraries)

Today's compilation process and linking process are of course separate, but we only need one action:

C:/> CL test. cpp

Or:

C:/> BCC32 test. cpp

This is because the compiler becomes smarter. Unless you specify/c option (indicating that only compilation is not associated), it automatically calls the connector for you. The question about which libraries to use has plagued programmer in the past. The Compiler also has a smart solution: it records the library functions used in the program, the name of the library to which they belong is also recorded, so that the connector can know which libraries to be joined from this table.

● Relationship between environment variables and dos vm (Virtual Machine)

You can enable multiple DOS boxes at the same time, but you cannot execute the above batches in a DOS Box and enjoy the environment settings in another dos vm.

This is because every DOS Box is a Virtual Machine, and no one can see each other.

Unless you have set these environment variables in autoexec. bat. In this case, any newly opened dos vm inherits the original dos vm environment and inherits the variable settings.

● Insufficient environment space

One of the most common problems is the lack of environment space.

After Visual C ++ is installed, a file named VCVARS32.BAT is found in its BIN subdirectory. This file is actually used to set the environment variables (this is described in the last step of the Visual C ++ installation process. Ah, don't read the instructions for installing software for many people !). Therefore, you can execute this file in any DOS Box to replace our own batch file.

However, we usually have experiences of failures"Out of environment space"Error message. This is because VCVARS32.BAT uses the following syntax:

Set INCLUDE = % MSVCDir %/ATL/INCLUDE; % MSVCDir %/MFC/INCLUDE; % INCLUDE %
Set LIB = % MSVCDir %/LIB; % MSVCDir %/MFC/LIB; % LIB %

This means that the original INCLUDE settings (% INCLUDE %) are appended with other settings, and the original LIB settings (% LIB %) are appended with other settings. If the original setting has been very long, so many times, "Out of environment space!

One way is to increase the size of the Environment Space. Please add this line in the c:/config. sys file:

Shell = C:/COMMAND. com c: // E: 1024/P

/E: 1024 indicates that the environment space is adjusted to 1024 bytes. (Not enough? Re-tune)

The second practice is not to use the "additional" pattern of VCVARS32.BAT. Instead, use our own batch file. You know, we may have several compiler environments (VC, BCB, G ++ compiler), and we need to test our program in turn. If we use the "additional" sentence pattern, we will try it several times, even a large environment space will be exhausted.

Both method 1 and method 2 must be implemented.

● Is there any scale limit?

Can I use the console mode (or command line mode) to compile a program? Is there any size limit on the program size? The answer is no!

Its disadvantage is that there is no tool to help you manage your files, there is no preset value for you to play a few words less, there is no analysis tool to help you organize objects, let you browse objects, symbols volumes. So once you have learned the basic skills, it is better to start designing medium and large programs by integrating the environment (IDE.

● Do not misunderstand

I am not reversing the game. I want everyone to go back to the age of Ru Mao drinking blood and be the best man. I think that for a C/C ++ beginner, the use of integrated environment (IDE) may be confusing. It is better to first work in console mode. On the one hand, I have a good understanding of some common environment settings. On the one hand, it is more convenient and easy to use, and I don't have to write a 1000-line small exercise. I have to start the native cannon, on the one hand, the power of knowledge can all be put on Language exercises.

If the integrated environment is used to a certain extent, it will not fall into the fog.

-- The end

Original: http://jjhou.csdn.net/article99-10.htm

 

========================================================== ============================

 

Even drop description:

Take VC 9.0as an example. For example, the directory where cl.exe is located is under D:/Program Files/Microsoft Visual Studio 9.0/VC/bin (the specific directory is determined by your installation location). In cmd, switch to this directory and enter cl. that is:

D:/Program Files/Microsoft Visual Studio 9.0/VC/bin> cl

The following situations may occur, as shown in the figure below (note: VC6.0 may not prompt that mspdb60.dll cannot be found, but VC8.0 and VC9.0 may prompt this error)

 

There are several solutions for this. One is to download mspdb60.dll online and copy it to the C:/WINDOWS/system32 directory.

The second solution is: in the D:/Program Files/Microsoft Visual Studio 9.0/VC/bin directory, there is a batch file VCVARS32.BAT. You can use the dir command to view details, in the Bin directory, you can directly run the input VCVARS32.BAT, for example:

D:/Program Files/Microsoft Visual Studio 9.0/VC/bin> VCVARS32.BAT

Then run the cl command, so that the mspdb60.dll cannot be found. input the cl command will produce the following output:

D:/Program Files/Microsoft Visual Studio 9.0/VC/bin> "D:/Program Files/Microsoft Visual Studio 9.0/Common7/Tools/vsvars32.bat"
Setting environment for using Microsoft Visual Studio 2008x86 tools.

Note: The advantage of using the second method is that the entire command line environment is set when you directly use the batch processing file VCVARS32.BAT provided by the compiler, and the first method is used, you also need to set some environment variables and write them in batches. Otherwise, the following errors may occur, such:

LINK: fatal error LNK1104: cannot open file "LIBC. lib"

Fatal error C1034: iostream: no include path set

Therefore, we need to set environment variables such as PATH, LIB, and INCLUDE.
For details, refer to this Article. However, this setting is temporary. Each time you open the DOS window, you have to run the batch processing again.

Of course, if you have registered the environment variable when installing VC9.0 (VC6.0 and VC9.0 are available, that is, whether to register the environment variable prompt during installation, during installation, I wonder if you have noticed it. there is a window named Setup Environment Variables. In this form, there is a single partition named Register Environment Variables. If you Register it, you do not need to set it any more, you can compile and run the program directly. If you forget to register the program during installation, you can right-click my computer and choose Properties> environment variable to set it, similar to configuring the JDK environment, this is not detailed here. After this setting, it is not a temporary setting. You do not need to set it every time you enable DOS. you can directly compile and run the program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.