2

I'm trying to compile certain files from my C program in Linux and give them grades. I'd like to give zero grade if the file did not compile. I already managed to do so, but I have a problem in this case, because gcc is printing a message to the screen (what is called the compilation error).

I've been googling and looked in the man for a flag which would prevent it from doing so, but haven't found one yet.

Does anyone know what is the flag? Or else, maybe there's other solution?

Here's my code:

//child process
    if((pid=fork())==0)
    {
        execl("/usr/bin/gcc", "/usr/bin/gcc", "-o", outpath, fullpath,NULL);
    }
    else
    {
        wait(&stat);
        if(WIFEXITED(stat))
        {
            stat=WEXITSTATUS(stat);
            if(stat!=0)
            {
                if(write(fdresult,",0\r\n",4)==-1)
                {
                    perror("Writing result.csv has failed.");
                    exit(1);
                }
                continue;
            }
        }
    }

Note:This code works. I just need a way to prevent gcc from printing an error when the file does not compile.

Thanks!

edit: Tried adding

dup2("/dev/null",2);

just before execl, but gcc still print out message.

4

1 回答 1

3

在 C 标准中,该freopen函数将给定流 ( stderr) 与给定路径 ( "/dev/null") 的文件相关联,并指定其模式 ( "w")。

#include <stdio.h>

freopen ("/dev/null", "w", stderr);
于 2013-03-24T13:11:20.013 回答