VS2019 popen 不使用环境变量

问题描述

我试图在我的 VS 项目中执行某种 eval 命令,但遇到了错误。这是我的代码

#include <iostream>
#include <stdexcept>
#include <stdio.h>
#include <string>
#include <fstream>
using string = std::string;

string eval(string cmd) {
    std::ofstream ree;
    ree.open("WRITEFILE.cpp",std::ofstream::out | std::ofstream::trunc);
    ree << cmd;
    ree.close();
    char buffer[128];
   string result = "";
   const char* cmdstr = "g++ WRITEFILE.cpp -o WRITE.exe && WRITE.exe"; // + libraries once I get it working
   std::cout << cmdstr;
   FILE* pipe = _popen(cmdstr,"r");
   if (!pipe) {
      return "popen Failed!";
   }
  while (!feof(pipe)) {

  // use buffer to read and add to result
  if (fgets(buffer,128,pipe) != NULL)
     result += buffer;
   }

   _pclose(pipe);
   return result;
} 

我的 Path 环境变量包括我的 mingw bin 目录以及我存储所有库的文件夹;但是我收到错误 'g++' is not recognized as an internal or external command.' 此外,如果我用完整路径替换 g++,它不会在同一文件夹中找到库。我知道这不是环境变量问题,因为我在所有较小的项目中都使用 G++(这个项目不适用)并且没有任何问题。我不确定现在要去哪里,所以感谢您的帮助

解决方法

您可以通过单击属性来检查 PATH 环境变量 在你的项目然后调试 - >环境然后编辑并输入 $(Path) 在弹出窗口的顶部窗口中。 Visual Studio 2019 将展示 PATH 环境的值被发送到应用程序时 将在 Visual Studio Community/Enterprise 或 Pro 中调试。 感谢@dresherjm