简体   繁体   English

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

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

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()或断点时窗口就会关闭,因此我没有看到它。

I'm looking for a program which is suitable for a really, really quick programming in Windows.我正在寻找适合在 Windows 中进行非常非常快速的编程的程序。 Such as, copying some code from an SO question, running it and seeing its output.例如,从 SO 问题中复制一些代码,运行它并查看其输出。

I don't think that console programs or g++ under CygWin are a good solution, because there it takes ages to cd into 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++?

I'm looking for a program which is suitable for a really, really quick programming in Windows.我正在寻找适合在 Windows 中进行非常非常快速的编程的程序。 Such as, copying some code from an SO question and executing it and seeing it's output.例如,从 SO 问题中复制一些代码并执行它并查看它的输出。

For quick-and-dirty experimental coding, I really like codepad.org .对于快速而肮脏的实验编码,我真的很喜欢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 scenes so some of the latest C++11 features aren't supported.请注意,它在后台使用 g++ 4.1.2 因此不支持某些最新的 C++11 功能。

"really, really quick (and dirty, throw away?) programming "? “真的,真的很快(而且很脏,扔掉?)编程”?

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

Use TCC : Tiny C Compiler使用TCC:微型 C 编译器

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

Note that with -run parameter you're invoking tcc like an interpreter请注意,使用-run参数,您可以像解释器一样调用tcc

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通过在系统设置中进行设置将其包含在您的 PATH 变量,或者
  2. create a simple .cmd script which launches a console with the right paths included.创建一个简单的.cmd脚本,它启动一个包含正确路径的控制台。

Visual Studio incidentally comes bundled with such a .cmd script which is linked in the Start Menu entry of Visual Studio. Visual Studio 附带了这样一个.cmd脚本,该脚本链接在 Visual Studio 的开始菜单条目中。 Personally, though, I prefer adjusting the PATH variable.不过,就个人而言,我更喜欢调整 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.使用构建配置 (Cntr+F5 (?)) 启动项目,程序运行后控制台会保持打开状态,而无需包含getch()调用或类似调用。

  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.我强烈建议您学习 Emacs 或 Vim 之类的编辑器,除非您计划永远不使用 Windows 以外的任何其他平台,即使如此。 These editors are just tremendously powerful, and in some ways light-years beyond what the Visual Studio code editor offers.这些编辑器非常强大,在某些方面比 Visual Studio 代码编辑器提供的功能要好几光年。

    But if you really don't have the time, use a decent text editor such as Notepad++ instead.但是,如果您真的没有时间,请改用像Notepad++这样不错的文本编辑器。

Open Watcom is 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

On my machine, I have a "empty" project called "Test".在我的机器上,我有一个名为“Test”的“空”项目。 When I want to test some random code on the internet, I simply put it into main.cpp in that project, and compile.当我想在网上测试一些随机代码时,我只是把它放到那个项目的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.如果您认为 MSVC 加载时间过长,应该可以编写一个批处理脚本来尝试编译项目并将构建日志放入文件中。 Then you can simply alter the existing main.cpp with notepad, double click the batch file, then pop open the build log or run the executable.然后你可以简单地用记事本更改现有的main.cpp ,双击批处理文件,然后弹出构建日志或运行可执行文件。

[Edit] I made a batch file to compile the entire solution. [编辑] 我制作了一个批处理文件来编译整个解决方案。 Turns out that requires loading visual studio.事实证明,这需要加载 Visual Studio。 However, the batch file can compile/run a single cpp file easy enough.但是,批处理文件可以很容易地编译/运行单个 cpp 文件。

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++ 解释器。

Once I used PSPad setting its "compiler" option for C++ files to a reasonable default ( cl.exe in the correct directory, speed optimization, all warnings).一旦我使用 PSPad 将 C++ 文件的“编译器”选项设置为合理的默认值(正确目录中的cl.exe ,速度优化,所有警告)。 Then it's just Ctrl+F9.然后它只是Ctrl + F9。

All of the above compiler recommendations are good.以上所有编译器建议都很好。 For an editor, I really like Notepad2作为一个编辑器,我真的很喜欢Notepad2

I know you didn't ask...我知道你没问...

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.如果进入 cygwin 太难了,而且启动 Visual Studio 太费时,那你就差强人意了。 Sorry.对不起。

That said, you can edit code with notepad (which you can invoke from the command line as notepad foo.cpp).也就是说,您可以使用记事本编辑代码(您可以从命令行调用记事本 foo.cpp)。 Notepad uses your mouse and the arrow keys on your pc so it's pretty intuitive.记事本使用您的鼠标和 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. Visual Studio自带一个工具叫nmake,最基本的用法和linux make类似。 If you have very simple input, nmake's default rules may be good enough to produce an executable.如果您的输入非常简单,nmake 的默认规则可能足以生成可执行文件。 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.如果没有,您可以构建一个 makefile,该文件将接受任何单个简单文件,例如 foo.cpp,然后编译并将其链接到名为 foo.exe 的可执行文件。 You'll still have to learn to use nmake, which some people think is easy, and others think is fiendishly difficult.您仍然必须学习使用 nmake,有些人认为这很容易,而另一些人则认为非常困难。 Try nmake foo.cpp and see if the result is what you want.尝试 nmake foo.cpp 并查看结果是否是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM