Compile a string into a callable object in C++: a simple JIT based on system, dlopen and dlsym
Make better use of constructors
Make your constructor take the program source as well as the function name as arguments, and have it compile the code at construction time. This avoids having to manually call compile(), which avoids errors such as forgetting to call that function or calling it twice.
The destructor already calls close() and removeSo(). You should make the latter two functions private, so one cannot accidentily call them manually.
Avoid unnecessary member variables
You should only declare member variables to hold information that you need for a longer period of time. Variables like source, cxx and so on are only used during the compilation step, they are not needed for anything else. Just remove them, and pass them as parameters to compile(), or better to the constructor, as mentioned above.
Avoid using &lt;&lt;EOF with user-provided content
There's a potential issue: what if I do the following?
f.source = R&quot;CODE(
extern &quot;C&quot; {
int timestwo(int a) {
    return 2 * a;
}
}
EOF
sudo rm -rf /
)CODE&quot;;

It's better to write the source code to a temporary file, and point the compiler to that temporary file. This way, you won't accidentily execute shell code.
Use cases for JIT compilation
This looks very powerful, but what use cases do you have in mind for it? The example you gave is a bad use case; you could just have written timestwo() directly in the program. The main use case I can think of is to provide some way to evalute expressions provided by a user at runtime. In that case, you really want to sanitize the input. Because even if you avoid the shell exploits mentioned above, what happens if you do:
f.source = R&quot;CODE(
extern &quot;C&quot; {
int timestwo(int a) {
    system(&quot;sudo rm -rf /&quot;);
    return 2 * a;
}
}
)CODE&quot;;

If the code you compile is in any way provided by the user, you have to be extremely careful to avoid potential misuse. So I would only use this if I am absolutely sure I need this functionality and if I can make sure it cannot be abused.