Use C/C ++ compiler in console mode-hou Jie

Source: Internet
Author: User

★★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,To enable us to obtain the compiler in any working directoryOf course, 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.
● 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
If you use C ++ Builder, do this:
C:/> BCC32 test. cpp
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 clever solution: it records the libraryfunctions 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 (VirtualMachine)
You can enable multiple DOS boxes at the same time, but you cannot execute the above batches in a DOSBox 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. Therefore, any newly opened dos vm inherits the original DOSVM environment and those 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:
SetINCLUDE = % 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.

Related Article

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.