Вопрос

I need to use dynamic library, writen on Qt/C++ in python code.

I found this tool http://pyqt.sourceforge.net/Docs/sip4/using.html#ref-simple-c-example And I have problems with using it.

So, I have this files for C++ library

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 &);
};

hello.cpp

#include "hello.h"
#include "stdio.h"

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

I compile it with qmake-qt4 Then use make, and got theesw files

-rwxr-xr-x 1 alex alex 630459 Dec 15 00:03 hello.so
lrwxrwxrwx 1 alex alex     17 Dec 15 00:01 libhello.so -> libhello.so.1.0.0
lrwxrwxrwx 1 alex alex     17 Dec 15 00:01 libhello.so.1 -> libhello.so.1.0.0
lrwxrwxrwx 1 alex alex     17 Dec 15 00:01 libhello.so.1.0 -> libhello.so.1.0.0
-rwxr-xr-x 1 alex alex  21295 Dec 15 00:01 libhello.so.1.0.0

Then, Ia have files for SIP configuration

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)

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

helloconfig.py.in

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)

Then I make configuration with

 $ python ./configure.py 

Then I have make Error with make

/usr/bin/ld: cannot find -lhello
collect2: error: ld returned 1 exit status
make: *** [hello.so] Error 1

But this is simple problem, that solved with adding -L.

And i have new .so in my directory - hello.so

Then, I am using simple python script to test result.

import hello
h = hello.Hello()
print h

And I have got an error

Ever/ QtExample $ python ./pythontest.py  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

Where is my mistake? What I am doing wrong?

Это было полезно?

Решение

Have you run make install? This should have added libhello.so into /usr/lib.

If this is just a test library and you don't want to install it, you should have libhello.so in same folder you've hello module or add libhello.so's folder path into your LD_LIBRARY_PATH environment variable.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top