Возникла проблема. Я хочу писать динамические библиотеки на C++.
И Использовать их в python. По роду задачи мне необходим будет QT и его механихм сигналов-слотов.
Загружать библиотеки я буду скорее всего с помошью QPluginLoader.
Гуглил-гуглил и гагуглил вот такую штуку http://pyqt.sourceforge.net/Docs/sip4/using.html#ref-using
И у меня возникли проблемы именно на этапе с QT.
И так у меня есть такие файлы.
hello.h
// Define the interface to the hello library. #include <qlabel.h> #include <qwidget.h> #include <qstring.h> class Hello : public QLabel { // This is needed by the Qt Meta-Object Compiler. Q_OBJECT public: Hello(QWidget *parent); private: // Prevent instances from being copied. Hello(const Hello &); Hello &operator=(const Hello &); }; //void setDefault(const QString &def);
hello.cpp
#include "hello.h" #include "stdio.h" /* void setDefault(const QString &def) { } */ Hello::Hello(QWidget *parent = 0):QLabel(parent) { printf("First Qt example function"); } Hello::Hello(const Hello &) { } Hello &Hello::operator=(const Hello &) { return *this; }
proj.pro
QT += core gui TARGET = hello TEMPLATE = lib SOURCES += hello.cpp HEADERS += hello.h
Теперь, следуя инстукциям из турториала я создал вот такие файлы
hello.sip
// Define the SIP wrapper to the hello library. %Module hello %Import QtGui/QtGuimod.sip %If (Qt_4_2_0 -) class Hello : public QLabel { %TypeHeaderCode #include <hello.h> %End public: Hello(QWidget *parent /TransferThis/ = 0); private: Hello(const Hello &); }; %End
configure.py
import os import sipconfig from PyQt4 import pyqtconfig # The name of the SIP build file generated by SIP and used by the build # system. build_file = "hello.sbf" # Get the PyQt configuration information. config = pyqtconfig.Configuration() # Get the extra SIP flags needed by the imported PyQt modules. Note that # this normally only includes those flags (-x and -t) that relate to SIP's # versioning system. pyqt_sip_flags = config.pyqt_sip_flags # Run SIP to generate the code. Note that we tell SIP where to find the qt # module's specification files using the -I flag. os.system(" ".join([config.sip_bin, "-c", ".", "-b", build_file, "-I", config.pyqt_sip_dir, pyqt_sip_flags, "hello.sip"])) # We are going to install the SIP specification file for this module and # its configuration module. installs = [] installs.append(["hello.sip", os.path.join(config.default_sip_dir, "hello")]) installs.append(["helloconfig.py", config.default_mod_dir]) # Create the Makefile. The QtGuiModuleMakefile class provided by the # pyqtconfig module takes care of all the extra preprocessor, compiler and # linker flags needed by the Qt library. makefile = pyqtconfig.QtGuiModuleMakefile( configuration=config, build_file=build_file, installs=installs ) # Add the library we are wrapping. The name doesn't include any platform # specific prefixes or extensions (e.g. the "lib" prefix on UNIX, or the # ".dll" extension on Windows). makefile.extra_libs = ["hello"] # Generate the Makefile itself. makefile.generate() # Now we create the configuration module. This is done by merging a Python # dictionary (whose values are normally determined dynamically) with a # (static) template. content = { # Publish where the SIP specifications for this module will be # installed. "hello_sip_dir": config.default_sip_dir, # Publish the set of SIP flags needed by this module. As these are the # same flags needed by the qt module we could leave it out, but this # allows us to change the flags at a later date without breaking # scripts that import the configuration module. "hello_sip_flags": pyqt_sip_flags } # This creates the helloconfig.py module from the helloconfig.py.in # template and the dictionary. sipconfig.create_config_module("helloconfig.py", "helloconfig.py.in", content)
from PyQt4 import pyqtconfig # These are installation specific values created when Hello was configured. # The following line will be replaced when this template is used to create # the final configuration module. # @SIP_CONFIGURATION@ class Configuration(pyqtconfig.Configuration): """The class that represents Hello configuration values. """ def __init__(self, sub_cfg=None): """Initialise an instance of the class. sub_cfg is the list of sub-class configurations. It should be None when called normally. """ # This is all standard code to be copied verbatim except for the # name of the module containing the super-class. if sub_cfg: cfg = sub_cfg else: cfg = [] cfg.append(_pkg_config) pyqtconfig.Configuration.__init__(self, cfg) class HelloModuleMakefile(pyqtconfig.QtGuiModuleMakefile): """The Makefile class for modules that %Import hello. """ def finalise(self): """Finalise the macros. """ # Make sure our C++ library is linked. self.extra_libs.append("hello") # Let the super-class do what it needs to. pyqtconfig.QtGuiModuleMakefile.finalise(self)
import hello h = hello.Hello() print h
$qmake-qt4 $make # это я собираю библиотеку libhello.so $ python ./configure.py $ make /usr/bin/ld: cannot find -lhello collect2: error: ld returned 1 exit status make: *** [hello.so] Error 1 Эту ошибку я исправил добавлением -L. в Makefile $ ls configure.py hello.cpp hello.o hello.so libhello.so.1.0 moc_hello.cpp pythontest.py siphellocmodule.o helloconfig.py hello.exp hello.sbf libhello.so libhello.so.1.0.0 moc_hello.o sipAPIhello.h siphelloHello.cpp helloconfig.py.in hello.h hello.sip libhello.so.1 Makefile proj.pro siphellocmodule.cpp siphelloHello.o
Traceback (most recent call last): File "./pythontest.py", line 1, in <module> import hello ImportError: libhello.so.1: cannot open shared object file: No such file or directory