【问题标题】:Can't build C++ program using Sublime Text 2, Windows 8无法使用 Sublime Text 2、Windows 8 构建 C++ 程序
【发布时间】:2012-10-22 09:52:24
【问题描述】:

我想你们中的许多人正在使用或曾经在 Windows 8 上使用 Sublime Text 2 编辑器。我有一个奇怪的错误:无法构建 C++ 程序。我的目标是使用 Sublime Text 2 构建和运行简单的程序,这样它就可以在下面的窗口中显示输出,Xcode 或 Codeblocks 样式。

我的 C++.sublime-build(这是默认设置):

{
    "cmd": ["g++", "${file}", "-o", "${file_path}/${file_base_name}"],
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.c++",

    "variants":
    [
        {
            "name": "Run",
            "cmd": ["bash", "-c", "g++ '${file}' -o '${file_path}/${file_base_name}' && '${file_path}/${file_base_name}'"]
        }
    ]
}

当我编译一个简单的 hello world 程序时,它编译并运行良好,并且输出显示在 sublime 文本的下部面板上,就像我想要的那样。

但如果我运行如下更复杂的程序:

//  main.cpp
//  LongestIncreasingSubsequence
#include <iostream>
#include <vector>
#include <limits>
using namespace std;

int main(int argc, const char * argv[])
{


    int n;

    cin >> n;
    vector<int> nums(n);
    vector<int> lis_so_far(n);
    int final_longest = 1;

    for (int i=0; i<n; i++) {
        cin >> nums[i];
        lis_so_far[i] = 1;
    }
    int so_far;
    for (int j=n-1; j>=0; j--) {

        so_far = 0;

        for (int i=j+1; i<n; i++) {
         //   cout << "hi" <<endl;
            if (nums[i] > nums[j]) {
              //  cout << "hello" <<endl;

                if (lis_so_far[i] > so_far ) {
                    so_far = lis_so_far[i];
                    //cout << so_far << endl;
                }

            }
        }
        if (j<n-1) {
            lis_so_far[j] += so_far;
            if (lis_so_far[j] > final_longest) {
                final_longest = lis_so_far[j];
            }
        }
    }

    for (int i=0; i<n; i++) {
        cout << lis_so_far[i] << endl;
    }
    cout << final_longest << endl;


    return 0;
}

,即使我安装了 cygwin,它也会给我一个缺少的 limits.h 错误。 ST2不应该知道c++库在哪里吗?:

C:\Users\Leonardo\Desktop\main5.cpp:11: limits: No such file or directory
[Finished in 6.5s with exit code 1]

如果我注释 #include 限制行,它会构建,但是当我运行它时,它会给我一个权限被拒绝错误,即使没有运行命令提示符:

/cygnus/cygwin-b20/H-i586-cygwin32/i586-cygwin32/bin/ld: cannot open output file C:\Users\Leonardo\Desktop/main5.exe: Permission denied
collect2: ld returned 1 exit status

有什么想法吗?我只是希望 ST2 能够像 Xcode 或 Codeblocks 那样运行。

【问题讨论】:

    标签: compiler-construction build compilation build-process sublimetext2


    【解决方案1】:

    这是一个崇高的控制台“错误”。这是因为您永远不会终止最后一次编译。例如。

    #include <iostream>
    int main(){
    cout << "it's a program" << endl;
    cin.get() // system pause
    }
    

    当你第一次运行时,你编译成功,但是,当你调用 cin.get() 暂停时,程序永远不会终止,并保持打开状态。

    然后,如果您再次尝试编译,您会看到权限错误,因为程序仍在运行。删除 pause 命令,您就不会出错了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多