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

如何在P4Python脚本中调用C++程序?报错排查求助

需求与问题

我需要编写Python脚本自动完成Perforce中文件的修改与提交,具体流程为:

  • 检出只读状态的目标文件
  • 调用UpdateContent.cpp程序修改文件内容
  • 提交变更到Perforce

文件存放情况:

  • UpdateResource.py和UpdateContent.cpp位于N:/ResourceTools/目录
  • 目标修改文件Version.h在其他独立目录

运行脚本时出现错误,错误触发点是Python脚本中使用subprocess调用g++编译UpdateContent.cpp的代码行。目前怀疑问题出在UpdateContent.cpp中ResourceInfoFileName的路径设置上。


相关代码

UpdateResource.py(Python脚本)

import subprocess
import os

# Perforce命令路径
P4_EXE = "p4.exe"
# 目标文件路径(示例,实际为其他目录的Version.h)
TARGET_FILE = "D:/Project/Version.h"
# 工具文件路径
CPP_SCRIPT = "N:/ResourceTools/UpdateContent.cpp"
CPP_EXE = "N:/ResourceTools/UpdateContent.exe"

def main():
    # 1. 检出Perforce文件
    try:
        subprocess.run([P4_EXE, "edit", TARGET_FILE], check=True, capture_output=True, text=True)
    except subprocess.CalledProcessError as e:
        print(f"检出文件失败: {e.stderr}")
        return
    
    # 2. 编译C++修改工具(此处出错)
    try:
        subprocess.run(["g++", CPP_SCRIPT, "-o", CPP_EXE], check=True, capture_output=True, text=True)
    except subprocess.CalledProcessError as e:
        print(f"编译C++程序失败: {e.stderr}")
        return
    
    # 3. 运行修改工具
    try:
        subprocess.run([CPP_EXE, TARGET_FILE], check=True, capture_output=True, text=True)
    except subprocess.CalledProcessError as e:
        print(f"修改文件失败: {e.stderr}")
        return
    
    # 4. 提交变更
    try:
        subprocess.run([P4_EXE, "submit", "-d", "Auto-update Version.h content"], check=True, capture_output=True, text=True)
    except subprocess.CalledProcessError as e:
        print(f"提交变更失败: {e.stderr}")
        return

if __name__ == "__main__":
    main()

UpdateContent.cpp(C++修改程序)

#include <iostream>
#include <fstream>
#include <string>

// 怀疑此处路径设置有误
std::string ResourceInfoFileName = "ResourceInfo.txt"; // 若为相对路径,可能无法找到文件

bool UpdateVersionFile(const std::string& filePath) {
    std::fstream file(filePath, std::ios::in | std::ios::out);
    if (!file.is_open()) {
        std::cerr << "无法打开目标文件: " << filePath << std::endl;
        return false;
    }

    // 读取并修改内容示例
    std::string content;
    std::getline(file, content, '\0');
    size_t versionPos = content.find("#define VERSION");
    if (versionPos != std::string::npos) {
        content.replace(versionPos, 15, "#define VERSION \"1.0.1\"");
        file.seekp(0);
        file << content;
        file.truncate(content.size());
    }

    file.close();
    return true;
}

int main(int argc, char* argv[]) {
    if (argc != 2) {
        std::cerr << "使用方法: UpdateContent.exe <目标文件路径>" << std::endl;
        return 1;
    }

    // 若ResourceInfoFileName是相对路径,程序运行时的工作目录可能不是预期路径
    std::ifstream infoFile(ResourceInfoFileName);
    if (!infoFile.is_open()) {
        std::cerr << "无法读取资源信息文件: " << ResourceInfoFileName << std::endl;
        return 1;
    }

    if (!UpdateVersionFile(argv[1])) {
        return 1;
    }

    return 0;
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 12:35:24