在命令提示符下运行 C++ - Windows

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11365850/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:05:04  来源:igfitidea点击:

Run C++ in command prompt - Windows

c++windowscommand-line

提问by Bluefire

I know that everyone uses an IDE nowadays, but I just find it simpler to write my code in notepad++, compile it using a command prompt command, and run it from there too. At least that works for Java and Python. I've tried to get my head around how to do that with C++, and haven't been able to find anything good. Is there any compiler (like Java's JDK) that I can stick into my path and use the C++ equivalent of javacand javato run and compile my code from CMD?

我知道现在每个人都使用 IDE,但我发现在 notepad++ 中编写我的代码、使用命令提示符命令编译它并从那里运行它更简单。至少这适用于 Java 和 Python。我试图弄清楚如何用 C++ 做到这一点,但一直找不到任何好的东西。是否有任何编译器(如Java的JDK),我可以坚持到我的路径,并使用C ++相当于javacjava运行,并从CMD编译我的代码?

Note: please don't post answers and comments about how IDEs are better - I know they are. I'm just used to doing it the old way :D

注意:请不要发布关于 IDE 如何更好的答案和评论 - 我知道它们是。我只是习惯于以旧方式做这件事:D

采纳答案by Rango

It depends on what compiler you're using.

这取决于您使用的编译器。

For example, if you are using Visual C++ .NET 2010 Express, run Visual C++ 2010 Express Command Prompt from the start menu, and you can simply compile and run the code.

例如,如果您使用的是 Visual C++ .NET 2010 Express,则从开始菜单运行 Visual C++ 2010 Express 命令提示符,您可以简单地编译和运行代码。

> cl /EHsc mycode.cpp
> mycode.exe

or from the regular command line, you can run vcvars32.batfirst to set up the environment. Alternatively search for setvcvars.cmd(part of a FLOSS project) and use that to even locate the installed VS and have it call vcvars32.batfor you.

或者从常规命令行,您可以vcvars32.bat先运行以设置环境。或者搜索setvcvars.cmd(FLOSS 项目的一部分)并使用它来定位已安装的 VS 并让它vcvars32.bat为您打电话。

Please check your compiler's manual for command lines.

请检查您的编译器手册中的命令行。

回答by codeDEXTER

Steps to perform the task:

执行任务的步骤:

  1. Yes, first install a compiler: Download from here

  2. Then type the C/C++ program, save it.

  3. Then open the command line and change directory, using cdto the particular directory where the source file is stored.

    like: cd C:\Documents and Settings\...

  4. Then to compile/run type in the command prompt,

    gcc sourcefile_name.cor gcc -o outputfile.exe

  1. 是的,首先安装一个编译器: 从这里下载

  2. 然后输入C/C++程序,保存。

  3. 然后打开命令行并更改目录,使用cd存储源文件的特定目录。

    喜欢: cd C:\Documents and Settings\...

  4. 然后在命令提示符中编译/运行类型,

    gcc sourcefile_name.c或者 gcc -o outputfile.exe

回答by JedaiCoder

If you're running Windows then make use of this:

如果您运行的是 Windows,请使用以下命令:

g++ -o program program.cpp

g++is the name of the compiler and -ois the option needed for creating a .o file. Program (without .cpp suffix) is the exefile and program.cppis your source file that you want to compile.

g++是编译器的名称,-o是创建 .o 文件所需的选项。程序(不带 .cpp 后缀)是exe文件,program.cpp是您要编译的源文件。

g++ -o program program.cpp&program.exe

Use this shortcut to run the .exefile of the program. This may run in Ubuntu but you may have to use .outsuffix instead of .exe. Use this handy batch script I made to execute your programs on Windows:

使用此快捷方式运行.exe程序文件。这可能在 Ubuntu 中运行,但您可能必须使用.out后缀而不是.exe. 使用我制作的这个方便的批处理脚本在 Windows 上执行您的程序:

@echo off&&cls
set /p pathName=Enter The Path where the file is located:%=%
cd %pathName%
REM set /p exec=Enter The Name of the executable you want to make:%=%
set /p file=Enter The Name of the file you want to compile:%=%
g++ -o %file% %file%.cpp
%file%.exe

save it as cppExecutor.bat

将其另存为 cppExecutor.bat

Also you could use the following commands on Unix (Linux and Mac) OS:

您也可以在 Unix(Linux 和 Mac)操作系统上使用以下命令:

CC program.cc

If you want to use gcc:

如果你想使用gcc

gcc -o program program.cpp

With the shortcut:

使用快捷方式:

gcc -o program program.cpp&program.exe

回答by 0xC0000022L

I really don't see what your problem is, the question is rather unspecific. Given Notepad++ I assume you use Windows.

我真的不明白你的问题是什么,这个问题相当不具体。鉴于 Notepad++,我假设您使用 Windows。

You have so many options here, from the MinGW (using the GCC tool chain and GNU make) to using a modern MSVC. You can use the WDK(ddkbuild.bat/.cmdor plain build.exe), the Windows SDK(nmake.exe), other tools such as premakeand CMake, or msbuildthat comes with MSVC and the Windows SDK.

您在这里有很多选择,从 MinGW(使用 GCC 工具链和 GNU make)到使用现代 MSVC。您可以使用WDKddkbuild.bat/.cmd或 plain build.exe)、Windows SDK( nmake.exe)、其他工具,例如premakeCMake,或者msbuildMSVC 和 Windows SDK 附带的工具。

I mean the compiler names will differ, cl.exefor MSVC and the WDK and Windows SDK, gcc.exefor MinGW, but even from the console it is customary to organize your project in some way. This is what makeand friends were invented for after all.

我的意思是编译器名称会有所不同,cl.exe对于 MSVC 和 WDK 以及 Windows SDK,gcc.exe对于 MinGW,但即使从控制台,也习惯于以某种方式组织您的项目。这make毕竟是和朋友们发明的。

So to know the command line switches of your particular compiler consult the manual of that very compiler. To find ways to automate your build (i.e. the ability to run a simple command instead of a complex command line), you could sift through the list on Wikipediaor pick one of the tools I mentioned above and go with that.

因此,要了解特定编译器的命令行开关,请查阅该编译器的手册。要找到自动化构建的方法(即运行简单命令而不是复杂命令行的能力),您可以筛选维基百科上列表或选择我上面提到的工具之一并使用它。

Side-note: it isn't necessary to ask people not to mention IDEs. Most professional developers have automated their builds to run from a command line and not from within the IDE (as during the development cycle for example), because there are so many advantages to that approach.

旁注:没有必要要求人们不要提及 IDE。大多数专业开发人员已自动构建从命令行运行,而不是从 IDE 内运行(例如在开发周期中),因为这种方法有很多优点。

回答by KRyan

Sure, it's how most compilers got started. GCCis probably the most popular (comes with most flavors of *nix). Syntax is just gcc my_source_code.cpp, or gcc -o my_executable.exe my_source_code.cpp. It gets more complicated, of course, when you have multiple source files (as in implementation; anything #included works automatically as long as GCC can find it).

当然,这是大多数编译器开始的方式。GCC可能是最受欢迎的(带有大多数 *nix 版本)。语法就是gcc my_source_code.cpp, 或gcc -o my_executable.exe my_source_code.cpp。当然,当您有多个源文件时(如在实现中;#include只要 GCC 可以找到,任何d 都会自动工作),这会变得更加复杂。

MinGWappears to be a version of GCC for Windows, if that's what you're using. I haven't tried it though.

MinGW似乎是适用于 Windows 的 GCC 版本,如果这是您使用的。不过我没试过。

Pretty sure most IDEs also include a command line interface. I know Visual Studio does, though I have never used it.

可以肯定的是,大多数 IDE 还包括一个命令行界面。我知道 Visual Studio 可以,尽管我从未使用过它。

回答by Deepak Singh

  1. Download MinGW form : https://sourceforge.net/projects/mingw-w64/
  2. use notepad++ to write the C++ source code.
  3. using command line change the directory/folder where the source code is saved(using notepad++)
  4. compile: g++ file_name.cpp -o file_name.exe
  5. run the executable: file_name.exe
  1. 下载 MinGW 表格:https: //sourceforge.net/projects/mingw-w64/
  2. 使用 notepad++ 编写 C++ 源代码。
  3. 使用命令行更改保存源代码的目录/文件夹(使用记事本++)
  4. 编译:g++ file_name.cpp -o file_name.exe
  5. 运行可执行文件:file_name.exe

回答by root.e

have MinGW compiler bin directory added to path.

将 MinGW 编译器 bin 目录添加到路径中。

use mingw32-g++ -s -c source_file_name.cpp -o output_file_name.oto compile

用于mingw32-g++ -s -c source_file_name.cpp -o output_file_name.o编译

then mingw32-g++ -o executable_file_name.exe output_file_name.oto build exe

然后mingw32-g++ -o executable_file_name.exe output_file_name.o构建exe

finally, you run with executable_file_name.exe

最后,你跑 executable_file_name.exe

回答by Nikunjkumar rathva

Open cmd and go In Directory where file is saved. Then, For compile, g++ FileName. cpp Or gcc FileName. cpp

打开cmd并进入保存文件的目录。然后,对于编译,g++ FileName。cpp 或 gcc 文件名。cp

For Run, FileName. exe

对于运行,文件名。可执行程序

This Is For Compile & Run Program.

这是用于编译和运行程序。

Make sure, gcc compiler installed in PC or Laptop. And also path variable must be set.

确保 gcc 编译器安装在 PC 或笔记本电脑上。并且还必须设置路径变量。

回答by Abhinav Singh

A better alternative to MinGW is bash for powershell. You can install bash for Windows 10 using the steps given here

MinGW 的一个更好的替代品是 bash for powershell。您可以使用此处给出的步骤为 Windows 10 安装 bash

After you've installed bash, all you've got to do is run the bash command on your terminal.

安装 bash 后,您所要做的就是在终端上运行 bash 命令。

PS F:\cpp> bash
user@HP:/mnt/f/cpp$ g++ program.cpp -o program
user@HP:/mnt/f/cpp$ ./program