# CMake, Flang, and fPIC

**URL:** https://discourse.cmake.org/t/cmake-flang-and-fpic/15316
**Category:** Usage
**Created:** [November 17, 2025, 4:47pm UTC](https://discourse.cmake.org/t/cmake-flang-and-fpic/15316 "2025-11-17T16:47:03Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)
#### Post date: [November 17, 2025, 4:47pm UTC](https://discourse.cmake.org/t/cmake-flang-and-fpic/15316/1 "2025-11-17T16:47:03Z")

</div>

To the gurus here: Is there anything “special” I need to do to enable fPIC/fPIE with Flang?

I ask because this simple project:

```cmake
cmake_minimum_required(VERSION 3.24)
project(test LANGUAGES Fortran)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
add_executable(test test.F90)

```

doesn’t seem to do it for flang. To wit:

```bash
> cmake -B build -S . --install-prefix=$(pwd)/install --fresh
-- The Fortran compiler identification is LLVMFlang 22.0.0
-- Detecting Fortran compiler ABI info
-- Detecting Fortran compiler ABI info - done
-- Check for working Fortran compiler: /discover/nobackup/projects/gmao/SIteam/comp/SLES-15/llvm-flang/2025-10-14/bin/flang - skipped
-- Configuring done (10.9s)
-- Generating done (0.0s)
-- Build files have been written to: /home/mathomp4/LLVM-CMake-FPIC/build
> tail -n 2 build/CMakeFiles/test.dir/flags.make
Fortran_FLAGS =

```

But if I run with gcc 15 or ifx:

```bash
> tail -n 2 build/CMakeFiles/test.dir/flags.make
Fortran_FLAGS = -fPIE

```

or with NAG:

```bash
> tail -n 2 build/CMakeFiles/test.dir/flags.make
Fortran_FLAGS = -PIC

```

I tried staring at the source and I see that [`Flang-Fortran.cmake`](https://gitlab.kitware.com/cmake/cmake/-/blob/master/Modules/Compiler/Flang-Fortran.cmake?ref_type=heads) has an `include(Compiler/Clang)`, and [that file](https://gitlab.kitware.com/cmake/cmake/-/blob/master/Modules/Compiler/Clang.cmake?ref_type=heads) does have some PIC/PIE stuff in it., but it’s a bit more convoluted than, say, the [NAG-Fortran.cmake](https://gitlab.kitware.com/cmake/cmake/-/blob/master/Modules/Compiler/NAG-Fortran.cmake?ref_type=heads) file.

Is it just that LLVM is “different” and maybe PIC is on by default? Not sure.

I will note that flang seems to not dislike the flags:

```bash
> flang -fPIC test.F90
> echo $?
0
> flang -fPIE test.F90
> echo $?
0

```

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [November 17, 2025, 4:55pm UTC](https://discourse.cmake.org/t/cmake-flang-and-fpic/15316/2 "2025-11-17T16:55:18Z")

</div>

Take a look at the [CheckPIESupported](https://cmake.org/cmake/help/latest/module/CheckPIESupported.html) module.

---

<div class="post-metadata">

### Author: ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)
#### Post date: [November 17, 2025, 4:58pm UTC](https://discourse.cmake.org/t/cmake-flang-and-fpic/15316/3 "2025-11-17T16:58:52Z")

</div>

Well, I just added:

```cmake
message(CHECK_START "Checking for Fortran linker PIE support")

include(CheckPIESupported)
check_pie_supported(OUTPUT_VARIABLE output LANGUAGES Fortran)
set_property(TARGET test PROPERTY POSITION_INDEPENDENT_CODE TRUE)

if(CMAKE_Fortran_LINK_PIE_SUPPORTED)
  message(CHECK_PASS "yes")
else()
  message(CHECK_FAIL "no")
  message(VERBOSE "PIE is not supported at link time:\n${output}"
                  "PIE link options will not be passed to linker.")
endif()

```

and I get:

```bash
> cmake -B build -S . --install-prefix=$(pwd)/install --fresh
-- The Fortran compiler identification is LLVMFlang 22.0.0
-- Detecting Fortran compiler ABI info
-- Detecting Fortran compiler ABI info - done
-- Check for working Fortran compiler: /discover/nobackup/projects/gmao/SIteam/comp/SLES-15/llvm-flang/2025-10-14/bin/flang - skipped
-- Checking for Fortran linker PIE support
-- Checking for Fortran linker PIE support - no
-- Configuring done (7.4s)
-- Generating done (0.0s)
-- Build files have been written to: /home/mathomp4/LLVM-CMake-FPIC/build

```

So huh. `flang` supports `-fPIC` and or `-fPIE` on the command line, but not via CMake? 🤷‍♂️

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [November 17, 2025, 5:19pm UTC](https://discourse.cmake.org/t/cmake-flang-and-fpic/15316/4 "2025-11-17T17:19:34Z")

</div>

I have the feeling that there is a confusion between two compilers: You mention the file `Modules/Compiler/Flang-Fortran.cmake` but, from the log, I see that the compiler identification is `LLVMFLang`. The associated configuration file is `Modules/Compiler/LLVMFLang-Fortran.cmake`.

And this file does not set the expected variables for the `PIE` configuration…

Can you check the availability of variables `CMAKE_Fortran_COMPILE_OPTIONS_PIE`, `CMAKE_Fortran_LINK_OPTIONS_PIE` and `CMAKE_Fortran_LINK_OPTIONS_NO_PIE`?.

---

<div class="post-metadata">

### Author: ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)
#### Post date: [November 17, 2025, 5:31pm UTC](https://discourse.cmake.org/t/cmake-flang-and-fpic/15316/5 "2025-11-17T17:31:36Z")

</div>

Sigh. So many flang! 🙂

You are right [LLVMFlang-Fortran.cmake](https://gitlab.kitware.com/cmake/cmake/-/blob/master/Modules/Compiler/LLVMFlang-Fortran.cmake?ref_type=heads) is sort of on the level of the NAG File!

Well, I could make a PR with:

```cmake
set(CMAKE_Fortran_COMPILE_OPTIONS_PIC "-fPIC")
set(CMAKE_Fortran_COMPILE_OPTIONS_PIE "-fPIE")

```

Let me do some proving to myself that the flags work…

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [November 17, 2025, 5:36pm UTC](https://discourse.cmake.org/t/cmake-flang-and-fpic/15316/6 "2025-11-17T17:36:51Z")

</div>

For a correct configuration for `PIE`, use, for example, `Modules/Compiler/GNU.cmake` file (especially the variable `_CMAKE_Fortran_PIE_MAY_BE_SUPPORTED_BY_LINKER`).

---

<div class="post-metadata">

### Author: ![mathomp4](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mathomp4/32/176_2.png) [@mathomp4](https://discourse.cmake.org/u/mathomp4)
#### Post date: [November 17, 2025, 5:45pm UTC](https://discourse.cmake.org/t/cmake-flang-and-fpic/15316/7 "2025-11-17T17:45:26Z")

</div>

Well, I just tried to prove it’s needed and…I couldn’t. That is I didn’t get the usual:

```auto
relocation R_X86_64_32S against `.rodata' can not be used when making a shared object; recompile with -fPIC

```

like message I’ve grown to know and love.

So, huh. I wonder if LLVM Flang ‘automatically’ adds `-fPIC` like functionality underneath?

Heck, I just tried my “I think this should let me be confident” and forced it to use `-fno-PIC` and `-fno-PIE` thinking, “well, that’ll do it” and it also worked.

So maybe this is all moot? LLVM Flang is advanced enough now to not need PIC/PIE?

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [November 17, 2025, 6:44pm UTC](https://discourse.cmake.org/t/cmake-flang-and-fpic/15316/8 "2025-11-17T18:44:20Z")

</div>

Maybe, by default, `PIC` and `PIE` are activated. But are these options supported by the compiler/linker? As well as the option to deactivate `PIE` at link step.
