# Consistent linking option using shared libraries within FindMPI.

**URL:** https://discourse.cmake.org/t/consistent-linking-option-using-shared-libraries-within-findmpi/14249
**Category:** Development
**Created:** [June 17, 2025, 2:33pm UTC](https://discourse.cmake.org/t/consistent-linking-option-using-shared-libraries-within-findmpi/14249 "2025-06-17T14:33:12Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![green-br](https://discourse.cmake.org/user_avatar/discourse.cmake.org/green-br/32/5619_2.png) [@green-br](https://discourse.cmake.org/u/green-br)
#### Post date: [June 17, 2025, 2:33pm UTC](https://discourse.cmake.org/t/consistent-linking-option-using-shared-libraries-within-findmpi/14249/1 "2025-06-17T14:33:12Z")

</div>

When building with Cray MPICH it seems to default is to us a static MPI library. I have in the project set `BUILD_SHARED_LIBS` so my resulting library is shared but since the Cray MPICH compiler is building statically it seems inconsistent in FindMPI looking for static library. Should FindMPI check the `BUILD_SHARED_LIBS` and add the required option to `MPI_COMPILER_FLAGS` to make this consistent. If I do this manually on the cmake command line with `-DMPI_COMPILER_FLAGS=-shared` it finds the shared MPI library and everything works and picks up the required dependencies via environment variables pointing to other shared library locations.

At the moment with the default finding of static MPI library it requires extra libraries to be passed to it at link stage since the output from `mpicc -show` doesn’t provide all the libraries it requires (in this case libfabric and librt). This will be reported to HPE.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [June 18, 2025, 6:13am UTC](https://discourse.cmake.org/t/consistent-linking-option-using-shared-libraries-within-findmpi/14249/2 "2025-06-18T06:13:11Z")

</div>

I’m not particularly familiar with the FindMPI module, but in general, `BUILD_SHARED_LIBS` is only relevant for things built by your project. Find modules are generally finding already-built things, so they should pretty much ignore `BUILD_SHARED_LIBS`.

Instead, some Find modules might have their own controls for preferring static over shared, or projects might use multiple calls to `find_package()` with different `PATHS`, `HINTS`, or `NAMES` and various `NO_...` options to convince a find module to prefer finding static over shared. You’d have to go through the documentation of the FindMPI module to see if there’s a formally supported way to achieve the outcome you are looking for.

I know that’s not a very satisfying answer, but I hope at least I’ve answered the `BUILD_SHARED_LIBS` part for you.

---

<div class="post-metadata">

### Author: ![green-br](https://discourse.cmake.org/user_avatar/discourse.cmake.org/green-br/32/5619_2.png) [@green-br](https://discourse.cmake.org/u/green-br)
#### Post date: [June 18, 2025, 8:07am UTC](https://discourse.cmake.org/t/consistent-linking-option-using-shared-libraries-within-findmpi/14249/3 "2025-06-18T08:07:45Z")

</div>

Thanks. My thoughts were that if my project is wanting to build a shared library should it not also check that its dependencies are shared? I see other modules such as FindwxWidgets and FindCUDA using BUILD\_SHARED\_LIBS as a proxy for whether to link to shared or static version of the libraries. At least there is a way to specify the flag for MPI but consistent behaviour for modules would be nice.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [February 20, 2026, 4:51am UTC](https://discourse.cmake.org/t/consistent-linking-option-using-shared-libraries-within-findmpi/14249/4 "2026-02-20T04:51:42Z")

</div>

Find modules should generally have their own controls for that (e.g., `Boost_USE_STATIC_LIBS`). While consistent static/shared modes are the most common, it isn’t universal.
