PIE link flags are not used when POSITION_INDEPENDENT_CODE is ON without check_pie_supported being called (CMP0083 is set to NEW)
Documentation to CMP0083 says that when it's set to NEW, POSITION_INDEPENDENT_CODE option should control usage of PIE link falgs (e.g. -pie
, -no-pie
on GCC). But when I set the POSITION_INDEPENDENT_CODE
to FALSE
, the -no-pie
option is not added until I call the check_pie_supported
.
Since a given linker may not support PIE flags in all environments in which it is used, it is the project's responsibility to use the CheckPIESupported module to check for support to ensure that the POSITION_INDEPENDENT_CODE target property for executables will be honored at link time.
From the doc I only understand that check_pie_supported
is just a helper for users to be able to check whether the PIE flags are supported or not in the current environment. It's not clear that calling it will change the behavior.
cmake_minimum_required(VERSION 3.14)
project(pie_test)
include(CheckPIESupported)
#check_pie_supported() # uncomment this line to add `-no-pie` to the link flags
add_executable(pie_test test.cpp)
set_property(TARGET pie_test PROPERTY POSITION_INDEPENDENT_CODE FALSE)
int main() { return 0; }
Tested on Ubuntu 22.04, cmake version 3.22.1, gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0.
Note that issue #18955 (closed) mentions in several comments - e.g. #18955 (comment 520102) that check_pie_supported
is required to be called.