# Include an fpm based library within a pre-existing/mixed language CMake project

**URL:** <https://fortran-lang.discourse.group/t/include-an-fpm-based-library-within-a-pre-existing-mixed-language-cmake-project/5023>\
**Category:** Help\
**Created:** [January 13, 2023, 8:24am UTC](https://fortran-lang.discourse.group/t/include-an-fpm-based-library-within-a-pre-existing-mixed-language-cmake-project/5023 "2023-01-13T08:24:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![hkvzjal](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/hkvzjal/32/3055_2.png) [@hkvzjal](https://fortran-lang.discourse.group/u/hkvzjal)\
**Post date:** [January 13, 2023, 8:24am UTC](https://fortran-lang.discourse.group/t/include-an-fpm-based-library-within-a-pre-existing-mixed-language-cmake-project/5023/1 "2023-01-13T08:24:36Z")

</div>

Hi, there might already be an answer to this but haven’t really found any explicit tutorial/examples:

So I’m working on a huge project base containing Fortran/C/C++ libraries. The project(s) contain several external dependencies, it builds intermediate libraries in either of the languages and at the end builds several packages. All of this is using CMake as the build system.

Now I’m wondering if I could (smoothly) introduce fpm to manage just one of the fortran projects having in mind that:

- This library project has upstream external dependencies
- It is a dependency for other libraries/executable downstream
- The same source code is used to build static libs (for compiling with the executable) and dynamic link libraries for creating a Python API (using ctypes)
- Local development is done on windows, using MVS2019 + Intel19 and WSL2+Intel for debugging linux. For deployment, everything is ran in a server that compiles the Windows and Linux distributions.

… So at the end of the day I have to be careful not to break out the house just to introduce a new package manager.

When looking at some of the tutorials/videos, I saw how to declare external dependencies using urls from github, but what about local dependencies that are not an fpm project

Q1: is there a way to at least declare the location of the upstream static/dynamic link libraries as dependencies? (is it safe to do so in fpm?)

Q2: Is there a way to include the build process within the pre-existing CMake build chain?

Thanks for any guidance on this

---

<div class="post-metadata">

**Author:** ![plevold](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/plevold/32/754_2.png) [@plevold](https://fortran-lang.discourse.group/u/plevold)\
**Post date:** [January 13, 2023, 2:41pm UTC](https://fortran-lang.discourse.group/t/include-an-fpm-based-library-within-a-pre-existing-mixed-language-cmake-project/5023/2 "2023-01-13T14:41:13Z")

</div>

I haven’t tried this, but I’ve been thinking about doing this as well. For just a single fpm project it should be possible to:

1. Use [FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html) to download the source
2. Create a custom target that runs fpm and build that project
3. Create an [imported library](https://cmake.org/cmake/help/latest/command/add_library.html#imported-libraries) that points at the static library that fpm built

This should work fine for a single dependency, but might get problematic for multiple projects: Say that you want to depend on fpm project _lib\_a_ and fpm project _lib\_b_ and both _lib\_a_ and _lib\_b_ depend on another fpm project _lib\_c_. If you add both with the procedure that I outlined above then fpm will build _lib\_c_ once for both _lib\_a_ and _lib\_b_. This will most likely lead to problems with the fact that you now have duplicate versions of _lib\_c_’s .mod files and its compiled functions.
