0

I have a C++ program foobar which starts with main() and then the flow of control goes through a first part, then the second part of the program. If I change main to foobar_main, I can then compile the whole program and a SWIG Python wrapper to a shared library foobar.so, and import this to Python, call foobar_main from within Python and everything works fine.

The second part communicates with the first one by some respectable C++ constructs. Specifically: the first part creates some single objects of some classes, and the second part uses class static methods to get those objects.

Now I want to run only the first part from main() and the second part from Python. That is, I want to start the C++ program foobar and then after the first part is finished, run a Python script (programmatically from within C++) that continues with the second part.

To do this, I:

  • compile the second part and a SWIG wrapper to foobar2.so
  • replace the second part of C++ code with system("python foobar2.py")
  • compile the modified C++ program to foobar1.so and load to foobar
  • write the script foobar2.py which imports foobar1 and foobar2 and then equivalent to the second part

Then I attempt to run foobar. It does not work, because it appears, that the routines in the second part complain that certain steps which should have been done in the first part, are not done.

This is embarasing but obviously I have some deep flaws here in my understanding of how computers work :) Can somebody clue me in what I am missing, including possibly simplifying the above process?

Adam
  • 16,808
  • 7
  • 52
  • 98
Mark Galeck
  • 6,155
  • 1
  • 28
  • 55
  • so both part 1 and part 2 are in C++? – Adam Oct 14 '13 at 03:51
  • also define "run only the first part from main and the second from python" What languages are doing what? – Adam Oct 14 '13 at 03:52
  • Yes both part 1 and part 2 are in C++. But then, there is the Python script written which is equivalent to part 2 (each Python command will eventually call, through a wrapper, the equivalent C++ method). "Run only ..." is clarified above. – Mark Galeck Oct 14 '13 at 04:16
  • The easiest way is to wrap part 1, then have a Python script that calls the wrapped part 1 then the Python part 2. This means the program starts with Python, not C++. The C++ entry is possible, but more annoying to accomplish. – Adam Oct 14 '13 at 05:19

1 Answers1

1

I'm going to assume your C++ code looks like this:

void part1()
{}

void part2()
{}

int main()
{
    part1();
    part2();
}

And that you have a Python version of part2() that is implemented with some other wrapped C++ functions. If these assumptions are wrong let me know.

I think the easiest way to go is to wrap part1() along with the other wrapped part2-related functions, then have a Python script like this:

import foobar

foobar.part1()
py_part2()

This of course means that the program starts in Python. If you need to start a C++ program for some reason (i.e. you need main()) then in order to use py_part2() you'll have to embed the Python interpreter inside your C++ program. This is a much more difficult and involved process, this answer has good info about how to get started.


Since you're learning I'll explain why system("python foobar2.py") doesn't work. In this scheme you have your C++ program start another process (program), named python, and then wait for it to finish. These are two completely different programs that in your case don't talk to each other and don't share anything in common. Hence why it doesn't work.

In general, reconsider anything that involves system. Its primary use seems to be to point out beginner programmers.

Community
  • 1
  • 1
Adam
  • 16,808
  • 7
  • 52
  • 98
  • No Adam, I apologize, but you don't seem to understand my questions. I know exactly how to do what you are telling me to do. I told you in the start of the question. When you say "start another process", and "two completely different programs", I am sorry, that is the whole point of using _shared_ libraries - that is why it _should_ work what I am trying to do, because of _shared_ libraries .so. They should allow to share symbols between processes. – Mark Galeck Oct 14 '13 at 12:23
  • yes indeed I have to embed. Yes I did not understand how computers work (thinking of it, I probably still don't). Thank you Adam. – Mark Galeck Oct 14 '13 at 18:59