我把环境变量设好了,然后我用CreateProcess()调用cl.exe,能编译单个C/C++文件,本来想用CL XX1.CPP XX2.CPP...就可以编译多个文件但是命令行可以,用CreateProcess时就会出现问题,编译不了,各位高手帮帮忙吧~

解决方案 »

  1.   

    但是用ShellExecute我不知道怎么把存cl编译的文件存到指定位置.下面是我的一些程序,还要改进,我想用一个函数查找所有.C或.CPP文件,然后用cl,请高手指点一下迷津.现在正在编写一个函数查找指定文件夹里所有的.C和.CPP文件,然后CL一下,不过在命令行可以,不知道在CreateProcess函数里是否可以.还有个问题,在命令行里不能用CL编译vc++的文件.连接时会出错.
    void CLinkmestDoc::OnProjComply() 
    {
    // TODO: Add your command handler code here
    /***************************保存文件*****************************/
    OnFileSaveAll();
    /****************************************************************/
    bErrFlag = FALSE;
    strShowCompiler="";
    AppandToShow(strShowCompiler);//将通道得到的信息在指定编辑窗显示 CString strBuf;
    m_xTextBuffer.GetText( 0, 0, m_xTextBuffer.GetLineCount() - 1,
    m_xTextBuffer.GetLineLength(m_xTextBuffer.GetLineCount() - 1), strBuf );
    if( strBuf.IsEmpty() ) 
    return;
    CString strPathName = GetPathName(); int index1 = strPathName.ReverseFind('.');
    CString tmp = strPathName;
    strPathName = strPathName.Left(index1);
    if(2 == (tmp.GetLength() - strPathName.GetLength()))
    {
    tmp = "cl.exe \/c " + strPathName + ".c";
    }
    else
    {
    tmp = "cl.exe \/c " + strPathName + ".cpp";
    }
       
    SECURITY_ATTRIBUTES sa;
    sa.bInheritHandle = TRUE;
    sa.lpSecurityDescriptor = NULL;
    sa.nLength = sizeof(SECURITY_ATTRIBUTES);
    if(!CreatePipe(&hRead,&hWrite,&sa,0))
    {
    AfxMessageBox("创建匿名管道失败");
    return;
    }
    STARTUPINFO sui;
    PROCESS_INFORMATION pi;
    ZeroMemory(&sui,sizeof(STARTUPINFO));
    sui.cb = sizeof(STARTUPINFO);
    sui.dwFlags = STARTF_USESTDHANDLES;
    sui.hStdInput = hRead;
    sui.hStdOutput = hWrite;
    sui.hStdError = GetStdHandle(STD_ERROR_HANDLE); int count = tmp.ReverseFind('\\');
    CString tmpn;
    tmpn = tmp.Left(count);
    tmpn = tmpn.Right(tmpn.GetLength()-10);
    AfxMessageBox(tmpn);

    /*********************编译***************************/
    if(!CreateProcess(NULL,tmp.GetBuffer(0),NULL,NULL,TRUE,0,NULL,tmpn.GetBuffer(0),&sui,&pi))
    {
    CloseHandle(hRead);
    CloseHandle(hWrite);
    hRead = NULL;
    hWrite = NULL;
    return;
    }
    else
    {
    AfxMessageBox("编译成功");
    WaitForSingleObject(pi.hProcess, INFINITE);
    char buf[800];
    DWORD dwRead;
    if(!ReadFile(hRead,buf,800,&dwRead,NULL))
    {
    return;
    }
    strShowCompiler = buf;
    AppandToShow(buf);
    if(pi.hThread)
    {
    CloseHandle(pi.hThread);
    }
    if(pi.hProcess)
    {
    CloseHandle(pi.hProcess);
    }
    }
    /*********************链接***************************/
    tmp = "link.exe " + strPathName + ".obj";
    tmpn = tmp.Left(count);
    tmpn = tmpn.Right(tmpn.GetLength()-9);
    // AfxMessageBox(tmpn);
    if(!CreateProcess(NULL,tmp.GetBuffer(0),NULL,NULL,TRUE,0,NULL,tmpn.GetBuffer(0),&sui,&pi))
    {
    CloseHandle(hRead);
    CloseHandle(hWrite);
    hRead = NULL;
    hWrite = NULL;
    return;
    }
    else
    {
    AfxMessageBox("链接成功");
    WaitForSingleObject( pi.hProcess, INFINITE );
    char buf[800];
    DWORD dwRead;
    if(!ReadFile(hRead,buf,800,&dwRead,NULL))
    {
    return;
    }
    strShowCompiler = buf;
    AppandToShow(buf);
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);
    }
    }
      

  2.   

    这个问题基本知道错误在哪里了,是个低级错误,忘了在C++中应该是"\\"才表示"\",换个问题好了,如何在用link.exe的时候设置属性让他能够自动找到vc的库文件,请举个例子好吗?还有就是如何实现单步调试的功能?请高手指教,若实现,会再开帖送分的.
      

  3.   

    单步调试就是 IDE 的主要功能,如果这个也可以让你共享,那 M$ 要哭了... 找个开源的 IDE 研究一下吧。link 的设置请参考 MSDN ,摘录如下:LINK is a 32-bit tool that links Common Object File Format (COFF) object files and libraries to create a 32-bit executable (.EXE) file or dynamic-link library (DLL). Following is a comprehensive list of options for LINK.EXE. For information on how to specify an option, see Set Linker Options. This section also includes information on Compiler-Controlled LINK Options, LINK Input Files, and LINK Output.Option Action 
    /ALIGN:number Specifies the alignment of each section 
    /BASE:{address | @filename,key} Sets a base address for the program 
    /COMMENT:["]comment["] Inserts a comment string into header 
    /DEBUG Creates debugging information 
    /DEBUGTYPE:CV
    /DEBUGTYPE:COFF
    /DEBUGTYPE:BOTH Creates particular formats of debugging information 
    /DEF:filename Passes a module-definition (.DEF) file to the linker 
    /DEFAULTLIB:library Searches specified library when resolving external references 
    /DELAY Controls the delayed loading of DLLs 
    /DELAYLOAD Causes the delayed loading of the specified DLL 
    /DLL Builds a DLL 
    /DRIVER[:UPONLY] Creates a Windows NT kernel mode driver 
    /ENTRY:function Sets the starting address 
    /EXETYPE:DYNAMIC Builds a virtual device driver 
    /EXPORT Exports a function 
    /FIXED[:NO] Creates a program that can be loaded only at its preferred base address 
    /FORCE[:{MULTIPLE|UNRESOLVED}] Forces link to complete in spite of unresolved or multiply defined symbols 
    /GPSIZE:# Specifies the size of communal variables for MIPS and Alpha platforms 
    /HEAP:reserve[,commit] Sets the size of the heap in bytes 
    /IMPLIB:filename Overrides the default import library name 
    /INCLUDE:symbol Forces symbol references 
    /INCREMENTAL:{YES|NO} Controls incremental linking 
    /LARGEADDRESSAWARE Tells the compiler that the application supports addresses larger than two gigabytes. 
    /LIBPATH:path Allows the user to override the environmental library path 
    /LINK50COMPAT Generates import libraries in Visual C++ Version 5.0 format 
    /MACHINE:{IX86|ALPHA|ARM|MIPS|MIPSR41XX|PPC|SH3|SH4} Specifies the target platform 
    /MAP Creates a map file  
    /MAPINFO:{EXPORTS|FIXUPS|LINES} Includes the specified information in the map file 
    /MERGE:from=to Combines sections 
    /NODEFAULTLIB[:library] Ignores all (or specified) default libraries when resolving external references 
    /NOENTRY Creates a resource-only DLL 
    /NOLOGO Suppresses startup banner 
    /OPT:{REF|NOREF|ICF[,iterations]|NOICF} Controls LINK optimizations 
    /ORDER:@filename Places COMDATs into the image in a predetermined order 
    /OUT:filename Specifies the output file name 
    /PDB:filename Creates a program database (.PDB) file 
    /PDBTYPE:{con[solidate]|sept[ypes]} Specifies where to store the Program Database (PDB) debug type information. 
    /PROFILE Enables profiling (creates a mapfile) 
    /RELEASE Sets the checksum in the .EXE header 
    /SECTION:name,attributes Overrides the attributes of a section 
    /STACK:reserve[,commit] Sets the size of the stack in bytes 
    /STUB:filename Attaches an MS-DOS stub program to a Win32 program 
    /SUBSYSTEM:{CONSOLE|WINDOWS|NATIVE|POSIX|WINDOWSCE} [,major[.minor] ] Tells the operating system how to run the .EXE file 
    /SWAPRUN:{NET|CD} Tells the operating system to copy the linker output to a swap file before running it 
    /VERBOSE[:LIB] Prints linker progress messages 
    /VERSION:major[.minor] Assigns a version number 
    /VXD Creates a virtual device driver (VxD) 
    /WARN[:level] Specifies warning level 
    /WS:AGGRESSIVE Aggressively trim process memory 
      

  4.   

    就讲讲思路就好了,比如在被编译的程序里在要调试的地方加一个获得信号才能运行的语句吗?而让程序里让程序能暂停的语句是哪些呢?我试过用getchar或getch都不行.
      

  5.   

    其实我想的是接受主程序选行,然后在所在行里加上getchar(),就需要对文件进行操作了输出一个cout<<endl;getchar();怎么样,也许这个方法有些笨了,高手帮帮忙,有什么好方法吗?
      

  6.   

    单步调试就是 IDE 的主要功能,如果这个也可以让你共享,那 M$ 要哭了... 找个开源的 IDE 研究一下吧。
    -----------------------------------------
    实现单步调试其实是比较容易的,即使你没有机会去写一个调试器,也非常需要看看这本著名的书,
    《Microsoft.NET和Windows应用程序调试》
    http://www.china-pub.com/computers/common/info.asp?id=20508
    4.3 MinDBG:一个简单的Win32调试器
      

  7.   

    可以选择自己制作makefile,不过我觉得麻烦,就使用的先搜索在本目录下所有.c及.cpp的文件,然后使用CString将所有文件加空格,并且要将该路径名加上,组合好后,再加上编译命令,我使用的是g++命令,当然命令的使用,你就要再从网上搜索了。