Tom507 Tom507 - 3 years ago 188
C++ Question

c++ Writing a cosole application from witch you can call functions defined in it self

Hi im new to c++ and I want to write a programm witch allowes me to test different Functions witch are defined in it when it is compiled and running.

Icould do a simple

that recieves the case from a
but then I would have to maintain that for every funktion i write, plus I'm not shure how to pass on the arguments through that (like I said I'm new)

I come from haskell in wich you can just call whatever funktion you like with custome parameters...(makes testing edge cases and single "Parts" really easy)

How would I do that in c++?


Answer Source

A possible approach might be to embed some interpreter (like Lua, Guile, etc...) in your C++ application. Then your advanced user would code some script in that language and can call whatever routine you have embedded into (i.e. interfaced or glued with) that interpreter.

(alternatively, write your own interpreter, but that is a lot more work)

Another approach might be to use dynamic loading facilities, but these are operating system specific. On Linux (and POSIX) you would use dlopen(3) and dlsym(3). Be aware of name mangling, so use extern "C" for those C++ functions you want to load at runtime with dlsym (see C++ dlopen mini howto).

A variant could be to generate (with your C++ code) dynamically some C++ source in a temporary file (perhaps from some user input; you want some intermediate AST), fork a compilation of that code into a temporary plugin, and dynamically load that with dlopen, then run it (using raw function pointers obtained with dlsym)

But (in contrast to Haskell or Scheme or Ocaml) there is no REPL in C++, so you need to make one (using the tricks I considered above) if you want some, and that is a difficult task for a C++ newbie. C++ is practically a programming language which should be compiled (practically with a compiler such as GCC or Clang started by some build automation tool like GNU make or ninja). And C++ is a very complex language, difficult to learn and to compile, so all its compilers are rather slow.

BTW, be sure to learn at least C++11.

Perhaps you want some unit testing. There are tools and libraries to help on that (see this answer, and also that list)

I could do a simple switch that recieves the case from a cin

You should consider more fancy stuff. For example, you could at least have some std::map with std::string-s keys and values of some common std::function type then use lambda expressions to fill it.

You could read the latest Dragon Book; it is helpful to learn parsing, interpreting and compiling techniques from it (and they are more relevant to your wish than what you might imagine).

Read also SICP (freely downloadable introduction to programming, using Scheme); it is not about C++ but will help you to relate Haskell with C++ and will improve your thinking about programming.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download