I need to compile and link some code at runtime. I am using the approach suggested here: https://stackoverflow.com/a/10565120/3038460
Basically I am writing my code into a .cpp file and then compiling with
/usr/bin/g++ -shared mysource.cpp -o libname.so
Everything works fine while I only #include
headers from standard libraries. But what if I need to use a custom class within the 'dynamic' code? How can include my own header? The .cpp file is temporary stored in the same location of my binary file and this might be different than the location of my source files. Is there a way to know at runtime where the original source code was located? I doubt.
Moreover I would like my code to work even if the original source code is not available.
To clarify, mysource.cpp might look like this:
#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
}
What's the best solution to solve this problem?