且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

是否可以在C ++的运行时动态创建一个函数?

更新时间:2022-06-01 02:30:21

,当然,在其他答案中提到没有任何工具,但只需使用C ++编译器

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

只需在您的C ++程序中执行这些步骤(在linux上,但在其他操作系统上必须类似)

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


  1. 使用 ofstream
  2. 将C ++程序写入文件(例如在/tmp/prog.cc中) >
  3. 通过 system(c ++ /tmp/prog.cc -o /tmp/prog.so -shared -fPIC);

  4. 动态加载程序,例如使用 dlopen()

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