如何在 Sublime Text 2、Windows 8 中构建和运行 C++ 程序?

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

How to build and run c++ programs in Sublime Text 2, Windows 8?

c++compiler-constructionbuild-processsublimetext2

提问by Leonardo Lopez

I installed Codeblocks with mingw, chose default compiler, and could build and run a simple hello program without errors.

我用 mingw 安装了 Codeblocks,选择了默认编译器,并且可以构建和运行一个简单的 hello 程序而不会出错。

I installed Sublime Text 2, copy pasted the same hello world program:

我安装了 Sublime Text 2,复制粘贴了相同的 hello world 程序:

// my first program in C++
#include <iostream>

using namespace std;

int main ()
{
    cout << "Hello World!";
    return 0;
}

Upon building, I get the error message:

构建时,我收到错误消息:

[Error 2] The system cannot find the file specified
[cmd:  [u'bash', u'-c', u"g++ '' -o '/' && '/'"]]
[dir:  C:\Windows\system32]
[path: C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\]
[Finished]

What do I need to do in order to build and run a simple program using Sublime Text 2?

为了使用 Sublime Text 2 构建和运行一个简单的程序,我需要做什么?

采纳答案by Leonardo Lopez

First, make sure you save the fileyou're working on, wherever on your drive, before building and running.

首先,在构建和运行之前,请确保正在处理的文件保存在驱动器上的任何位置。

Sublime Text 2 needs g++, bash, etc in order to compile. These packages need to be installed on your computer, as per the instructions on this page:

Sublime Text 2 需要 g++、bash 等才能编译。这些软件包需要按照此页面上的说明安装在您的计算机上:

http://mjiang.com/mec/cs244/files/Installing%20c++_g++_on_Windows.pdf

http://mjiang.com/mec/cs244/files/Installing%20c++_g++_on_Windows.pdf

回答by Biswajit Paul

For WINDOWS:

对于 WINDOWS:

If you have Dev C++ (Bloodshed) then,

如果你有 Dev C++ (Bloodshed) 那么,

OPEN SUBLIME TEXT 2and creat a new file to write your code (change build system to c++ through Tools> Build System> C++as SublimeText2doesn't come with build-system for c)

OPEN SUBLIME TEXT 2并创建一个新文件来编写您的代码(Tools> Build System> C++由于SublimeText2未附带,因此将构建系统更改为 c++ build-system for c

After that, you save that file to binfolder contained in Dev-Cppfolder and press ctrl+b

之后,将该文件保存到文件bin夹中包含的Dev-Cpp文件夹中,然后按ctrl+b

If your code is correct (bug free) then you'll found a corresponding file (in .exeformat) on same directory which will show you

如果您的代码正确(无错误),那么您将.exe在同一目录中找到相应的文件(格式),该文件将显示

Hello World!

你好,世界!

REMEMBER: SUBLIME TEXT 2 is an Editor, not a COMPILER

记住:SUBLIME TEXT 2 是一个编辑器,而不是一个编译器

回答by Minh-Triet Pham Tran

You could use my working C++.sublime-build file for Windows: https://gist.github.com/trietptm/4950038

您可以将我的工作 C++.sublime-build 文件用于 Windows:https: //gist.github.com/trietptm/4950038

回答by Aranganathan

just create new Build-system (TOOLS->BUILD SYSTEM->NEW BUILD SYSTEM)

只需创建新的构建系统(工具->构建系统->新构建系统)

{
"windows":
{
"cmd": ["g++", "$file_name","-o", "${file_base_name}.exe", "-lm", "-Wall", "&","start", "${file_base_name}.exe"]
},
"selector": "source.c++",
"shell": true,
"working_dir": "${file_path}"
}

and save it as (name_you_can_provide).sublime-build and use that build system. :)

并将其保存为 (name_you_can_provide).sublime-build 并使用该构建系统。:)

回答by pharask

(I assume you already have installed MingW in your computer.)

(我假设您已经在您的计算机中安装了 MingW。)

You need to go to Preferences->Browse Packages->C++ folder->C++.sublime-build;bring this C++.sublime buildfile into the sublime text editor and now paste this code :

你需要去 Preferences->Browse Packages->C++ folder->C++.sublime-build; 将此C++.sublime 构建文件带入 sublime 文本编辑器,然后粘贴以下代码:

{ "cmd": ["g++", "$file", "-o", "$file_base_name"], "selector": "source.c++", "working_dir": "$file_path", "variants": [ { "name": "Run", "cmd": ["g++", "$file", "-o", "$file_base_name", "&&", "$file_path/$file_base_name"], "shell": true } ]
}

Hope this helps you.

希望这对你有帮助。

回答by pharask

You must install MinGW, then add path to MinGW to PATH variable.

您必须安装 MinGW,然后将 MinGW 的路径添加到 PATH 变量。