私は、QVector、QColorなどのようなqtオブジェクトを継承することなく、それらを使用するいくつかのクラスを持つライブラリを作成しました。今私はこれらのオブジェクトを部分的にPythonで利用できるようにしたいと考えています。私は最初にSIPを試しましたが、これはあまりにも文書化されていないので、例を構築することさえできません。今、標準のC++クラスでうまく動作するboost.pythonを試しています。boost.pythonとqt
しかし、いったんQtを組み込むと、まだコンパイルされますが、Pythonにインポートすることはできません。ここで、最小の例である:
testclass.h
#include <QDebug>
#include <QVector>
#include <QColor>
class testclass
{
public:
testclass();
const char* output();
QVector<double> & data();
static int x(){return 1;}
QColor * c();
private:
QVector<double> v;
};
struct stat;
testclass.cpp
#include "testclass.h"
testclass::testclass()
{
}
const char* testclass::output()
{
qDebug() << "string";
return "hello";
}
QVector<double>& testclass::data()
{
return v;
}
QColor* testclass::c()
{
return new QColor();
}
testclassBoost.cpp
#include "testclass.h"
#include <boost/python.hpp>
using namespace boost::python;
BOOST_PYTHON_MODULE(libtestclass)
{
// Create the Python type object for our extension class and define __init__ function.
class_<testclass>("testclass", init<>())
.def("output", &testclass::output) // Add a regular member function.
;
}
CMakeList.txt
project(boostpythontest)
cmake_minimum_required(VERSION 2.8)
find_package(Qt4 REQUIRED)
FIND_PACKAGE(Boost 1.45.0)
IF(Boost_FOUND)
SET(Boost_USE_STATIC_LIBS OFF)
SET(Boost_USE_MULTITHREADED ON)
SET(Boost_USE_STATIC_RUNTIME OFF)
FIND_PACKAGE(Boost 1.45.0 COMPONENTS python)
ELSEIF(NOT Boost_FOUND)
MESSAGE(FATAL_ERROR "Unable to find correct Boost version. Did you set BOOST_ROOT?")
ENDIF()
include_directories(${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR} ${Boost_INCLUDE_DIRS} "/usr/include/python2.7")
set(SRCS
testclass.cpp
testclassBoost.cpp
)
add_library(testclass SHARED ${SRCS})
target_link_libraries(testclass ${Boost_LIBRARIES} ${QT_QTCORE_LIBRARY})
は今、次のエラーで得られたライブラリーの結果をインポートしようとしている:
Python 2.7.2 (default, Jun 27 2011, 14:59:25)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import libtestclass
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: ./libtestclass.so: undefined symbol: _ZN6QColor10invalidateEv
は興味深いことに、いくつかのQTのクラスは問題ではありません。関数c()がなければ正常に動作します(QVectorでは問題ありません)。この仕事をするために私は何ができますか?私はPythonでqtのものを使用するつもりはありませんが、ライブラリのC++部分だけでqtを使いたいと思います。
はい、これは答えです、ありがとうございます!私は$ {QT_QTCORE_LIBRARY}を$ {QT_LIBRARYS} に置き換え、セット(QT_USE_GUI TRUE) ($ {QT_USE_FILE})を追加しました。しかし、なぜ私はライブラリをコンパイルし、別のC++プログラムで使用することもできますか? – Felix
@Felix:AFAIK、ELF共有ライブラリは、欠落したシンボルとリンクすることができ、ロード時に提供される予定です。 Pythonは.soをロードしようとする前にQtGuiシンボルをロードしなかったため、実行時にエラーが発生しました。 –