Qt C++;Python中的代码,PyQt4
我需要使用动态库,用python代码在Qt/C++上编写 我找到了这个工具 我在使用它时遇到了问题Qt C++;Python中的代码,PyQt4,python,c++,qt,pyqt,python-sip,Python,C++,Qt,Pyqt,Python Sip,我需要使用动态库,用python代码在Qt/C++上编写 我找到了这个工具 我在使用它时遇到了问题 所以,我有C++ 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
所以,我有C++ 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 &);
};
proj.pro
QT += core gui
TARGET = hello
TEMPLATE = lib
SOURCES += hello.cpp
HEADERS += hello.h
我用qmake-qt4编译它
然后使用make,并获取这些sw文件
-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配置的文件
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)
你好,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
然后我用
$ python ./configure.py
那我犯了一个错误
/usr/bin/ld: cannot find -lhello
collect2: error: ld returned 1 exit status
make: *** [hello.so] Error 1
但这是一个简单的问题,可以通过添加-L来解决。
我有新的。所以在我的目录中-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
Ever/QtExample$python./pythontest.py回溯(最近一次调用):文件“/pythontest.py”,第1行,在
导入hello ImportError:libhello.so.1:无法打开共享对象文件:没有这样的文件或目录
我的错误在哪里?我做错了什么?您是否运行了
makeinstall
?这应该将libhello.so添加到/usr/lib中
如果这只是一个测试库,而您不想安装它,那么您应该将
libhello.so
放在您拥有hello模块的同一文件夹中,或者将libhello.so
的文件夹路径添加到LD\u library\u path
环境变量中。您还没有将libhello.so.1文件放在系统库路径中。所以在制作时需要指定-L。显然,您还需要在运行时指定库路径,但您还没有这样做。尝试将LIBPATH或LD_LIBRARY_PATH环境变量设置为包含libhello.so.1文件的目录。另外,可能还有一种特定于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