1

我需要使用动态库,用 Python 代码写在 Qt/C++ 上。

我发现了这个工具http://pyqt.sourceforge.net/Docs/sip4/using.html#ref-simple-c-example 我在使用它时遇到了问题。

所以,我有这个 C++ 库的文件

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

你好.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

我用qmake-qt4编译 然后使用make,得到了 theesw 文件

-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

然后,Ia 有 SIP 配置文件

配置文件

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)

你好.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)

然后我用

 $ python ./configure.py 

然后我用 make 弄错了

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

但这是一个简单的问题,通过添加 -L 即可解决。

我的目录中有新的.so - hello.so

然后,我使用简单的 python 脚本来测试结果。

import hello
h = hello.Hello()
print h

我有一个错误

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

我的错误在哪里?我做错了什么?

4

1 回答 1

2

你跑了make install吗?这应该将 libhello.so 添加到 /usr/lib 中。

如果这只是一个测试库并且你不想安装它,你应该libhello.so在你有 hello 模块的同一个文件夹中或者将libhello.so文件夹路径添加到你的LD_LIBRARY_PATH环境变量中。

于 2013-12-15T19:26:07.557 回答