Thursday, February 10, 2011

Compiling program within another program using gcc

From command line i am getting file name which i have to compile using gcc. lets say its like this.

./a.out fileToBeCompiled.c

Then how i can compile this file using gcc within my program? lets say main.c for which a.out is being made.

  • You can always call gcc to compile from an shell execution command within your program.

    Reference to the system function.

    itsaboutcode : Thanks, it work for me.
    ammoQ : Using system is a bad idea, but if it works for itsaboutcode, it shall be fine with me
    From monksy
  • Just exec gcc from within you program.

    int main(int argc, char** argv)
    {
       execv("/usr/bin/gcc", argv);
    }
    
    From stimms
  • Learn how to call fork(), exec(), and wait(). You probably want to use execv() for this purpose.

    From D.Shawley
  • ./a.out fileToBeCompiled.c && gcc fileToBeCompiled.c
    

    If ./a.out fails (it it returns something other than 0) gcc will not be called

    From pmg

0 comments:

Post a Comment