Thread: C++ compiler as a lib function?

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    15

    C++ compiler as a lib function?

    I would like to pass a string of valid C++ code to a library function which compiles it to machine code and returns it. Then I should be able to call another lib function which executes it. I want the compiler to be all memory resident involving no file operations during compilation. Does it exist?

  2. #2
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Not to my knowledge but then I don't know of anyone outside of a trojan or worm writer that wanted such a thing. There are other ways to accomplish this if your requirements are adjusted somewhat...like going with python code->bytecode, stuff like that. What purpose is served by forcing the compiler to be entirely memory-resident?
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    15
    I know I can accomplish this in other languages. I've been using javascript and perl which both have eval functions, although the string is interpreted and not compiled I think.

    I want it in C++ to evaluate user input which is a C expression or close. I could complete it to a C prog and compile it in the usual way from the program, but it's slow. That's why I want it all in memory: speed.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I have never tried it, but perhaps you should take a look at Ch.
    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

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    15
    Quote Originally Posted by laserlight View Post
    I have never tried it, but perhaps you should take a look at Ch.
    Thanks for the tip. Very interesting. Worth trying.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by mustermeister View Post
    I want it in C++ to evaluate user input which is a C expression or close. I could complete it to a C prog and compile it in the usual way from the program, but it's slow. That's why I want it all in memory: speed.
    If it is just for single expressions, ie, you are talking about some kind of command line interpreter such as available in perl and other interpreted languages (and with js too in the form of some online things, I think w3c has one), where "the user" is literally someone typing at a keyboard, this will not be slow at all.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiler warning and a confusing function declaration.
    By PaulBlay in forum C Programming
    Replies: 3
    Last Post: 05-20-2009, 10:18 AM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. problem about static Lib and function pointer
    By wu7up in forum C Programming
    Replies: 3
    Last Post: 02-24-2003, 09:34 AM
  4. Replies: 5
    Last Post: 02-08-2003, 07:42 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM