How to compile a C + + program with the command line

Source: Internet
Author: User

As a programmer, if you only know how to drag a control on the IDE to write programs, and do not know how to compile the program directly through the compiler. Although there is no big deal, but if mastered the manual compilation of skills, it will certainly be a dazzling technology. From an objective point of view, on the one hand, this skill helps you to be in front of colleagues or classmates in their own programming aspects of the "Ashes level" status. On the other hand, this out-of-the-IDE compilation process can help you get rid of many annoying incidents on the IDE as you brush your questions.

In fact, on-line search, indeed there are many of this article, but many are based on 32-bit Win7 or win8 system, unfortunately, my computer is a 64-bit WIN10 system. After a lot of losses, the code is finally compiled to run through. It's a bitter tear. The following describes how to compile C + + programs with Cmd+notepad in a 64-bit WIN10 system.

1) Set the environment variables: we know that the general C/D program development experience three processes: source code editing--compile and build the. obj file--the link is delivered as an. exe file. So the key role in this process is the compiler, that is, we have to find the C + + compiler. If you already have Visual Studio installed, just look it up in the file. If the appropriate installation package is not installed, I will also describe another method. First say that you have installed vs: Locate the file under the installation directory, such as e:\ the installation directory of various software \vs2015, depending on your own software installation directory. And basically all the VS version, the directory name is almost consistent, so don't worry about the installation of the version path name will be inconsistent issues. If found, you should be able to see:

Find VC This folder, of course you can see vb,vc#, etc., these folders contain the relevant language environment package, no tube, directly find the VC Bin folder, and then the path e:\ various software installation directory \vs2015\vc\bin added to the system environment variables. As for the environment variables, locate my Computer, right-click Properties, and then locate the system variable, environment variable, advanced system settings,->path, create a new one, and then add the path to the line.

The purpose of this is to let the system know where the compiler is, because the environment variable is the default retrieval path when the operating system executes the command. With this environment variable, our computer can find the corresponding file in the specified folder. For example, to find A.exe, simply enter a.exe on the command line without having to write down the absolute path.

If you do not install VS, then you have at least VC, if you say you did not install what to do, that ... Then what kind of programmer are you? Anyway is the default already installed VC case, find the path, such as D:\Program Files\Microsoft Visual Studio\vc98\bin, obviously this is also we need the path of the compiler. In addition to the compiler, the compiler needs to provide the system library file. lib, System header file. The corresponding path has the appropriate LIB and include folders in the VC98 folder. The following is to make environment variable settings, in order to prevent problems, we also need to find the VC98 sibling path under the Common folder Msdev98\bin, look for see if there is no Mspdb60.dll file, if there are words directly to its path, such as D:\Program Files\Microsoft Visual Studio\common\msdev98\bin and our D:\Program Files\Microsoft Visual Studio\vc98\bin path Two is added to the environment variable. We then write a batch file to import the appropriate variables. Write a text like this

INCLUDE = D:\Program Files\Microsoft Visual Studio\vc98\include

LIB = D:\Program Files\Microsoft Visual Studio\vc98\lib

Save As Path.bat, and then run on the line.

With the above setting, we add the path to the compiler in the environment variable, in order to test whether it is successful, you can enter CL in the start->cmd to see if there is a similar window display

Some words will show success.

2) Programming Program debugging

Although the environment variable setting is not the same, the result of the above two cases is roughly the same, so the following is the first case, that is, the configuration of the compiler with VS is tested.

Open the Notepad and write the following code:

#include <iostream>using namespace Std;int main () {cout<< "Hellow World" <<endl;while (1) {}return 0;}

In any one of the disks, such as the F disk is saved as test.cpp. Then open cmd and enter Vcvars32.bat. Then go to the save path of our code, such as F:, then direct this input f:

Shown below:

Then enter CL/EHSC Test.cpp will appear as follows:

Haha, see/out:test.exe have wood there. Open our F-packing folder directly to see if there are no EXE files! Run under:

is not a sense of accomplishment!

There are a few points to note: 1, only 2009 later version, at the command line to compile the first need to execute "vcvars32.bat", the previous version is not required.

2, Visual Studio 2012 before the command-line compilation parameters generally used CL-GX xxx.cpp,2012 after the proposed CL-EHSC Xxx.cpp

How to compile a C + + program with the command line

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.