Linux使用cmake编译项目,如何使用cmake在linux中构建Qt项目(How to build Qt project in linux with cmake)...
如何使用cmake在linux中構(gòu)建Qt項(xiàng)目(How to build Qt project in linux with cmake)
我使用的是ubuntu 14.04,cmake 2.8.12.2,Qt5.6.2(內(nèi)置版本),GNU make 3.81
用cmake PathToSource -G "Eclipse CDT4 - Unix Makefiles"運(yùn)行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源文件,構(gòu)建并安裝它沒(méi)有問(wèn)題。 我再做cmake PathToSource -G "Eclipse CDT4 - Unix Makefiles" ,制作它。 我收到很多錯(cuò)誤,比如/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
更新時(shí)間:2020-07-05 07:07
最滿意答案
Qt 5.7需要C ++ 11編譯器。 如果你從auto pair得到那種錯(cuò)誤,聽(tīng)起來(lái)你的編譯器沒(méi)有編譯C ++ 11代碼。 有兩個(gè)可能的原因:
你只需要將-std=c++11傳遞給你的編譯器,正如這個(gè)問(wèn)題所解釋的那樣 。
你有太舊的編譯器。 但是,由于您使用相同的編譯器編譯Qt 5.7本身,這對(duì)您來(lái)說(shuō)應(yīng)該不是問(wèn)題。
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
相關(guān)問(wèn)答
你的腳本有幾個(gè)錯(cuò)誤,還有一些東西可以改進(jìn)。 更改后,它將如下所示: 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 ,您應(yīng)該使用(組合)這些函數(shù): 在頂級(jí)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
...
首先,如果要進(jìn)行適當(dāng)?shù)囊蕾囮P(guān)系處理,請(qǐng)明確列出要包含的文件,替換file(GLOB ...) 。 這還將確保構(gòu)建正在為您期望的文件集創(chuàng)建依賴關(guān)系。 除了下面的原因, 這個(gè)答案還有更多關(guān)于你可能想要這樣做的細(xì)節(jié)。 AUTOUIC的CMake文檔包括以下聲明: 如果找到與ui_.h匹配的預(yù)處理程序#include指令,并且存在.ui文件,則將執(zhí)行uic以生成相應(yīng)的文件。 您能否確認(rèn)您的.cpp源代碼具有遵循此模式的#include指令? 在您的file(GLOB
...
CMake生成順序是根據(jù)文件和目標(biāo)之間的依賴關(guān)系計(jì)算的。 如果您的qt庫(kù)依賴于.ui文件生成的標(biāo)頭,那么您必須在目標(biāo)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通常應(yīng)該在.ui文件上執(zhí)行UIC 順便說(shuō)一句,使用target_link_libraries只在鏈接時(shí)設(shè)置目標(biāo)之間的依賴關(guān)系。
...
這不是cmake問(wèn)題,而是使用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不同,你可以找到許多這樣的有用鏈接,順便說(shuō)一下我的ubuntu的版本是12.04并且我安裝了沒(méi)有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
...
我認(rèn)為你是這個(gè)bug的受害者(仍未解決)。 我不是100%確定出了什么問(wèn)題,但你可以在評(píng)論中找到一些想法。 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二進(jìn)制文件等。 Qt Creator使用工具包作為(多個(gè))項(xiàng)目中一起使用的東西的集合,因此您不必一次又一次地定義這些相同的設(shè)置。 套件中可用的設(shè)置取決于您啟用的插件,如果未設(shè)置某些信息,Creator非常高興 - 只要您正在處理的項(xiàng)目不需要此信息。 因此,如果你打開(kāi)一個(gè)基于qmake的項(xiàng)目,如果一個(gè)工具包沒(méi)有設(shè)置Qt版本(這是提供qmake二進(jìn)制文件),創(chuàng)建者會(huì)抱怨。 如果您嘗試打開(kāi)基于cmake的項(xiàng)
...
您需要使用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得到那種錯(cuò)誤,聽(tīng)起來(lái)你的編譯器沒(méi)有編譯C ++ 11代碼。 有兩個(gè)可能的原因: 你只需要將-std=c++11傳遞給你的編譯器,正如這個(gè)問(wèn)題所解釋的那樣 。 你有太舊的編譯器。 但是,由于您使用相同的編譯器編譯Qt 5.7本身,這對(duì)您來(lái)說(shuō)應(yīng)該不是問(wèn)題。 Qt 5.7 requires C++11 compiler. If you get that kind of error from auto pair, it sounds lik
...
總結(jié)
以上是生活随笔為你收集整理的Linux使用cmake编译项目,如何使用cmake在linux中构建Qt项目(How to build Qt project in linux with cmake)...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: kaggle账号_Kaggle 数据挖掘
- 下一篇: linux切换目录使用命令,linux命