Windows 7 命令行编译 C++ 代码(使用 Visual Studio)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3283988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows 7 command line to compile C++ code (with Visual Studio)
提问by dato datuashvili
I know this is similar to the question How do I run my code from the command line?, but I'll be a little more specific.
我知道这类似于问题 如何从命令行运行我的代码?,但我会更具体一点。
My project - project3.txt
- is located on my Desktop. Below is the code I need to run:
我的项目 - project3.txt
- 位于我的桌面上。下面是我需要运行的代码:
int a=atoi(arg[1]);
int b=atoi(arg[2]);
How do I compile it from the command line?
如何从命令行编译它?
回答by Jess
Start a VS2010 Command prompt:
启动 VS2010 命令提示符:
Start->All Programs->Microsoft Visual Studio 2010->Visual Studio Tools->Visual Studio Command Prompt (2010)
开始->所有程序->Microsoft Visual Studio 2010->Visual Studio 工具->Visual Studio 命令提示符(2010)
Switch to your directory:
切换到您的目录:
cd c:\Users\(your username)\Desktop
or
或者
cd c:\Users\davit\Desktop
Compile the code:
编译代码:
cl (yourFileName).cpp
or
或者
cl project3.cpp
Run the code:
运行代码:
(yourfilename).exe (arg1) (arg2)
or
或者
project3.exe 7 8
回答by David Pratte
To run the Visual studio command prompt from the start menu:
从开始菜单运行 Visual Studio 命令提示符:
Then the two options you probably want to use are either cl(labeled 2), to build a single C/C++ file, or msbuild(labeled 3), to build the whole project file you created with visual studio. You'll also probably want to change the initial directory, as shown in 1.
那么您可能想要使用的两个选项是cl(标记为 2),用于构建单个 C/C++ 文件,或msbuild(标记为 3),用于构建您使用 Visual Studio 创建的整个项目文件。您可能还想更改初始目录,如 1 所示。
Please note that these images were taken using a localized version of Windows. You are looking for an option named Command Prompt. It is also using a project name of testApp, so if you're not using that, or you're not using the default file hierarchy that Visual Studio creates, you'll have to make sure the file paths are correct.
请注意,这些图像是使用本地化版本的 Windows 拍摄的。您正在寻找一个名为Command Prompt的选项。它还使用testApp的项目名称,因此如果您没有使用它,或者您没有使用 Visual Studio 创建的默认文件层次结构,则必须确保文件路径正确。
回答by James Curran
Now, may I assume that you are also using VisualStudio ?
现在,我可以假设您也在使用 VisualStudio 吗?
The C++ compiler is buried deep in your Program Files folder. (somewhere like C:\Program Files\Microsoft Visual Studio 9.0\VC\bin
but that will vary with the version). You need that folder on your DOS PATH environment variable, plus the paths to the libraries & header files. These are set up by running the VCVARS32.BAT file (in C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools
)
C++ 编译器深埋在您的 Program Files 文件夹中。(在某个地方,C:\Program Files\Microsoft Visual Studio 9.0\VC\bin
但会因版本而异)。您需要 DOS PATH 环境变量中的该文件夹,以及库和头文件的路径。这些是通过运行 VCVARS32.BAT 文件(在C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools
)中设置的
Then the basic form is :
那么基本形式是:
cl myfile.cpp
回答by Andrew Flanagan
Assuming Visual C++: There's information on running Microsoft's compiler from the command-line here.
假设VISUAL C ++:有上运行的命令行微软的编译器的信息在这里。
If you're simply trying to runthe application, you just type in the name of the executable to execute it at the command line.
如果您只是想运行该应用程序,只需在命令行中输入可执行文件的名称即可执行它。
Might I suggest if you're doing such simple scripts that you use an interpreted language such as Rubyor Python? It's really much easier unless you're constrained to use C++.
如果您正在编写使用解释性语言(例如Ruby或Python)的简单脚本,我是否可以建议您?除非您被限制使用 C++,否则它真的要容易得多。
回答by cpx
From the VS Studio CMD Prompt
从 VS Studio CMD 提示
cl.exe source.cpp
See more on Compiler Command-Line Usage
回答by bta
If you are writing C++ code, do not save your source files with a .txt extension. Use .c for C-language source and .cpp for C++. Some compilers can have problems if you use the wrong file extension.
如果您正在编写 C++ 代码,请不要使用 .txt 扩展名保存源文件。将 .c 用于 C 语言源代码,将 .cpp 用于 C++。如果您使用错误的文件扩展名,某些编译器可能会出现问题。
Since you mentioned Windows, I will assume you are using Visual Studio. If this is the case here are a few links that might help you create a new project and compile your code. Some of these reference older versions of VS, but the general concepts still apply.
既然您提到了 Windows,我将假设您使用的是 Visual Studio。如果是这种情况,这里有一些链接可以帮助您创建新项目并编译代码。其中一些参考了旧版本的 VS,但一般概念仍然适用。
http://pdfcast.org/pdf/how-to-create-a-c-program-using-visual-studio
http://pdfcast.org/pdf/how-to-create-ac-program-using-visual-studio
http://www.functionx.com/cpp/references/projectcreation.htm(VS instructions are towards the bottom)
http://www.functionx.com/cpp/references/projectcreation.htm(VS说明在底部)
http://web.sau.edu/lilliskevinm/C%2B%2BTutorial/dotNet/
http://web.sau.edu/lilliskevinm/C%2B%2BTutorial/dotNet/
If you are not using Visual Studio, please edit your question and include the name and version of your compiler.
如果您没有使用 Visual Studio,请编辑您的问题并包括您的编译器的名称和版本。