且构网

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

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

更新时间:2022-04-08 22:01:22

,当然,没有任何工具在其他答案中提到,但只是使用 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. 编译程序
  5. 动态加载程序,例如使用 dlopen()