gcc / g ++给我错误“CreateProcess:没有这样的文件或目录”

时间:2011-03-30 18:28:30

标签: gcc g++

-edit-它似乎是路径的问题,无法找到它的bin /文件夹。即使g ++在bin目录中。

我正在尝试在我的应用程序中在Windows上启动g ++但我收到以下错误。我如何解决它?注意我可以在提示中g++ dummy.cpp没有问题。

args -o file.exe -x c++ -

标准输出

: CreateProcess: No such file or directory

-edit-我的代码是......

#include <windows.h> 
#include <stdio.h> 
#include <strsafe.h>

#include <ios>
#include <iostream>
#include <fstream>
#include <sstream>
#include <exception>
#include <string>
#include <deque>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

string gcc_bin="E:/dev/external/MinGW/bin/g++.exe";
string gcc_folder="E:/dev/external/MinGW/bin/";

int launch_gcc(ostringstream&o);
int main(){
    ostringstream osz;
    osz << "#include <cstdio>" << endl << "int main(){ printf(\"hello\"); } return 4; }";
    {
        launch_gcc(osz);
    }
    return 0;
}





void ErrorExit(PTSTR);
int launch_gcc(ostringstream&o)
{

    char buf2[4096];
char buf[4096];
ExpandEnvironmentStrings("%PATH%", buf, 4095);
OutputDebugString(buf);

    sprintf(buf2, "PATH=%s;%s;\0\0", gcc_folder.c_str(), buf);

    STARTUPINFO startupInfo;
    PROCESS_INFORMATION processInformation;

    HANDLE g_hChildStd_IN_Rd = NULL;
    HANDLE g_hChildStd_IN_Wr = NULL;
    HANDLE g_hChildStd_OUT_Rd = NULL;
    HANDLE g_hChildStd_OUT_Wr = NULL;
    HANDLE g_hChildStd_ERR_Rd = NULL;
    HANDLE g_hChildStd_ERR_Wr = NULL;

    HANDLE g_hInputFile = NULL;

    SECURITY_ATTRIBUTES saAttr;  
    saAttr.nLength = sizeof(SECURITY_ATTRIBUTES); 
    saAttr.bInheritHandle = TRUE; 
    saAttr.lpSecurityDescriptor = NULL; 

    if ( ! CreatePipe(&g_hChildStd_OUT_Rd, &g_hChildStd_OUT_Wr, &saAttr, 0) ) 
    ErrorExit(TEXT("StdoutRd CreatePipe")); 
    if ( ! SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0) )
    ErrorExit(TEXT("Stdout SetHandleInformation"));

    if ( ! CreatePipe(&g_hChildStd_ERR_Rd, &g_hChildStd_ERR_Wr, &saAttr, 0) ) 
    ErrorExit(TEXT("StderrRd CreatePipe")); 
    if ( ! SetHandleInformation(g_hChildStd_OUT_Rd, HANDLE_FLAG_INHERIT, 0) )
    ErrorExit(TEXT("Stderr SetHandleInformation"));

    if (! CreatePipe(&g_hChildStd_IN_Rd, &g_hChildStd_IN_Wr, &saAttr, 0)) 
    ErrorExit(TEXT("Stdin CreatePipe")); 
    if ( ! SetHandleInformation(g_hChildStd_IN_Wr, HANDLE_FLAG_INHERIT, 0) )
    ErrorExit(TEXT("Stdin SetHandleInformation")); 

    ZeroMemory( &startupInfo, sizeof(STARTUPINFO) );
    startupInfo.cb = sizeof(STARTUPINFOA); 
    startupInfo.hStdError = g_hChildStd_OUT_Wr;
    startupInfo.hStdOutput = g_hChildStd_ERR_Wr;
    startupInfo.hStdInput = g_hChildStd_IN_Rd;
    startupInfo.dwFlags |= STARTF_USESTDHANDLES;

    ZeroMemory( &processInformation, sizeof(PROCESS_INFORMATION) );

    bool bSuccess = CreateProcess(
        gcc_bin.c_str(),
        " -o \"c:/dev/src/git/myprj/theout.exe\" -x c++ -",
0,
  0,
  1,
  NORMAL_PRIORITY_CLASS,
  0,//buf2,
  0,//gcc_folder.c_str(),
  &startupInfo,
  &processInformation
);
   if ( ! bSuccess ) 
      ErrorExit(TEXT("CreateProcess"));
   else 
   {
      // Close handles to the child process and its primary thread.
      // Some applications might keep these handles to monitor the status
      // of the child process, for example. 

      CloseHandle(processInformation.hProcess);
      CloseHandle(processInformation.hThread);
   }


    { 
    DWORD dwRead, dwWritten; 
    BOOL bSuccess = FALSE;

    auto sz=o.str();
    bSuccess = WriteFile(g_hChildStd_IN_Wr, sz.c_str(), sz.size(), &dwWritten, NULL);
    //if ( ! bSuccess ) break; 

    if ( ! CloseHandle(g_hChildStd_IN_Wr) ) 
        ErrorExit(TEXT("StdInWr CloseHandle")); 
    } 



    #define BUFSIZE 1024*4
    { 
    DWORD dwRead, dwWritten; 
    CHAR chBuf[BUFSIZE]; 
    BOOL bSuccess = FALSE;
    HANDLE hParentStdOut = GetStdHandle(STD_OUTPUT_HANDLE);

    chBuf[0]=0;
    if (!CloseHandle(g_hChildStd_OUT_Wr)) 
        ErrorExit(TEXT("StdOutWr CloseHandle")); 

    for (;;) 
    { 
        bSuccess = ReadFile( g_hChildStd_OUT_Rd, chBuf, BUFSIZE, &dwRead, NULL);
        if( ! bSuccess || dwRead == 0 ) break; 

        bSuccess = WriteFile(hParentStdOut, chBuf, 
                            dwRead, &dwWritten, NULL);
        chBuf[dwWritten]=0;
        if (! bSuccess ){ 
            printf("%s", chBuf);
            break; 
        }
    } 
    }

    { 
    DWORD dwRead, dwWritten; 
    CHAR chBuf[BUFSIZE]; 
    BOOL bSuccess = FALSE;
    HANDLE hParentStdErr = GetStdHandle(STD_ERROR_HANDLE);

    if (!CloseHandle(g_hChildStd_ERR_Wr)) 
        ErrorExit(TEXT("StdOutWr CloseHandle")); 

    for (;;) 
    { 
        bSuccess = ReadFile( g_hChildStd_ERR_Rd, chBuf, BUFSIZE, &dwRead, NULL);
        if( ! bSuccess || dwRead == 0 ) break; 

        bSuccess = WriteFile(hParentStdErr, chBuf, 
                            dwRead, &dwWritten, NULL);
        chBuf[dwWritten]=0;
        if (! bSuccess ){ 
            printf("%s", chBuf);
            break; 
        }
    }
    auto a=1;
    }


    return 0;
}

void ErrorExit(PTSTR lpszFunction) 
{ 
    LPVOID lpMsgBuf;
    LPVOID lpDisplayBuf;
    DWORD dw = GetLastError(); 

    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0, NULL );

    lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, 
        (lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR)); 
    StringCchPrintf((LPTSTR)lpDisplayBuf, 
        LocalSize(lpDisplayBuf) / sizeof(TCHAR),
        TEXT("%s failed with error %d: %s"), 
        lpszFunction, dw, lpMsgBuf); 
    MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); 

    LocalFree(lpMsgBuf);
    LocalFree(lpDisplayBuf);
    ExitProcess(1);
}

10 个答案:

答案 0 :(得分:6)

尝试将g ++编译器的路径添加到PATH环境变量中:

TCHAR *path;
TCHAR *newpath;
DWORD dwSize = GetEnvironmentVariable(TEXT("PATH"), NULL, 0);

path = new TCHAR[dwSize];
GetEnvironmentVariable(TEXT("PATH"), path, dwSize);


dwSize += MAX_PATH;
newpath = new TCHAR[dwSize];
_tcscpy_s(newpath, dwSize, TEXT("E:\\dev\\external\\MinGW\\bin;"));
_tcscat_s(newpath, dwSize, path);
SetEnvironmentVariable(TEXT("PATH"), newpath);

delete[] path;
delete[] newpath;

此时,您的进程的环境块包含PATH变量,其中包含g ++编译器的路径。注意:这不会影响用户的环境。

您可以使用char代替TCHARstrcpystrcat。这种方式适用于两种情况:启用Unicode且不支持Unicode。

答案 1 :(得分:4)

很可能系统上存在编译器的双实例。

如果是这样,请尝试以下实验,以便当前路径下的那个可以运行源编译:

D:\cmn_dev\mingw64\bin>.\g++ HelloGcc.cpp -o Hello.exe

希望这可能会有所帮助。问候。

答案 2 :(得分:3)

您使用的是哪种GCC for Windows? MinGW,Cygwin,还有什么?

您是否尝试过登录和退出此问题? CreateProcess: No such file or directory

在Windows上,GCC需要将其bin目录放在PATH上,它不会查找自己的二进制文件的目录。尝试添加像

这样的东西
wchar_t buf[4096];
ExpandEnvironmentStringsW(L"%PATH%", buf, 4095);
OutputDebugStringW(buf);

在调用g ++之前进入你的程序,以确保目录位于程序环境的路径上(如果你没有在调试器中运行你的程序,请使用wprintf)

注意你是否试图在其中包含空格字符的路径上安装GCC,MinGW和Cygwin都会对此发出警告。

答案 3 :(得分:2)

我遇到了同样的问题,并在将“C:\ MinGW \ msys \ 1.0 \ bin”添加到PATH系统变量后解决了问题。

答案 4 :(得分:1)

使用Sysinternals ProcessMonitor(http://technet.microsoft.com/en-us/sysinternals/bb896645)跟踪代码发出的系统调用(CreateFile,CreateProcess,Registry查询)及其成功和返回值。这也显示了找到代码找不到的可执行文件的所有不同尝试 - 大多数时候这很明显,哪个错误(例如错字,转义,路径中的空格等)导致代码无法找到g ++可执行文件。

答案 5 :(得分:1)

使用像BertNase这样的Sysinternals ProcessMonitor说。你所做的是找到在进程名称列下进行编译的.exe名称,如gcc.exe。然后查看Result列,任何不是SUCCESS的内容都会检查出来。我认为你所寻找的是一个名词没有找到的结果。

我遇到了同样的问题并按照我刚才提到的做了,我发现gcc.exe正在为cc1obj.exe获取NAME NOT FOUND结果。所以我做了一个有根据的猜测并进入了\ libexec \ gcc \ mingw32 \ 4.5.0下的我的MinGW文件夹(版本号可能与你不一样)并制作了cc1.exe的副本,然后将其重命名为cc1obj.exe 。而wala,解决了这个问题。

你可能没有错过同一个文件,但听起来好像遵循这个过程会修复它。

答案 6 :(得分:0)

昨天我遇到一个问题,程序无法处理这样的路径:

<some stuff>;%ProgramFiles%\path\to\bins;<some other stuff>

我将其替换为:

<some stuff>;C:\Program Files\path\to\bins;<some other stuff>

它有效。你可能想看看这个

答案 7 :(得分:0)

你得到的错误说gcc“CreateProcess”的功能试图访问某个文件但找不到它。

显然,如果gcc捕获错误,它正在运行,所以你的PATH没有错。

然而,gcc无法找到编译所需的程序或库。我在mingw32上知道这是一个奇怪的错误。我最好的建议是重新安装mingw32。您可能已经更改了程序使用的文件。

答案 8 :(得分:0)

我在设置Atom时也遇到了问题。但后来我发现我的MinGW是从Codeblocks文件夹中复制的。通过官方MinGW安装程序重新安装软件包并添加目录路径(对于我的案例C:\ MinGW \ bin)

  

高级系统设置&gt;环境变量&gt;路径

解决了我的问题

答案 9 :(得分:0)

旧线程,但谁知道它可以提供帮助。

对我来说,我将完整路径替换为编译器的位置。

所以:

set path="< folder that contains the compiler>"

我认为当前路径中可能包含一些未被编译器正确解析的元素(没有双引号,没有空格等)

相关问题