如何用cmake在linux下构建Qt项目

How to build Qt project in linux with cmake

本文关键字:构建 Qt 项目 linux 何用 cmake      更新时间:2023-10-16

我使用的是ubuntu 14.04, cmake 2.8.12.2, Qt5.6.2(一个构建版本),GNU make 3.81

使用cmake PathToSource -G "Eclipse CDT4 - Unix Makefiles"运行cmake后
我用make。我得到#error "You must build your code with position independent code if Qt was built with -reduce-relocations. " "Compile your code with -fPIC (-fPIE is not enough)." # error "You must build your code with position independent code if Qt was built with -reduce-relocations. "

然后我下载Qt5.7.0的源文件,构建并安装它没有问题。我再做一次cmake PathToSource -G "Eclipse CDT4 - Unix Makefiles",成功了。我得到了很多错误,如/home/sflee/Documents/Software_dev/3rd_party/Qt5.7.0/include/QtCore/qhash.h:957:10: error: ‘pair’ does not name a type auto pair = qAsConst(*this).equal_range(akey);/home/sflee/Documents/Software_dev/3rd_party/Qt5.7.0/include/QtCore/qbasicatomic.h:285:14: error: ‘Ops’ has not been declared { return Ops::fetchAndAddRelease(_q_value, valueToAdd); }

如何解决?

Qt 5.7需要c++ 11编译器。如果您从auto pair中得到这种错误,则听起来您的编译器没有编译c++ 11代码。有两个可能的原因:

  1. 你只需要将-std=c++11传递给你的编译器,正如这个问题所解释的。

  2. 你的编译器太旧了。然而,因为你用同样的编译器编译了Qt 5.7,这对你来说应该不是问题。