Cmake不能在makefile中添加"-fPIE "标志。

问题描述 投票:0回答:1

我试图编译嵌入了python的C++代码,尽管我在CMakeList.txt中添加了'-fPIE'标志,但编译器并没有编译我的代码。尽管我在CMakeList.txt中添加了'-fPIE'标志,但编译器并没有编译我的代码。这是我的CMakeList。

cmake_minimum_required(VERSION 3.3)
project(TestCython)

add_executable(main main.cpp)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-result -Wsign-compare -g -fdebug-prefix-map=/build/python3.8-6QL2k7/python3.8-3.8.2=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector -Wformat -Werror=format-security  -DNDEBUG -g -fwrapv -O3 -Wall -fPIE")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-unused-result -Wsign-compare -g -fdebug-prefix-map=/build/python3.8-6QL2k7/python3.8-3.8.2=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector -Wformat -Werror=format-security  -DNDEBUG -g -fwrapv -O3 -Wall -fPIE")

# Eigen
find_package(Eigen3 3.3 REQUIRED NO_MODULE)

# Embedded Python Includes
include_directories(/usr/include/python3.8
                    /usr/include/eigen3/    )

# Embedded Python Linker
link_directories(/usr/lib/python3.8/config-3.8-x86_64-linux-gnu
                 /usr/lib)

target_link_libraries(  main
            python3.8
            crypt
            pthread
            dl
            util
            m   )       

set(CMAKE_C_COMPILER gcc)
set(CMAKE_CXX_COMPILER g++)

如果我运行生成的make文件,它会报告很多类似的错误,并要求我用'-fPIE'重新编译,比如这个。usrbinld: usrlibgccx86_64-linux-gnu9......x86_64-linux-gnulibpython3.8.a(call.o): 重置R_X86_64_32对`.rodata.str1.8'不能用于制作PIE对象;用-fPIE重新编译。

如果我不使用cmake,编译成功。

g++ -I/usr/include/python3.8  -Wno-unused-result -Wsign-compare -g -fdebug-prefix-map=/build/python3.8-6QL2k7/python3.8-3.8.2=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector -Wformat -Werror=format-security  -DNDEBUG -g -fwrapv -O3 -Wall -fPIE main.cpp -o main -L/usr/lib/python3.8/config-3.8-x86_64-linux-gnu -L/usr/lib -lpython3.8 -lcrypt -lpthread -ldl  -lutil -lm

Cmake版本是3.16.3. Python版本是3.8.2。系统是Ubuntu 20.04如何解决这些错误?

python c++ linux cmake g++
1个回答
0
投票

请注意,大多数情况下,这个错误与以下问题无关 PIE 标志,而是你的构建过程中出现了其他的问题,不幸的是,由于你的CMake配置有点问题,所以很难判断。

这个特殊的错误可能是由于你的属性设置不正确;在设置 CMAKE_CXX_FLAGS 和类似的变量,如果你这样做,什么都不做。之后 通过以下方式确定你的目标 add_executable. 这完全可以通过使用更现代的CMake风格来避免(不在本回答的范围内,只需在谷歌上搜索 现代CMake 的一些例子),在这种特殊的情况下,我们不需要在这些变量上设置标志,而是可以做以下操作

target_compile_options(main PRIVATE -fpie)
© www.soinside.com 2019 - 2024. All rights reserved.