Reputation: 289
I'm trying to create a simple game using C++ with Python embedding. The python code is embedded in my C++ code. I used Python/C API for this. There are 2 goals that I would like to achieve:
1) Application should be able to run on a computer that does not have Python installed.
2) Application should be an one standalone executable file(.exe)
For example, the following simple code only works if the system has python already installed on it:
#include <Python.h>
#include <conio.h>
#include <stdio.h>
int main()
{
char pySearchPath[] = "Python27";
Py_SetPythonHome(pySearchPath);
Py_Initialize();
PyRun_SimpleString("print 'Hello World'");
PyRun_SimpleString("for i in range(5):\n"
"\tprint i,\n");
Py_Finalize();
getch();
return 0;
}
Python needs to be installed on the computer or these files should be in the executable's directory:
python27.dll
Python27\
DLLs\ (contents of DLLs folder)
Lib\ (contents of Lib folder)
Question: How can I include/add/bind these files within my .exe
so that my application can execute on a system which has no python installed? I would like to have a standalone executable. Is it possible? If yes then please provide some hints about how to accomplish this? I already invested much of time searching in internet but unfortunately I didn't find any thing useful.
Probably, there is an other possibility to solve this problem, however I am not sure. For example could the following method work?
Python-Code ----transforming----> bytecode -----> native code -------> import in programm
UPD: With Cython I generate .pyd file (.dll). How can I generate .h file from its or call some functions in my app(if it is possible, of course)?
Thank you.
Upvotes: 4
Views: 1936
Reputation: 5315
Yes, you can embed python in C++. Google provided me two links that I think can be useful for you. I am not sure if you already knew these links as you didn't mention in your question about your research.
http://www.codeproject.com/Articles/11805/Embedding-Python-in-C-C-Part-I This link has two part tutorial on this topic.
http://realmike.org/blog/2012/07/05/supercharging-c-code-with-embedded-python/ This link is also discussing the same topic.
It is basically avoided to paste off site links in the answer. But unfortunately the answer to your question is too long that I am forced to provide the links here. Sorry for that. Just to be safe you should download the whole tutorial from these links, so that you don't regret in future if the tutorials become offline!
Upvotes: 1