用-fpic替换-fPIC [英] Replace -fPIC with -fpic

查看:183
本文介绍了用-fpic替换-fPIC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Qt CMake时会自动添加 -fPIC 标志来编译选项。我想使用 -fpic ,所以我遍历了所有Cmake变量并将 -fPIC 替换为 -fpic

When using Qt CMake automatically adds the -fPIC flag to compile options. I want to use -fpic, so I went through all Cmake variables and replaced -fPIC with -fpic.

cmake_minimum_required(VERSION 3.5)
project(sss)

find_package(Qt5 REQUIRED COMPONENTS Core Sql)

get_cmake_property(_variableNames VARIABLES)
foreach (_variableName ${_variableNames})
        if (NOT "${${_variableName}}" STREQUAL "")
                string(REPLACE "-fPIC" "-fpic" ${_variableName} ${${_variableName}})
                string(REPLACE "-fPIE" "-fpie" ${_variableName} ${${_variableName}})
        endif()
        #message(STATUS "${_variableName}=${${_variableName}}")
endforeach()

set(CMAKE_CXX_FLAGS "-fpie")
set(CMAKE_EXE_LINKER_FLAGS "-fpie -pie")

add_executable(sss main.cpp)

target_link_libraries(sss Qt5::Core Qt5::Sql)

main.cpp 包含

#include <QSqlDatabase>

int main(){
        QSqlDatabase::addDatabase("QPSQL");
}

不幸的是,CMake仍然添加了 -fPIC 标志,虽然列出的变量不包含它:

Unfortunately CMake still adds the -fPIC flag, althoguh the listed variables does not contain it:

Building CXX object CMakeFiles/sss.dir/main.cpp.o
/usr/bin/c++   -DQT_CORE_LIB -DQT_NO_DEBUG -DQT_SQL_LIB -isystem /usr/include/x86_64-linux-gnu/qt5 -isystem /usr/include/x86_64-linux-gnu/qt5/QtCore -isystem /usr/lib/x86_64-linux-gnu/qt5/mkspecs/linux-g++-64 -isystem /usr/include/x86_64-linux-gnu/qt5/QtSql  -fPIC -o CMakeFiles/sss.dir/main.cpp.o -c src/main.cpp

如何替换 -fPIC -fpic

推荐答案

将我的评论变成答案

您的代码将覆盖CMake全局变量,例如 CMAKE_CXX_COMPILE_OPTIONS_PIC CMAKE_CXX_COMPILE_OPTIONS_PIE

Your code overwrites CMake global variables like CMAKE_CXX_COMPILE_OPTIONS_PIC or CMAKE_CXX_COMPILE_OPTIONS_PIE.

但是Qt带来了自己的 -fPIC 选项通过目标属性。 Qt5 :: Core 目标确实具有 INTERFACE_COMPILE_OPTIONS 设置为 -fPIC (请参见例如此处)。

But Qt brings its own -fPIC option through target properties. The Qt5::Core target does have INTERFACE_COMPILE_OPTIONS set to -fPIC (see e.g. here).

尝试通过添加

Try overwriting the target properties by adding

set_property(TARGET Qt5::Core PROPERTY INTERFACE_COMPILE_OPTIONS "-fpic")

在您调用 find_package(Qt5 ...)之后。

这篇关于用-fpic替换-fPIC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆