Thread: Compile Function?

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    1

    Compile Function?

    Hey guys,

    I was just wondering if there is any type of library that allows me to compile C code? I'm looking for something like:

    Code:
    ...
    
    char *c = CCodeAsAString(); /* Some Function that returns "int main() {...}" */
    
    ...
    
    char *machinecode = compile(c);
    
    ...

    Is there some sort of GCC library or anything? I just don't actually wanna dig into the massive GCC tree or use a shell script wrapper...

    Thanks in advance.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You'd be better off just executing MinGW's (or some equiv. compiler's) tools from your program and then getting the output from the files it spits out.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    53
    Maybe this would be of interest to you, too. Otherwise, you could indeed just use the installed compiler, perhaps using some self-written glue so it produces a linkable binary, which you can load into your own application. I've seen this approach used in a simple graphing calculator a long time ago (for BeOS, in fact) so you could type actual code as your expression. I thought it was a pretty cool idea.

    --
    Computer Programming: An Introduction for the Scientifically Inclined

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    I'd go with something like TCC (http://bellard.org/tcc/) if you were to try and include it in your project. Otherwise do what MacGyver said.

    ie,

    Write the string to a file,
    Call gcc or whatever the users compiler is
    Call the resulting binary

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM