Is it possible to create a function dynamically, during runtime in C++?

Yes, of course, without any tools mentioned in the other answers, but simply using the C++ compiler.

just follow these steps from within your C++ program (on linux, but must be similar on other OS)

  1. write a C++ program into a file (e.g. in /tmp/, using an ofstream
  2. compile the program via system("c++ /tmp/ -o /tmp/ -shared -fPIC");
  3. load the program dynamically, e.g. using dlopen()

Leave a Comment