In my program, I need to call a subprocess *sub_prog*. I do this with a system call, e.g. system("sub_prog arg1 arg2...");. Simple enough; all works correctly.
Unfortunately the supercomputer I am using does not support the spawning of subprocess (due to the architecture, you can’t use fork(), popen() etc. with MPI. It’s just a fact.). So the solution would be to make *sub_prog* a (shared) library and call it as a library function, rather than as a sub-process.
I created the shared library *libsub_prog.so* fine. The problem is – the function I want to call is *sub_prog* itself, i.e., I want to call main(argc,argv) of sub_prog.
Questions regarding shared libraries usually center around calling member functions from shared libraries. But what if I want to call the main function itself?
(I thought about renaming *sub_prog*’s int main to another name, essentially turning its main into just another member function. But there are several global variables deefined right before int main). Would I have to put these in a header globals.h and go in and manually insert “#include globals.h” in every source file?)
Thanks to all in advance.
I would just turn it into a normal function by giving it a different name, and call it from the main executable using this new name. If you declare the function to have
extern "C"linkage, it should be easy to locate the function at runtime usingdlsym. As to the global variables, you shouldn’t need to do anything.