在 Windows 中编写和编译 C/C++ 程序的最快方法

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

Fastest way to write & compile a C/C++ program in Windows

c++cideinteractive

提问by Felix Dombek

I'm usually using Visual Studio, but several things bother me when I just quickly want to test some code:

我通常使用 Visual Studio,但是当我只想快速测试一些代码时,有几件事困扰着我:

  • it has a rather long startup time
  • it always needs a project to execute/debug files
  • program output gets printed to the console, but the window simply closes when I don't insert a getchar()or a breakpoint in the program and thus I'm not seeing it.
  • 它有一个相当长的启动时间
  • 它总是需要一个项目来执行/调试文件
  • 程序输出被打印到控制台,但是当我没有getchar()在程序中插入 a或断点时窗口就会关闭,因此我没有看到它。

I'm looking for a program which is suitable for a really, really quick programming in Windows. Such as, copying some code from an SO question, running it and seeing its output.

我正在寻找适合在 Windows 中进行非常非常快速的编程的程序。例如,从 SO 问题中复制一些代码,运行它并查看其输出。

I don't think that console programs or g++ under CygWin are a good solution, because there it takes ages to cdinto the right dir to save the file, I'm not used to editors such as Vim, and typing in the compiler commandline myself has always annoyed me etc.

我不认为 CygWin 下的控制台程序或 g++ 是一个好的解决方案,因为cd在正确的目录中保存文件需要很长时间,我不习惯像 Vim 这样的编辑器,并且我自己在编译器命令行中输入总是惹恼我等。

So I guess what I'm looking for is a very lightweight free C/C++ IDE which is preconfigured to work with a free compiler (bonus points if it is even shipped with it.)

所以我想我正在寻找的是一个非常轻量级的免费 C/C++ IDE,它被预先配置为与一个免费的编译器一起工作(如果它甚至附带有奖励积分。)

What can you recommend which adresses at least two items from the list above?

您可以推荐哪些地址至少包含上面列表中的两个项目?

Is there maybe even a program which can execute/interpret C or C++ in an interactive commandline (like Python)?

甚至可能有一个程序可以在交互式命令行(如 Python)中执行/解释 C 或 C++?

采纳答案by Andrew Durward

I'm looking for a program which is suitable for a really, really quick programming in Windows. Such as, copying some code from an SO question and executing it and seeing it's output.

我正在寻找适合在 Windows 中进行非常非常快速的编程的程序。例如,从 SO 问题中复制一些代码并执行它并查看它的输出。

For quick-and-dirty experimental coding, I really like codepad.org. Not having to create a file is especially nice as it saves me from coming up with a suitable name and disk location. Be aware that it uses g++ 4.1.2 behind the scenesso some of the latest C++11 features aren't supported.

对于快速而肮脏的实验编码,我真的很喜欢codepad.org。不必创建文件特别好,因为它使我不必想出合适的名称和磁盘位置。请注意,它在后台使用 g++ 4.1.2 因此不支持某些最新的 C++11 功能。

回答by Keith

"really, really quick (and dirty, throw away?) programming "?

“真的,真的很快(而且很脏,扔掉?)编程”?

  • Compiler : VC++ command line - you already have it.
  • Editor: Notepad or somesuch
  • Compilation process: A .BAT file you write once and supply a parameter with the name of the single source file.
  • Location: Set up some desktop shortcuts to a known directory for your test code.
  • 编译器:VC++ 命令行 - 你已经拥有了。
  • 编辑器:记事本之类的
  • 编译过程:您编写一次的 .BAT 文件并提供带有单个源文件名称的参数。
  • 位置:为您的测试代码设置一些桌面快捷方式到已知目录。

回答by pmg

Use TCC : Tiny C Compiler

使用TCC:微型 C 编译器

  1. start a command prompt
  2. cd wherever
  3. notepad main.c
  4. write code in notepad. save
  5. back in the command prompttype tcc -run main.c
  6. notice errors, go back to 4
  1. 启动命令提示符
  2. cd 任何地方
  3. 记事本main.c
  4. 用记事本写代码。节省
  5. 回到命令提示符类型tcc -run main.c
  6. 注意错误,返回 4

Note that with -runparameter you're invoking tcclike an interpreter

请注意,使用-run参数您tcc像解释器一样调用

回答by Konrad Rudolph

Which compiler you use doesn't really matter. I prefer G++ but cl.exe(from Visual Studio) works equally well.

您使用哪种编译器并不重要。我更喜欢 G++,但cl.exe(来自 Visual Studio)同样有效。

In order to use the compiler quickly from the command line, either

为了从命令行快速使用编译器,要么

  1. include it into your PATH variable by setting it in the system settings, or
  2. create a simple .cmdscript which launches a console with the right paths included.
  1. 通过在系统设置中进行设置将其包含在您的 PATH 变量,或者
  2. 创建一个简单的.cmd脚本,它启动一个包含正确路径的控制台。

Visual Studio incidentally comes bundled with such a .cmdscript which is linked in the Start Menu entry of Visual Studio. Personally, though, I prefer adjusting the PATH variable.

Visual Studio 附带有这样一个.cmd脚本,该脚本链接在 Visual Studio 的“开始”菜单条目中。不过,就个人而言,我更喜欢调整 PATH 变量。

Then you can simply invoke the compiler from any directory in the command line. If you are too lazy to write the whole command line, create a script to do it for you. Or use Cygwin and (C)Make.

然后您可以简单地从命令行中的任何目录调用编译器。如果您懒得编写整个命令行,请创建一个脚本来为您完成。或者使用 Cygwin 和 (C)Make。

Two additional remarks:

补充两点:

  1. Starting the project using the build configuration (Cntr+F5 (?)) leaves the console open after the program has run, without you having to include getch()calls or similar.
  2. I highly recommend you learn an editor such as Emacs or Vim, unless you plan never to use any other platform than Windows, and even then. These editors are just tremendously powerful, and in some ways light-years beyond what the Visual Studio code editor offers.

    But if you really don't have the time, use a decent text editor such as Notepad++instead.

  1. 使用构建配置 (Cntr+F5 (?)) 启动项目会在程序运行后使控制台保持打开状态,而无需包含getch()调用或类似操作。
  2. 我强烈建议您学习 Emacs 或 Vim 之类的编辑器,除非您计划永远不使用 Windows 以外的任何其他平台,即使如此。这些编辑器非常强大,在某些方面比 Visual Studio 代码编辑器提供的功能要好几光年。

    但是,如果您真的没有时间,请改用像Notepad++这样不错的文本编辑器。

回答by Alexey Frunze

Open Watcomis easy to install and use, it's fast and it's the closest compiler to MSVC++, although it's noticeably behind in features (especially in C++).

Open Watcom易于安装和使用,速度很快,并且是最接近 MSVC++ 的编译器,尽管它在功能上明显落后(尤其是在 C++ 中)。

I don't use its IDE at all as I got used to doing most of the stuff in the console, but it's there and the debugger is there too.

我根本不使用它的 IDE,因为我习惯于在控制台中做大部分事情,但它在那里,调试器也在那里。

Compiling one-filers is easy.

编译 one-filer 很容易。

Compiling C code:

编译C代码:

wcl386.exe /we /wx /q sourcefile.c

Compiling C++ code:

编译C++代码:

wcl386.exe /xs /we /wx /q sourcefile.cpp

回答by Mooing Duck

On my machine, I have a "empty" project called "Test". When I want to test some random code on the internet, I simply put it into main.cppin that project, and compile.

在我的机器上,我有一个名为“Test”的“空”项目。当我想在互联网上测试一些随机代码时,我只需将其放入main.cpp该项目中,然后编译即可。

If you think MSVC takes too long to load, it should be possible to write a batch script that attempts to compile the project and puts the build log in a file. Then you can simply alter the existing main.cppwith notepad, double click the batch file, then pop open the build log or run the executable.

如果您认为 MSVC 加载时间太长,应该可以编写一个批处理脚本来尝试编译项目并将构建日志放入文件中。然后您可以简单地main.cpp使用记事本更改现有文件,双击批处理文件,然后弹出构建日志或运行可执行文件。

[Edit] I made a batch file to compile the entire solution. Turns out that requires loading visual studio. However, the batch file can compile/run a single cpp file easy enough.

[编辑] 我制作了一个批处理文件来编译整个解决方案。事实证明,这需要加载 Visual Studio。但是,批处理文件可以很容易地编译/运行单个 cpp 文件。

回答by TheBuzzSaw

My favorite IDE: http://www.codeblocks.org/

我最喜欢的 IDE:http: //www.codeblocks.org/

Here is a direct link to the download that includes the MinGW compiler: http://download2.berlios.de/codeblocks/codeblocks-10.05mingw-setup.exe

这是包含 MinGW 编译器的下载的直接链接:http: //download2.berlios.de/codeblocks/codeblocks-10.05mingw-setup.exe

You're not gonna find any (good) C/C++ interpreters.

你不会找到任何(好的)C/C++ 解释器。

回答by Matteo Italia

Once I used PSPad setting its "compiler" option for C++ files to a reasonable default (cl.exein the correct directory, speed optimization, all warnings). Then it's just Ctrl+F9.

一旦我使用 PSPad 将 C++ 文件的“编译器”选项设置为合理的默认值(cl.exe在正确的目录中,速度优化,所有警告)。然后它只是Ctrl + F9。

回答by drdwilcox

All of the above compiler recommendations are good. For an editor, I really like Notepad2

以上所有编译器建议都很好。作为一个编辑器,我真的很喜欢Notepad2

I know you didn't ask...

我知道你没问...

回答by seattlecpp

First off, your expectations are not reasonable. no program can guess what you want, over a range of input from the simplest to the most complex. If cd'ing into cygwin is too hard, and starting up visual studio is too time-consuming, you're pretty much toast. Sorry.

首先,你的期望是不合理的。在从最简单到最复杂的输入范围内,没有程序可以猜测您想要什么。如果进入 cygwin 太难了,而且启动 Visual Studio 太费时,那你就差强人意了。对不起。

That said, you can edit code with notepad (which you can invoke from the command line as notepad foo.cpp). Notepad uses your mouse and the arrow keys on your pc so it's pretty intuitive.

也就是说,您可以使用记事本编辑代码(您可以从命令行调用记事本 foo.cpp)。记事本使用您的鼠标和 PC 上的箭头键,因此非常直观。

you can use visual studio tools from the command line, without having to fiddle with project files.

您可以从命令行使用 Visual Studio 工具,而无需摆弄项目文件。

Visual studio comes with a tool called nmake, the most basic usage of which is similar to linux make. If you have very simple input, nmake's default rules may be good enough to produce an executable. If not, you may be able to construct a makefile that will take any single simple file, say foo.cpp, and compile and link it to an executable called foo.exe. You'll still have to learn to use nmake, which some people think is easy, and others think is fiendishly difficult. Try nmake foo.cpp and see if the result is what you want.

Visual Studio自带一个工具叫nmake,最基本的用法和linux make类似。如果您的输入非常简单,nmake 的默认规则可能足以生成可执行文件。如果没有,您可以构建一个 makefile,该文件将采用任何单个简单文件,例如 foo.cpp,然后编译并将其链接到名为 foo.exe 的可执行文件。您仍然必须学习使用 nmake,有些人认为这很容易,而另一些人则认为非常困难。尝试 nmake foo.cpp 并查看结果是否是您想要的。