You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:Sublime Text 3中无法编译运行简单C++程序

Fixing C++ Compilation & Execution Issues in Sublime Text 3

Hey there, let's work through this problem you're facing with compiling and running C++ code in Sublime Text 3. I've gone over your details, and here's a breakdown of what's happening and how to resolve it:

Your Problem Recap

You switched from Python to C++ for data structures/algorithms, set up Sublime Text 3 with MinGW-w64 (tried both standalone and Code::Blocks' compiler), but your basic input/output C++ code won't run. The error you're getting is:

系统找不到文件 G:\Programming\C++\second.exe. [Finished in 15.0s with exit code 1]
[shell_cmd: g++ "G:\Programming\C++\second.cpp" -o "G:\Programming\C++/second" && "G:\Programming\C++/second"] [dir: G:\Programming\C++] [path: ...]

You also mentioned permission denial errors on restart, which adds another layer to the issue.

Root Causes to Check

  1. Mixed Path Separators: Your shell command uses both backslashes (\) and forward slashes (/) in the output path, which can confuse Windows' file system.
  2. Failed Compilation: The "file not found" error likely means the compiler didn't generate the second.exe file at all—maybe due to incorrect MinGW setup, environment variable issues, or permission blocks.
  3. Permission Restrictions: Your folder or Sublime might not have rights to create/run executable files, especially if the folder is in a protected system directory or blocked by antivirus.

Step-by-Step Solutions

1. Verify MinGW is Properly Configured

First, confirm your compiler is working outside Sublime:

  • Open Command Prompt (cmd) and run g++ --version.
  • If this returns a version number, MinGW is in your PATH correctly. If not:
    • Double-check that the MinGW bin folder path (like C:\Program Files (x86)\mingw-w64\i686-7.1.0-posix-dwarf-rt_v5-rev0\mingw32\bin) is in your system PATH.
    • Restart your computer after updating PATH—environment changes don't take effect in already open programs.

2. Fix the Sublime Build System

Your current build command has path inconsistencies. Replace it with a robust, auto-generating build config:

  • Go to Tools > Build System > New Build System.
  • Paste this JSON code and save it as C++_Run.sublime-build:
{
    "cmd": ["g++", "${file}", "-o", "${file_path}/${file_base_name}.exe"],
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c, source.c++",
    "shell": true,
    "variants": [
        {
            "name": "Run",
            "cmd": ["start", "cmd", "/k", "${file_path}/${file_base_name}.exe"]
        }
    ]
}
  • This config uses Sublime's variables (${file}, ${file_path}) to avoid manual path typos, and the "Run" variant opens a persistent cmd window (so you can input values for cin without the window closing instantly).

3. Resolve Permission Issues

  • Move Your Code Folder: If your code is in a protected directory (like C:\Program Files), move it to a regular folder (e.g., D:\C++_Projects or C:\Users\YourName\Documents\C++).
  • Run Sublime as Admin: Right-click Sublime Text 3 and select "Run as administrator" to bypass folder permission blocks.
  • Disable Antivirus Temporarily: Some antivirus tools block executable file creation. Try disabling it briefly to test if it's causing the issue (remember to re-enable it afterward).

4. Test Manually First

Before using Sublime's build system, compile your code manually in cmd to rule out compiler issues:

  • Navigate to your code folder: cd G:\Programming\C++
  • Run: g++ second.cpp -o second.exe
  • If this command throws errors, fix those first (e.g., missing libraries, incorrect syntax) before going back to Sublime.

Why This Works

Code::Blocks handles compiler paths and execution automatically, but Sublime requires explicit configuration. By fixing the build system, verifying your compiler setup, and addressing permissions, you'll get your C++ code running smoothly.

内容的提问来源于stack exchange,提问作者Ashish Gupta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:00:30