-edit- похоже, что проблема с путем и невозможностью найти его папку / папку.Несмотря на то, что g ++ находится в этом каталоге bin.
Я пытаюсь запустить g ++ на окнах в моем приложении, но я получаю ошибку ниже.Как мне это исправить?примечание стороны, я могу сделать g++ dummy.cpp
в приглашении без проблем.
args -o file.exe -x c++ -
stdout
: 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);
}