我需要在运行时编译和链接一些代码。我正在使用这里建议的方法:https ://stackoverflow.com/a/10565120/3038460
基本上我正在将我的代码写入 .cpp 文件,然后使用
/usr/bin/g++ -shared mysource.cpp -o libname.so
一切正常,而我只#include
来自标准库的标题。但是如果我需要在“动态”代码中使用自定义类怎么办?如何包含我自己的标题?.cpp 文件临时存储在我的二进制文件的同一位置,这可能与我的源文件的位置不同。有没有办法在运行时知道原始源代码的位置?我怀疑。
此外,即使原始源代码不可用,我也希望我的代码能够工作。
澄清一下,mysource.cpp 可能如下所示:
#include <stdlib.h>
#include <vector>
#include "myheader.h" <---- how can g++ find this file? At runtime,
when I create mysource.cpp, I have no idea
where myheader.h is located
void f(){
// Code
}
解决此问题的最佳解决方案是什么?