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

Unity集成g++编译报错:无法执行cc1plus,找不到文件或目录

解决g++编译报错"cannot execute 'cc1plus'"的问题

问题说明

你在Unity开发编程主题游戏时,集成g++编译器出现以下错误:

Compilation failed: g++.exe: fatal error: cannot execute 'cc1plus': CreateProcess: No such file or directory compilation terminated.
单独在终端执行编译命令也会触发相同报错。

错误原因

g++并非仅依赖g++.exe单个文件,它需要配套的编译前端(如cc1plus.exe)、链接器、标准库头文件及其他组件才能正常工作。仅将g++.exe放入StreamingAssets目录,缺少这些核心依赖,导致编译器无法执行完整编译流程。

解决方案

方案1:部署完整MinGW环境到StreamingAssets

这是最可靠的解决方案,适合游戏发布场景:

  1. 下载完整的MinGW-w64发行包(选择与游戏目标平台匹配的版本,比如Windows 64位游戏选x86_64架构的MinGW)
  2. 将MinGW目录下的bin、include、lib等核心子目录完整复制到Unity的StreamingAssets文件夹,最终目录结构如下:
    StreamingAssets/
    ├─ mingw64/
    │  ├─ bin/
    │  │  ├─ g++.exe
    │  │  ├─ cc1plus.exe
    │  │  └─ ...(其他依赖可执行文件)
    │  ├─ include/
    │  ├─ lib/
    │  └─ ...(其他必要目录)
    
  3. 修改代码中的编译器路径:
    private readonly string compilerPath = $"\"{Application.streamingAssetsPath}/mingw64/bin/g++.exe\" ";
    
  4. 编译进程设置中添加工作目录,确保编译器能找到依赖:
    private void Compile()
    {
        CreateFile();
        var process = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = compilerPath.Trim('"'),
                Arguments = $"-o \"{outputFilePath}\" \"{inputFilePath}\"",
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true,
                // 设置工作目录为MinGW的bin目录
                WorkingDirectory = Path.Combine(Application.streamingAssetsPath, "mingw64", "bin")
            }
        };
        process.Start();
        string output = process.StandardOutput.ReadToEnd();
        string error = process.StandardError.ReadToEnd();
        process.WaitForExit();
    
        if (process.ExitCode == 0)
        {
            UnityEngine.Debug.Log("Compilation succeeded");
        }
        else
        {
            UnityEngine.Debug.LogError("Compilation failed: " + error);
        }
    }
    

方案2:临时添加环境变量(仅适合开发环境)

如果仅在开发阶段测试,可临时将MinGW的bin目录添加到系统环境变量PATH中:

// 在编译前执行
string mingwBinPath = Path.Combine(Application.streamingAssetsPath, "mingw64", "bin");
string currentPath = Environment.GetEnvironmentVariable("PATH");
Environment.SetEnvironmentVariable("PATH", $"{mingwBinPath};{currentPath}");

注意:该方法发布给玩家时兼容性较差,需确保玩家机器有对应依赖,不推荐用于正式发布。

额外注意事项

  • 确保MinGW版本与游戏目标平台架构一致(32位/64位)
  • 打包游戏时,检查Unity打包设置,确保StreamingAssets下的MinGW相关文件被完整打包

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 00:55:35