在X11平台下qt程序,首先准备好程序中需要使用的资源,库和插件...
比如你的可运行程序取名叫作panel,那把你的panel,那些libQt*.so.4和libQt*.so.4.6.0(链接和共享库都要)放在同一目录下(也可以不同,只要小小修改下shell文件).plugins就不多说了。
在程序的同目录下,新建一個空文档,取名panel.sh (文件名与程序名同名,扩展名为sh,shell文件)。
在panel.sh中原封不动的写入以下语句:
#!/bin/sh
appname=`basename $0 | sed s,/.sh$,,`
dirname=`dirname $0`
tmp="${dirname#?}"
if [ "${dirname%$tmp}" != "/" ]; then
dirname=$PWD/$dirname
fi
LD_LIBRARY_PATH=$dirname
export LD_LIBRARY_PATH
$dirname/$appname $*
保存文件,退出。在终端給文件+x属性: 切换到程序的目录,输入
chmod +x panel.sh
然后运行shell文件就行了(确保panel程序具备X属性),它会自动更改环境变量,运行程序。
如果要调试shell文件,只需要在终端输入:
sh -x panel.sh
这样就ok了。
——————————————————————————————————————————
Windows下:
第一种,静态编译:
mingw平台静态编译,在编译Qt的时候,有个configure.exe程序,
configure.exe -static -platform win32-g++
可以产生静态编译项目文件。然后,
mingw32-make sub-src
就可以编译出静态库。如果只是用来分发程序,也可以mingw32-make release sub-src只编译一个静态库。
第二种 动态编译:
可执行文件 与 需要的动态库放到同一个文件夹下,一般需要
myprogram.exe
mingwm10.dll
libgcc_s_dw2-1.dll
qtcore4.dll
qtgui4.dll
有同学抱怨,动态库拷过去以后,程序报错 无法定位程序输入点于动态链接库QtCore4.dll上 ,这一般是由于系统中装了多套Qt的运行库,而你拷贝的不是Qt安装目录下的库所导致的。比如,当安装的是Qt SDK时,很容易导致这个问题,因为里面的Qt是mingw编译的,但里面的QtCreator是msvc编译的,所以不少人不小心就吧QtCreator带的Qt运行库拷过来了。
如果你不需要其他的插件,那么就可以发布程序了,然而不少同学抱怨 jpg、gif、bmp 等格式的图片无法显示,这是因为 Qt 原生支持 png,而其他格式需要通过插件支持(插件在 %QTDIR%/plugins\imageformats 目录下)
你只需将需要的插件拷贝到可执行程序所在的目录下的 imageformats 目录下即可
myprogram.exe
imageformats\qjpeg4.dll
imageformats\qgif4.dll
...
同样,如果你的程序需要gb2312、gbk编码支持,那么需要将 %QTDIR%\plugins\codecs 目录下的相应插件拷贝到可执行程序所在目录下的 codecs 目录下
myprogram.exe
codecs\qcncodecs4.dll
Linux下:
在panel.sh中语句:
#!/bin/sh appname=`basename $0 | sed s,\.sh$,,` dirname=`dirname $0` tmp="${dirname#?}" if [ "${dirname%$tmp}" != "/" ]; then dirname=$PWD/$dirname fi LD_LIBRARY_PATH=$dirname export LD_LIBRARY_PATH $dirname/$appname $* |
保存文件,退出.在终端给文件+x属性: 切换到程序的目录,输入
chmod +x panel.sh
然后运行shell文件就行了(确保panel程序具备X属性),它会自动更改环境变量,运行程序.
如果要调试shell文件,只需要在终端输入:
sh -x panel.sh
这样就ok了.
关于plugins,有以下3种处理方法:
# Using qt.conf. This is the recommended approach since it provides the most flexibility.
# Using QApplication::addLibraryPath() or QApplication::setLibraryPaths().
# Using a third party installation utility or the target system's package manager to change the hard-coded paths in theQtCore library.
第二种方法很简单。qt.conf的方法也不错.看看这个:
Entry Default Value Prefix QCoreApplication::applicationDirPath() Documentation doc Headers include Libraries lib Binaries bin Plugins plugins Data . Translations translations Settings . Examples . Demos . |
最简单的qt.conf文件這樣寫就好了:(插件在当前文件夹下的plugins文件夹里)
[Paths] Prefix = . Plugins = plugins |