0

Currently, I embed lua in my c++ code.

I wonder if the same is possible with c++. That way I can pass objects and use libraries within the script. to do this, I think I would need the following from the master c++ code:

  1. execute the running of the script
  2. compile the script
  3. run the script
  4. the script passes pointer to it's data objects
  5. the script is done and the program uses the data objects.

is this possible? small example?

For me it is useful as I ran science calculation that I don't want to stop but i want to add functionality on the fly. I also see this being useful for servers in c++ that don't want to shut down but want to make a change.

5
  • 1
    You're looking for a C++ interpreter: stackoverflow.com/questions/69539/… Commented Oct 20, 2012 at 22:46
  • @OlafDietsche: That page you linked to mentioned CINT which is getting replaced with Cling. Cling uses a parser based on LLVM/Clang and is way more powerful than CINT ever could be. Commented Oct 20, 2012 at 23:26
  • @honk Cling is mentioned as well: stackoverflow.com/a/12116668/1741542. I mentioned the other link, because it gives an overview over several solutions. Btw, I don't have experience with any C++ interpreter. Commented Oct 20, 2012 at 23:30
  • cling is great but limited. I'm looking more for compile then run solution. Commented Oct 20, 2012 at 23:41
  • i was thinking more in terms of may be using spawn to compile the program and execute but i'm not sure how to pass an object faq.cprogramming.com/cgi-bin/…. but since i'm new to this I wanted to ask a broader question. Commented Oct 20, 2012 at 23:50

1 Answer 1

2

You may choose to use some kind of C++ interpreter but they can't do every thing. C++ is one of the hardest to implement(if not that hardest) languages. Just think about macros, templates and every other thing that make C++ as powerful as we know they are really hard to parse and understand and beside all of that we have optimization that create such fast and thin codes for us, so how an interpreter can be such powerful and understand all of this? Even best known compilers may fail to understand all of the standard or at least have errors for understanding complicated C++ codes. In one of my projects that heavily use templates using MSVC 2010, it take about 10 minutes to compile the code! So interpreters usually simplify their task and only support a subset of full C++, so if you are cool with this use some interpreter. I never used one of them but I heard cint is good.

The other option is you have a free compiler like g++ and actually compile your code into some kind of shared library and then load it dynamically and call some functions from it based on some kind of configuration.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you. yes, i'm looking more for compile and share solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.