C++ 如何使用-reduce重新定位构建QT
我使用的是Ubuntu 16.04、cmake 3.10.1、QT 5.6.2。C++ 如何使用-reduce重新定位构建QT,c++,qt,C++,Qt,我使用的是Ubuntu 16.04、cmake 3.10.1、QT 5.6.2。 我以前在windows上开发应用程序,所以我不知道如何在linux平台上排除故障。 当我编译代码时,会出现错误 In file included from /usr/local/Qt/5.6.2/5.6/gcc_64/include/QtCore/qcoreapplication.h:37:0, from /usr/local/Qt/5.6.2/5.6/gcc_64/include
我以前在windows上开发应用程序,所以我不知道如何在linux平台上排除故障。
当我编译代码时,会出现错误
In file included from /usr/local/Qt/5.6.2/5.6/gcc_64/include/QtCore/qcoreapplication.h:37:0,
from /usr/local/Qt/5.6.2/5.6/gcc_64/include/QtWidgets/qapplication.h:37,
from /usr/local/Qt/5.6.2/5.6/gcc_64/include/QtWidgets/QApplication:1,
from /home/sulfred/Documents/SoftwareDev/github/SulfredLee/PcapReplayer/BackEnd/main.cpp:3:
/usr/local/Qt/5.6.2/5.6/gcc_64/include/QtCore/qglobal.h:1087:4: error: #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. "\
^
Q1.如何验证我的Qt是使用
-reduce-relocations
构建的显然Qt已经使用
-reduce-relocations
编译;错误消息指出,您必须使用适当的标志构建自己的代码。相关的:
因此,只需尝试将
-fPIE
或-fPIC
标志添加到编译器标志中即可。显然,Qt已使用-reduce-relocations
编译;错误消息指出,您必须使用适当的标志构建自己的代码。相关的:
因此,只要尝试将
-fPIE
或-fPIC
标志添加到编译器标志中即可。在现代CMake中,使用以下方法添加-fPIC
编译标志:
此外,Qt通常对
fPIE
不满意,但它需要-fPIC
。使用CMake变量设置fPIC
仅适用于库目标。此变量将把fPIE
添加到可执行文件目标中,这对于Qt来说是不够的。因此,使用target\u compile\u options()
此外,Qt通常对fPIE
不满意,但它需要-fPIC
。使用CMake变量设置fPIC
仅适用于库目标。此变量将把fPIE
添加到可执行文件目标中,这对于Qt来说是不够的。因此,使用target\u compile\u options()
来显式设置fPIC
。您看到这个问题了吗?非常感谢,但是我没有使用QMake。你看到这个问题了吗?非常感谢,但我没有使用QMake。非常感谢。以下是我在我的CMakeLists.txt
,集合(CMAKE\u CXX\u FLAGS)-Wall-fPIC-std=c++11)中使用的内容
。非常感谢。以下是我在我的CMakeLists.txt
,集合(CMAKE\u CXX\u FLAGS“-Wall-fPIC-std=c++11”)
中使用的内容。
add_executable(MyExecutable ...)
target_compile_options(MyExecutable PRIVATE -fPIC)