Thread: Is it possible....?

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    29

    Is it possible....?

    So i was sitting in calculus today absent-mindedly not studying for the final when i thought of something. Is it possible to write and compile a program, that writes and compiles a program? (oooOOo inception)
    something like:
    [code]
    open file;
    file.write(insert program to be written here-ish);
    save file "something.c";
    compile "something.c";
    //and maybe (this might be pushing it)
    run "something";
    [\code]

    If this has already been done then i would love to see it.
    Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Whyrusleeping
    Is it possible to write and compile a program, that writes and compiles a program?
    Yes, since it is possible to write a program whose output is a program, and it is possible to write a compiler. It becomes a matter of connecting these two, e.g., modify a compiler to produce a program that it then compiles, or write the program source output to file and then invoke a compiler to accept that generated file as input.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Whyrusleeping View Post
    Is it possible to write and compile a program, that writes and compiles a program?
    Yes.
    Quote Originally Posted by Whyrusleeping View Post
    If this has already been done then i would love to see it.
    I hear that you can search for things on the internet.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Not only is that possible, but you can also write a program which when compiled and run, produces it's own source code as output, which can of course again be compiled and run to once again produce itself.
    This is called a "Quine".
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Whyrusleeping View Post
    So i was sitting in calculus today absent-mindedly not studying for the final when i thought of something. Is it possible to write and compile a program, that writes and compiles a program? (oooOOo inception)
    something like:
    [code]
    open file;
    file.write(insert program to be written here-ish);
    save file "something.c";
    compile "something.c";
    //and maybe (this might be pushing it)
    run "something";
    [\code]

    If this has already been done then i would love to see it.
    Thanks
    Of course you can... it's called a compiler ...

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    29
    So i guess i should specify that i want the program to not just write the source code for another program, but compile and possibly run it. is this as simple as im thinking? like, could i call gcc from inside my program?

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Whyrusleeping View Post
    So i guess i should specify that i want the program to not just write the source code for another program, but compile and possibly run it. is this as simple as im thinking? like, could i call gcc from inside my program?
    That would be called an IDE... (Integrated Developer's Environment)... and you're probably using one already.

    Now... on the off chance you are hinting at the program *itself* writing your source code, while you sit and smile... no. (Think "SkyNet")

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    29
    well, im using emacs. which, i dont beleive is an IDE. What code would i use to call gcc from inside a program?

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Whyrusleeping View Post
    well, im using emacs. which, i dont beleive is an IDE. What code would i use to call gcc from inside a program?
    From the online EMACS manual... Building - GNU Emacs Manual

    The main page is here... GNU Emacs - GNU Project - Free Software Foundation (FSF)

  10. #10
    Registered User
    Join Date
    May 2011
    Posts
    29
    im not THAT bad at this. i know how to compile... what i was asking was using C what code would i use to for instance, call gcc?
    i.e.
    Code:
    int main()
    {
    compile("file.c");
    return 0;
    }

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Look into the system() call from the C libaries or whatever calls your OS provides ... eg. ShellExecute() on windows.

Popular pages Recent additions subscribe to a feed