I have a C++ program and I want to implement scripts on it. The desired scenario is, I have an executable of c++ code, it then calls at specific times a python script so it knows what to do through the embeded interpreter and the script then uses some form of API from the c++ program. This is where I ran into a problem. To expose c++ code to python you need to compile a DLL of the wrappers that you want and load it as a module inside python and that breaks my intention of python accessing the executable's functions.
Any way to resolve this problem without resorting to put so much pieces of c++ on a shared library?
What you want to do is to embed Python code into your application. There is an article on python.org on how to do that using raw CPython, but it's not that exhaustive when it comes to C++. A better bet might be to use Boost.Python or SWIG.
Related
I have a problem. I write a python script to make my work faster and now I want to share it with my team.
I don't want them to mess with some imports that are missing in the basic python installation. I know there is a way to compile python to exe, but I wonder if I can compile the code and the imports without messing with py2exe.
Does python have a built-in solution for that?
I saw that python have pyc compile option. Does it compile the import modules as well?
Thanks,
Or
No I don't believe you have a built-in standalone compilation mode native to Python. The pyc is a compiled code but not the kind you usually distribute as an executable program (meaning you would still need the Python interpreter).
If you don't want to use py2exe or other similar packages I advise you to use a portable version of Python with which you can distribute your software (see for example WinPython). The easiest way to accomplish this is by giving the portable distribution together with your code and perhaps a batch file (or similar, if you want to have a .exe alike behavior).
NOTE: You can provide the pyc compile code of the libraries you are using and putting them on the root of you software (or just stating where those imports should happen) but I predict this will give you problems in the future due to dependencies between different libraries. So, it's possible although I would hardly considerate it a good solution for what it seems to me you are trying to achieve.
We know that Qt has a plugin architecture that makes it possible to load code into an application without recompiling or relinking.
We also can integrate javascript into Qt using QScriptEngine.
I am wondering if there are any other dynamic features in Qt, which can add new features/functions to our application, without recompling or relinking.
Maybe too many (that's why your post is downvoted)
For example, you can get a python shell in your code without changing almost anything via the PythonQt library
You can get Lua too!
I have a newbie question regarding Lua.
If I embed some Lua script inside my C++ application. When I compile my C++ application, will the Lua script part be compiled into machine code or does C++ application runs the Lua script part each time with Lua interpreter?
The web is saying using LuaJIT will improve embedded script performance greatly, then I guess the Lua script inside C++ application is never compiled into machine code.
If I would like to squeeze every bit of performance in this kind of situation, i.e, I would like to write part of my program in Lua to be embedded in a C++ application. What is my best option? Is there something I can used to compile Lua part into C++/C part and will this improve performance?
The entire point of a JIT compiler is to generate machine code at runtime from the source files.
LuaJIT will look for 'hotspots' in your code that run frequently (such as inner loops or frequently used functions), and try to compile them to machine code. It doesn't matter where the code came from; after loading, that is completely irrelevant.
I'm trying to write my first game in c++, and I want it to dynamically load everything from files. This includes the enemies, and I was wondering if there was a way to dynamically include their code at runtime, instead of linking the on compile, so that the levels are easily interchangeable. Lua might be an option but I have no clue where to start, and dll seems to be Windows-only (and I wouldn't know where to start there anyway). Can anyone help with this?
tl;dr I want to link in code to my c++ game at runtime.
For the Lua approach you first need to choose the version first. Right now there is the major version 5.1 and 5.2. My previous work was using 5.1 and for my new project I decided to update to 5.2, however I found that my favorite script wrapping tool (SWIG) does not work with 5.2. Just something to decide at the beginning, because you do not want to get a version working and then have to change it.
Lua comes with makefile build environment. My first experience of trying to build on Windows was a bit of a nightmare, did not appear to just run out-of-the-box, so I opted to create my own Visual Studio project at the time, and just include all the .C files in the project. There are two files which need to selectively included/excluded depending on how you intend to compile: lua.c and luac.c. If you are planning to embed Lua in your app, then exclude both of these files; they both contain a main() function and are designed to build console apps. Include all the rest of the C files in your project.
You should be able to compile easy from this point.
When you include the headers of Lua, keep in mind that the functions are C functions so if you are including them from C++ you need to wrap the file inclusion inside of: extern "C" {} - example: C++ Lua 5.1 Issue
Wrapping your interfaces in another topic and there are lots of resources available. My favorite is SWIG but there are lots of options, including hand coding the conversion of your C/C++ -> LUA -> C/C++ code. Would recommend just focusing on getting the first part working first, get the interpreter embedded so that you can run a "hello, world!" script from Lua inside your app.
So going by your requirement of crossplatform use and dynamic linking, what you're probably looking for is an environment like QT which has QLibrary: https://stackoverflow.com/a/9675063/453673
But https://softwareengineering.stackexchange.com/questions/88685/why-arent-more-desktop-apps-written-with-qt
MingW is the open-source equivalent for Visual C++, so it can help you writing code for Windows (though if I had a choice, I'd directly use Visual C++). The way dll's are loaded in Windows is somewhat similar to the way they're loaded in Linux, so you'll be able to write code with #ifdef's to do conditional compilation. I've written one such program a couple of years back.
To load a shared library(always with .so as suffix) under Linux, you could use dlopen(), dlsym() and dlclose()
I would like to call python script files from my c++ program.
I am not sure that the people I will distribute to will have python installed.
I would like to call python script files from my c++ program.
This means that you want to embed Python in your C++ application. As mentioned in Embedding Python in Another Application:
Embedding Python is similar to
extending it, but not quite. The
difference is that when you extend
Python, the main program of the
application is still the Python
interpreter, while if you embed
Python, the main program may have
nothing to do with Python — instead,
some parts of the application
occasionally call the Python
interpreter to run some Python code.
I suggest that you first go through Embedding Python in Another Application. Then refer the following examples
Embedding Python in C/C++: Part I
Embedding Python in C/C++: Part II
Embedding Python in Multi-Threaded C/C++ Applications
If you like Boost.Python, you may visit the following links:
Embedding Python with Boost.Python Part 1
Boost has a python interface library which could help you.
Boost.Python
Interestingly, nobody has mentioned pybind11, yet. From their documentation:
pybind11 is a lightweight header-only library that exposes C++ types
in Python and vice versa, mainly to create Python bindings of existing
C++ code. Its goals and syntax are similar to the excellent
Boost.Python library by David Abrahams: to minimize boilerplate code
in traditional extension modules by inferring type information using
compile-time introspection. [...] Since its creation, this library has
grown beyond Boost.Python in many ways, leading to dramatically
simpler binding code in many common situations.
Concretely, calling into a Python function (called embedding) is as simple as this (taken from the documentation):
#include <pybind11/embed.h> // everything needed for embedding
namespace py = pybind11;
int main() {
py::scoped_interpreter guard{}; // start the interpreter and keep it alive
py::print("Hello, World!"); // use the Python API
}
Use system call to run a python script from C++
#include<iostream>
#include <cstdlib>
using namespace std;
int main ()
{
int result = system("/usr/bin/python3 testGen1.py 1");
cout << result;
}
Embeding the Python interpreter inside your C++ app will let you run Python programs using your application run Python scripts. It will also make it easier possible for those scripts to call C++ functions in your application. If this is what you want then the Boost library mentioned previously may be what you want to make it easier to create the link. In the past I have used SWIG to generate Python interfaces to C++ code. It was not clear from your question whether you wanted the Python scripts to call your C++ program or whether you just wanted the C++ to call Python.
Many of the Python functions use modules which are not built into the Python interpreter. If your Python scripts call these functions then you will either need to have your users install Python or include the python runtime files with your application. It will depend on what modules you import in you Python scripts.
Boost is probably the best choice, however if you're wanting something that's more standalone, and if this is for use with Windows (which seems feasible given that they are the people least likely to have Python installed), then you can use py2exe to create a DLL with entry points suitable for COM objects. You can then interface with the library via COM. (Obviously this is not at all useful as a cross-platform solution).
Using Inter Process Communication (IPC) over socket can be a possible solution. Use a local network socket to listen/trasfer commands between both.