如何使用cmake在linux中构建Qt项目(How to build Qt project in linux with cmake)
我使用的是ubuntu 14.04,cmake 2.8.12.2,Qt5.6.2(内置版本),GNU make 3.81
用cmake PathToSource -G "Eclipse CDT4 - Unix Makefiles"运行cmake之后
我做的。 我得到#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. "\ #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); } /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); }
怎么解决?
I am using ubuntu 14.04, cmake 2.8.12.2, Qt5.6.2 (a built version), GNU make 3.81
After I run cmake with cmake PathToSource -G "Eclipse CDT4 - Unix Makefiles"
I do make. I get #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. "\
I then download source file of Qt5.7.0, build and install it without problem. I do again cmake PathToSource -G "Eclipse CDT4 - Unix Makefiles", make it. I get many errors, such as /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); and /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); }
How to solve it?
原文:https://stackoverflow.com/questions/40316244
更新时间:2020-07-05 07:07
最满意答案
Qt 5.7需要C ++ 11编译器。 如果你从auto pair得到那种错误,听起来你的编译器没有编译C ++ 11代码。 有两个可能的原因:
你只需要将-std=c++11传递给你的编译器,正如这个问题所解释的那样 。
你有太旧的编译器。 但是,由于您使用相同的编译器编译Qt 5.7本身,这对您来说应该不是问题。
Qt 5.7 requires C++11 compiler. If you get that kind of error from auto pair, it sounds like your compiler is not compiling C++11 code. There are two possible reasons:
You just need to pass -std=c++11 to your compiler, as explaned under this question.
You have too old compiler. However, since you compiled Qt 5.7 itself with the same compiler, this shouldn't be the problem for you.
2017-05-23
相关问答
你的脚本有几个错误,还有一些东西可以改进。 更改后,它将如下所示: cmake_minimum_required(VERSION 3.0.2)
project(MyProject)
find_package(Qt5Widgets)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
add_library(mainwindow mainwindow.cpp)
target_link_lib
...
要在CMakeLists.txt中使用Qt5 ,您应该使用(组合)这些函数: 在顶级CMakeLists.txt中 #### Qt5
find_package( Qt5Core REQUIRED )
find_package( Qt5Widgets REQUIRED )
find_package( Qt5Gui REQUIRED )
find_package( Qt5OpenGL REQUIRED )
find_package( Qt5Multimedia REQUIRED )
find_pack
...
首先,如果要进行适当的依赖关系处理,请明确列出要包含的文件,替换file(GLOB ...) 。 这还将确保构建正在为您期望的文件集创建依赖关系。 除了下面的原因, 这个答案还有更多关于你可能想要这样做的细节。 AUTOUIC的CMake文档包括以下声明: 如果找到与ui_.h匹配的预处理程序#include指令,并且存在.ui文件,则将执行uic以生成相应的文件。 您能否确认您的.cpp源代码具有遵循此模式的#include指令? 在您的file(GLOB
...
CMake生成顺序是根据文件和目标之间的依赖关系计算的。 如果您的qt库依赖于.ui文件生成的标头,那么您必须在目标qt输入中添加${qt_UI_H} : QT5_WRAP_UI(qt_UI_H ${qt_UI})
[...]
add_library(qt "${DIR_SRC}/qt/form_main.cpp" ${qt_UI_H})
在编译libqt之前,CMake通常应该在.ui文件上执行UIC 顺便说一句,使用target_link_libraries只在链接时设置目标之间的依赖关系。
...
这不是cmake问题,而是使用about.cpp文件损坏。 由于某种原因,它有流浪的角色。 解决方案是“uncorrupt it”,然后它将使用相同的cmake文件。 This is not a cmake issue, but corruption with your about.cpp file. It has got stray characters for one reason or another. The solution is "uncorrupt it", and then it
...
将qt5添加到CMakeLists.txt与qt4不同,你可以找到许多这样的有用链接,顺便说一下我的ubuntu的版本是12.04并且我安装了没有apt-get的qt,我使用这种格式在我的cmakelists中查找qt。文本 : find_package(Qt5Widgets)
include_directories(${Qt5Widgets_INCLUDES}
/opt/Qt5.0.2/5.0.2/gcc/include/QtGui
/opt/Qt5.0.2/5.0.2/gcc/include
...
我认为你是这个bug的受害者(仍未解决)。 我不是100%确定出了什么问题,但你可以在评论中找到一些想法。 I think you're a victim of this bug (still unsolved). I'm not 100% sure of what's going wrong, but you can find some ideas in the comments.
您可以在Qt Creator中使用或不使用Qt,使用或不使用C ++编译器,使用或不使用cmake二进制文件等。 Qt Creator使用工具包作为(多个)项目中一起使用的东西的集合,因此您不必一次又一次地定义这些相同的设置。 套件中可用的设置取决于您启用的插件,如果未设置某些信息,Creator非常高兴 - 只要您正在处理的项目不需要此信息。 因此,如果你打开一个基于qmake的项目,如果一个工具包没有设置Qt版本(这是提供qmake二进制文件),创建者会抱怨。 如果您尝试打开基于cmake的项
...
您需要使用CMAKE_PREFIX_PATH 。 例如: cmake.exe -DCMAKE_PREFIX_PATH="C:/path/to/Qt/5.X/compiler/lib/cmake"
You need to use CMAKE_PREFIX_PATH. For example: cmake.exe -DCMAKE_PREFIX_PATH="C:/path/to/Qt/5.X/compiler/lib/cmake"
Qt 5.7需要C ++ 11编译器。 如果你从auto pair得到那种错误,听起来你的编译器没有编译C ++ 11代码。 有两个可能的原因: 你只需要将-std=c++11传递给你的编译器,正如这个问题所解释的那样 。 你有太旧的编译器。 但是,由于您使用相同的编译器编译Qt 5.7本身,这对您来说应该不是问题。 Qt 5.7 requires C++11 compiler. If you get that kind of error from auto pair, it sounds lik
...