本文章基于友之臂提供的arm版本的qtopia2.2.0和x86版本的qtopia2.2.0,前提是系统中已经安装好交叉编译器。
要实现多线程,主要是要获得带多线程支持的动态链接库libqte-mt.so.2,libqte-mt.so.2需要通过编译qt获得,下面是编译libqte-mt.so.2的方法(arm和x86版本方法类似,一起叙述)。
1)
arm版本解压后的路径为:/opt/ARM/arm-qtopia/
x86版本解压后的路径为:/opt/ARM/x86-qtopia/
2)- cd
/opt/ARM/arm-qtopia/qtopia-2.2.0-FriendlyARM - source
setQteEnv - cd
qt2 - make
clean - echo
yes | ./configure -embedded -no-xft -qconfig qpe -depths 16,32 -system-jpeg -qt-zlib -qt-libpng -gif -no-g++-exceptions -no-qvfb -xplatform linux-arm-g++ -tslib –thread
x86版本执行如下命令:
- cd
/opt/ARM/x86-qtopia/qtopia-2.2.0-FriendlyARM - source
setQteEnv - cd
qt2 - make
clean - echo
yes|./configure -embedded -no-xft -qconfig qpe -depths 16,32 -system-jpeg -gif -thread
用编辑器将其内容的最后一行”cd
- SHELL=/bin/sh
-
- src-mt:
-
cd src; $(MAKE) 'QT_THREAD_SUFFIX=-mt' - 'QT_LFLAGS_MT=$$(SYSCONF_LFLAGS_THREAD)
$$(SYSCONF_LIBS_THREAD)' - 'QT_CXX_MT=$$(SYSCONF_CXXFLAGS_THREAD)
-DQT_THREAD_SUPPORT' - 'QT_C_MT=$$(SYSCONF_CFLAGS_THREAD)
-DQT_THREAD_SUPPORT' -
cd examples/guithreads; $(MAKE) 'QT_THREAD_SUFFIX=-mt' - 'QT_LFLAGS_MT=$$(SYSCONF_LFLAGS_THREAD)
$$(SYSCONF_LIBS_THREAD)' - 'QT_CXX_MT=$$(SYSCONF_CXXFLAGS_THREAD)
-DQT_THREAD_SUPPORT' - 'QT_C_MT=$$(SYSCONF_CFLAGS_THREAD)
-DQT_THREAD_SUPPORT' -
cd examples/semaphores; $(MAKE) 'QT_THREAD_SUFFIX=-mt' - 'QT_LFLAGS_MT=$$(SYSCONF_LFLAGS_THREAD)
$$(SYSCONF_LIBS_THREAD)' - 'QT_CXX_MT=$$(SYSCONF_CXXFLAGS_THREAD)
-DQT_THREAD_SUPPORT' - 'QT_C_MT=$$(SYSCONF_CFLAGS_THREAD)
-DQT_THREAD_SUPPORT' -
cd src; $(MAKE) clean
- make
src-mt
5)如果在如上编译过程中提示 qtextcodec.cpp 编译错误,将 /opt/ARM/new-qtopia/arm-qtopia/qtopia-2.2.0-FriendlyARM/qt2/src/tools/qtextcodec.cpp 改成下面的代码:( 2148-2180 行)
- #define
QT_NO_EUCJPCODEC - #ifndef
QT_NO_EUCJPCODEC - (void)new
QEucJpCodec; - #endif
- #define
QT_NO_SJISCODEC - #ifndef
QT_NO_SJISCODEC - (void)new
QSjisCodec; - #endif
- #define
QT_NO_JISCODEC - #ifndef
QT_NO_JISCODEC - (void)new
QJisCodec; - #endif
- #define
QT_NO_EUCKRCODEC - #ifndef
QT_NO_EUCKRCODEC - (void)new
QEucKrCodec; - #endif
- #define
QT_NO_GBKCODEC - #ifndef
QT_NO_GBKCODEC - (void)new
QGbkCodec; - #endif
- #define
QT_NO_BIG5CODEC - #ifndef
QT_NO_BIG5CODEC - (void)new
QBig5Codec; - #endif
- #define
QT_NO_RTLCODEC - #ifndef
QT_NO_RTLCODEC - (void)new
QArabicCodec; - (void)new
QHebrewCodec; - #endif
- #define
QT_NO_TSCIICODEC - #ifndef
QT_NO_TSCIICODEC - (void)new
QTsciiCodec; - #endif
6)对于arm版本,在/opt/ARM/arm-qtopia/qtopia-2.2.0-
x86版本需要修改/etc/ld.so.conf如下,并执行ldconfig指令。如果不修改的话,在后面的程序执行过程中,会提示找不到libqte-mt.so.2动态链接库。
- include
ld.so.conf.d/*.conf - /opt/ARM/x86-qtopia/qtopia-2.2.0-FriendlyARM/qtopia/lib
- /opt/ARM/x86-qtopia/qtopia-2.2.0-FriendlyARM/qt2/lib
下面以一个例子来说明如何使用多线程编程。
创建如下文件:
- example.pro
//项目文件 - main.cpp
//main函数文件 - example.cpp
//例子程序 - example.h
//例子程序头文件 - arm-build
//编译arm版本可执行文件的脚本 - x86-build
//编译x86版本可执行文件的脚本 - run
//在x86上运行程序的脚本
- example.pro
- CONFIG
+= qtopiaapp - CONFIG
-= buildQuicklaunch - CONFIG
+= qt warn_on release thread - DESTDIR
= $(PWD) -
- HEADERS
+= example.h - SOURCES
+= example.cpp - SOURCES
+= main.cpp - TARGET
= example - unix:LIBS
+= -lm -L/lib
main.cpp
- #include
<qtopia/qpeapplication.h> - #include
<stdio.h> - #include
<qobject.h> - #include
<stdlib.h> -
- #include
"example.h" -
- int
main(int argc,char *argv[]) - {
-
QPEApplication a(argc,argv); -
-
ExampleThread myThread; -
myThread.start(); -
-
return a.exec(); - }
- #include
"example.h" -
- ExampleThread::ExampleThread()
- {
-
- }
- ExampleThread::~ExampleThread()
- {
-
- }
- void
ExampleThread::run() - {
-
while(1) -
{ -
printf("example\n"); -
sleep(1); -
} - }
- #ifndef
EXAMPLE_H - #define
EXAMPLE_H -
- #include
<qthread.h> - #include
<qobject.h> - #include
<stdio.h> - #include
<fcntl.h> - #include
<unistd.h> - #include
<stdlib.h> - #include
<errno.h> - #include
<assert.h> - #include
<signal.h> -
- class
Q_EXPORT ExampleThread : public QObject,public QThread - {
- Q_OBJECT
-
- signals:
-
void ExampleSignal(); - public:
-
ExampleThread(); -
~ExampleThread(); - protected:
- virtual
void run(); -
- };
-
- #endif
//EXAMPLE_H
- #!/bin/bash
-
- source
/opt/ARM/arm-qtopia/qtopia-2.2.0-FriendlyARM/setQpeEnv - qmake
-spec /opt/ARM/arm-qtopia/qtopia-2.2.0-FriendlyARM/qtopia/mkspecs/qws/linux-arm-g++ -o Makefile *.pro -
- make
clean - make
- #!/bin/bash
-
- source
/opt/ARM/x86-qtopia/qtopia-2.2.0-FriendlyARM/setQpeEnv - qmake
-o Makefile -spec /opt/ARM/x86-qtopia/qtopia-2.2.0-FriendlyARM/qtopia/mkspecs/qws/linux-generic-g++ *.pro -
- make
clean - make
- #!/bin/sh
-
- export
ApplicationDir=$PWD - /opt/ARM/x86-qtopia/qtopia-2.2.0-FriendlyARM/qt2/bin/qvfb
-width 320 -height 240 -depth 16 & - #export
CAMERA_DEVICE=/dev/video1 - #qtopia-2.2.0-FrinedlyARM/qt2/bin/qvfb
-width 640 -height 480 -depth 16 & - cd
/opt/ARM/x86-qtopia/qtopia-2.2.0-FriendlyARM/qtopia/image - mkdir
root 2>/dev/null || true - export
HOME=$PWD/root - cd
opt/Qtopia - export
PATH=$PWD/bin:$PATH - export
LD_LIBRARY_PATH=$PWD/lib:$LD_LIBARAY_PATH - export
QTDIR=$PWD - export
QPEDIR=$PWD - export
KDEDIR=$PWD/../kde -
- sleep
3 - $ApplicationDir/example
-qws
为 arm-build,x86-build,run三个脚本添加可执行权限,运行
- ./x86-build
- ./run
执行arm-build,编译成arm版本的程序,复制到开发板上执行,如果也能输出example,表示arm版本添加多线程支持成功
( 为使qt支持输出调试信息到串口,修改 /bin/qtopia 文件最后一行为 exec