• 8287阅读
  • 1回复

编译静态QT源码包 [复制链接]

上一主题 下一主题
离线rootlife
 

只看楼主 倒序阅读 楼主  发表于: 2010-12-03
关键词: QT4makefile
好长时间没来了, 关于发布QT程序的事情也没有什么时间去搞, 因为我主要是搞Windows平台,用VC的,所以花在QT上的时间不多,
前几天把QT的静态编译弄完了, 没及时上来发, 如果有谁看到这篇文章就当一些借鉴吧。建议大家多上一下QT centre,上面讨论得比较深入,问题解决方案也比较多,
会学到更多的知识。

话不多说,首先从http://qt.nokia.com/downloads下载最新的源码包,记住是源码包不是BIN文件,是右边的,然后解压它,这是QT centre上的说法。

因为QT跟java不同,QT是平台相关的东西 ,所以要用到LINUX平台的G++编译器,及X11库和其相关库,所以我们先安装这些库
sudo apt-get install g++
sudo apt-get install libX11-dev libXext-dev libXtst-dev

然后切换到源码包所在目录, ./configure -static            ( -release / -debug)
选择你想要的版本。是debug还是其release 的,这部比较快,

然后是make ,或make sub-src 。make 时间长,make 的东西除了源码外还有Demos , examples这些东西, 所以建议make sub-src.
然后
sudo make install_qmake                         安装qmake  。安装目录为/usr/local/Trolltech
sudo make install_mkspecs                      //

然后设置你的profile文件,这个文件在/etc目录下。
在其中加入下面这行。
export PATH="$PATH:/usr/local/Trolltech/Qt-4.7.0/bin"  编译的时候自动搜索bin目录,用qmake进行编译。 记得看准目录,不要从这里照抄路径。
到此后,据QT centre上的文档说你已经可以用qmake直接编译你的程序了, 这部我没有试, 我接下来直接 sudo make install
这部花的时间极长,比configure那个还长好几倍,编译了许多的Demo 程序,例子程序,等了几乎有2个多钟。

QT centre上给了三行命令, 如下:
 ./configure -static -nomake demos -nomake examples -nomake tools
 make
 make install
好像是说这样就不用make demos, examples这些东西了, 其中最后一个tools好像不能用。不过我以前用的是BIN文件,不知道源码这个命令参数可不可以,各位不妨试试。

完成后,你就可以直接make 你的QT程序了, 当然这样看起来有点麻烦,因为要在终端里面编译自己的程序了,但qmake的确是个很好用的东西,他甚至可以自己生成makefile文件, 你只需要转到你要编译的源码目录下,然后make,就可以自动完成对程序的编译。 个人觉得很方便,所以建议大家也花个10分20分的时间看下qmake是如何编译程序的,还有就是如何写一个简易的.pro文件。

具体的文档,大家可以参考QT centre, 我就是看得这个。
文章标题为: Building static Qt on Linux
具体网址没有记下来,因为直接把网页下载下来了。
现附录上文档的内容。

Building
First you have to download the source package from Trolltech and unpack it.
I assume the shared version of Qt is not installed. If the shared version of Qt is installed, read the text after "Hints".
Then open a console and switch to the source directory.
Before Qt 4.3.x
type
./configure -static

type
make sub-src

If you only type make (without the sub-src) then all examples and all demos will be build. With static libs this takes very much space (A test got about 3.8GiB big and it wasn't finished yet.), so I really don't recommend that way.

 type
 su
 make install_qmake
 make install_mkspecs
 make sub-src-install_subtargets

 If you want to install the documentation as well
 make install_htmldocs
 Documentation can be found in file:///usr/local/Trolltech/Qt-4.2.2/doc/html/index.html
 set the path in your ~/.profile
 export PATH="$PATH:/usr/local/Trolltech/Qt-4.2.2/bin"
 now you can use the qmake located in /usr/local/Trolltech/Qt-4.2.2/bin to build a static version of your project.
Just run qmake and make.

For Qt 4.3.x
type:
 ./configure -static -nomake demos -nomake examples -nomake tools
 make
 make installHints

Coexistence of shared and static versions
If there is already a shared version of Qt installed, make sure you specify a different installation directory when you configure Qt for a static build.
For instance, if you used something like /usr/local/Trolltech/Qt-4.2.2 when you installed Qt (which is probably the case if you did a plain ./configure, make and make install) then run the configure command specifying another directory like so:
 ./configure -static -prefix /usr/local/Trolltech/Qt-4.2.2_static
Keep in mind that when you want to switch from shared to static version or vice versa, you need to change the path variable that is used by the linker.
The reason it is necessary to have different directories for the static and shared versions is that, and I quote the ld linker manual:
ld will search a directory for a library with an extension of .so before searching for one with an extension of .a.
Though the -static option to the linker ld will force the use of static libraries, it seems much more straightforward to place the static and shared libraries in different locations.
No debug libraries
It is recommended not to build the debug version of the libraries. The compile time is very long and they take much space. Use the -release option to configure to build a release only version of the libraries. For example:
 ./configure -static -release

Distro independency
If you would like to be better assured that your program will run on an unknown target system, use the following options (valid as of Qt 4.2.2)
-qt-zlib -qt-gif -qt-libpng -qt-libmng -qt-libjpeg
for example:
 ./configure -static -release -qt-zlib -qt-gif -qt-libpng -qt-libmng -qt-libjpeg

These options tell Qt to use the zlib, gif, png, mng and jpeg libraries that are bundled with Qt, and not to look for those libraries on the system where the application is run. This means that you can run your application on target systems which may not have those libraries or where these libraries have different names. I experienced some of these problems when running executables on an Ubuntu-system which where compiled on a SuSE-system.



[ 此帖被rootlife在2010-12-03 23:45重新编辑 ]
离线rootlife

只看该作者 1楼 发表于: 2010-12-03
修改profile文件属性
sudo chmod  ago+rw /etc/profile
快速回复
限100 字节
 
上一个 下一个