【问题标题】:MSBuild not compiling using "system" in a console window applicationMSBuild 未在控制台窗口应用程序中使用“系统”进行编译
【发布时间】:2017-11-02 00:43:48
【问题描述】:

我正在尝试在控制台应用程序中使用 system 关键字编译项目以调用 MSBuild。 MSBuild 吐出一个项目未找到错误,我不知道为什么。路径和项目名称都正确并打印出来。 https://gyazo.com/624847f060e242ad702f16174ac75701

我的代码:

bool MSBuild::compile(std::string path, std::string solution) {
if (path.length() == 0 || solution.length() == 0)
    return false;

std::string cmd1 = "cd ";
cmd1.append(path);

std::cout << "Command: " << cmd1 << std::endl;

std::string cmd2 = "msbuild ";
cmd2.append(solution);
cmd2.append(" /p:configuration=debug");

std::cout << "Command: " << cmd2 << std::endl;

system(cmd1.c_str());
system(cmd2.c_str());
return true;
}

我已经确认 MSBuild 在手动输入具有相同参数的相同命令到 cmd 窗口时成功编译项目。 https://gyazo.com/a7f4c3f07f3f44b418734f4a979ca398

【问题讨论】:

    标签: c++ msbuild


    【解决方案1】:

    system() 的每次调用都会产生一个带有自己的环境和工作(当前)目录的命令提示符。

    第二次调用“不知道”第一次调用的操作,因此它将尝试在默认情况下使用的任何工作目录中运行您提供的命令。

    您可以在致电 system(cmd1.c_str()); 后致电 system("cd"); 来证明这一点 - 这很可能不是您期望的值!

    一种可能的解决方法是尝试将您的两个命令与命令提示符用于链接命令的内容连接在一起,&amp;&amp;&amp;,具体取决于您的偏好。

    您的代码的示例改编版本:

    bool MSBuild::compile(std::string path, std::string solution) {
    if (path.length() == 0 || solution.length() == 0)
        return false;
    
    std::string cmd1 = "cd ";
    cmd1.append(path);
    
    std::cout << "Command: " << cmd1 << std::endl;
    
    std::string cmd2 = "msbuild ";
    cmd2.append(solution);
    cmd2.append(" /p:configuration=debug");
    
    std::cout << "Command: " << cmd2 << std::endl;
    
    std::string cmd = cmd1 + std::string(" & ") + cmd2;
    std::cout << "Combined Command: " << cmd << std::endl;
    
    //system(cmd1.c_str());
    //system(cmd2.c_str());
    system(cmd.c_str());
    return true;
    }
    

    您将在此问答中找到一些相关的补充参考资料,包括&amp;&amp;&amp; 之间的区别:What does “&&” in this batch file?

    另一种选择是将您的命令写入单个批处理文件并使用system() 调用它。

    【讨论】:

      猜你喜欢
      • 2020-06-16
      • 1970-01-01
      • 1970-01-01
      • 2019-01-18
      • 2011-04-20
      • 2014-02-17
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      相关资源
      最近更新 更多