读书人

请教:怎么借用编译器

发布时间: 2013-03-14 10:33:15 作者: rapoo

请问:如何借用编译器
要开发一个软件,软件中有编程界面、用户在里面用c语言编程。那么,软件开发人员(非最终用户)如何解决编译器的问题呢?请详细解答,谢谢。
[解决办法]
可以参考GUN gcc编译器,代码是开源的,但直接用时会受到GPL条款的限制。
[解决办法]
参考Win-TC的做法 ?
[解决办法]
开发 compiler, 顶
[解决办法]

// This is a simple filter application.  It will spawn 
// the application on command line. But before spawning
// the application, it will create a pipe that will direct the
// spawned application's stdout to the filter. The filter
// will remove ASCII 7 (beep) characters.

// Beeper.Cpp

/* Compile options needed: None */
#include <stdio.h>
#include <string.h>

int main()
{
int i;
for(i=0;i<100;++i)
{
printf("\nThis is speaker beep number %d... \n\7", i+1);
}
return 0;
}


// BeepFilter.Cpp
/* Compile options needed: none
Execute as: BeepFilter.exe <path>Beeper.exe
*/
#include <windows.h>
#include <process.h>
#include <memory.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>

#define OUT_BUFF_SIZE 512
#define READ_HANDLE 0
#define WRITE_HANDLE 1
#define BEEP_CHAR 7

char szBuffer[OUT_BUFF_SIZE];

int Filter(char* szBuff, ULONG nSize, int nChar)
{
char* szPos = szBuff + nSize -1;
char* szEnd = szPos;
int nRet = nSize;

while (szPos > szBuff)
{
if (*szPos == nChar)
{
memmove(szPos, szPos+1, szEnd - szPos);
--nRet;
}
--szPos;
}
return nRet;
}

int main(int argc, char** argv)
{
int nExitCode = STILL_ACTIVE;
if (argc >= 2)
{
HANDLE hProcess;
int hStdOut;
int hStdOutPipe[2];

// Create the pipe
if(_pipe(hStdOutPipe, 512, O_BINARY
------解决方案--------------------


O_NOINHERIT) == -1)
return 1;

// Duplicate stdout handle (next line will close original)
hStdOut = _dup(_fileno(stdout));

// Duplicate write end of pipe to stdout handle
if(_dup2(hStdOutPipe[WRITE_HANDLE], _fileno(stdout)) != 0)
return 2;

// Close original write end of pipe
close(hStdOutPipe[WRITE_HANDLE]);

// Spawn process
hProcess = (HANDLE)spawnvp(P_NOWAIT, argv[1],
(const char* const*)&argv[1]);

// Duplicate copy of original stdout back into stdout
if(_dup2(hStdOut, _fileno(stdout)) != 0)
return 3;

// Close duplicate copy of original stdout
close(hStdOut);

if(hProcess)
{
int nOutRead;
while (nExitCode == STILL_ACTIVE)
{
nOutRead = read(hStdOutPipe[READ_HANDLE],
szBuffer, OUT_BUFF_SIZE);
if(nOutRead)
{
nOutRead = Filter(szBuffer, nOutRead, BEEP_CHAR);
fwrite(szBuffer, 1, nOutRead, stdout);
}

if(!GetExitCodeProcess(hProcess,(unsigned long*)&nExitCode))
return 4;
}
}
}

printf("\nPress \'ENTER\' key to continue... ");
getchar();
return nExitCode;
}


[解决办法]
就是说有部分IDE的功能?
就像Code Blocks那样,或者某些online judge那样?
[解决办法]
什么叫“软件开发人员如何解决编译器的问题呢?”?

[解决办法]
我给的例子是调用其它程序并将其标准输入、标准输出、错误输出用管道重定向以便由代码控制。
编译器一般提供命令行版,用命令行参数指定要编译的代码和编译选项,输出到标准输出、错误输出。
结合以上两者,你可以用管道的方式调用编译器编译,链接器链接,并获取其输出分析,达到自动编译和链接的目的。

Code Blocks是不是开源的?如果是请参考其相关源代码。

读书人网 >C语言

热点推荐