# Is it appropriate to use enable\_language in a CMake package file?

**URL:** <https://discourse.cmake.org/t/is-it-appropriate-to-use-enable-language-in-a-cmake-package-file/4335>\
**Category:** Code\
**Created:** [October 25, 2021, 10:55am UTC](https://discourse.cmake.org/t/is-it-appropriate-to-use-enable-language-in-a-cmake-package-file/4335 "2021-10-25T10:55:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![szhorvat](https://discourse.cmake.org/user_avatar/discourse.cmake.org/szhorvat/32/1906_2.png) [@szhorvat](https://discourse.cmake.org/u/szhorvat)\
**Post date:** [October 25, 2021, 10:55am UTC](https://discourse.cmake.org/t/is-it-appropriate-to-use-enable-language-in-a-cmake-package-file/4335/1 "2021-10-25T10:55:53Z")

</div>

We have a library that has a pure-C interface, but is partially written in C++ and uses OpenMP. We generate a [CMake package file](https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html) for this library, so other CMake projects can easily use it.

However, when a C language project tries to use our library, CMake shows an error like this:

```plaintext
  Target "test_prog" links to target "OpenMP::OpenMP_CXX" but the target
  was not found. Perhaps a find_package() call is missing for an IMPORTED
  target, or an ALIAS target is missing?

```

This happens only if `test_prog`’s `CMakeLists.txt` contains:

```auto
project(test_prog C)

```

I.e., it happens when only the C language is enabled, not C++. If the dependent project uses `project(test_prog C CXX)` or simply `project(test_prog)`, then everything works fine.

However, users of our library are often pure-C projects, and they have no way to know that they are supposed to enable C++ support in CMake. To make things more confusing, out library can be compiled without OpenMP support, in which case this problem doesn’t even appear. Is there a solution to the problem that does not require our users to explicitly enable C++ support?

After some experimentation, I see that I can solve this problem as follows: I find the `find_package(OpenMP)` line in the `ourlib-config.cmake` CMake package file, and add `enable_language(CXX)` beforehand.

**Question:** Is this the correct solution, i.e. adding `enable_language(CXX)` to our package file?

The [CMake documentation](https://cmake.org/cmake/help/latest/command/enable_language.html) states:

> This command must be called in file scope, not in a function call. Furthermore, it must be called in the highest directory common to all targets using the named language directly for compiling sources or indirectly through link dependencies. It is simplest to enable all needed languages in the top-level directory of a project.

This gives me pause. It sounds like `enable_language` should not be used in a package file.

---

<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:** [October 25, 2021, 9:53pm UTC](https://discourse.cmake.org/t/is-it-appropriate-to-use-enable-language-in-a-cmake-package-file/4335/2 "2021-10-25T21:53:24Z")

</div>

I would recommend only _testing_ whether a language is enabled in your config package file, not actually enabling a language. If your package requires a language that isn’t enabled, you can halt with a fatal error informing the user they need to enable the language in the top level of the project. You can get the set of enabled languages with the [ENABLED\_LANGUAGES](https://cmake.org/cmake/help/latest/prop_gbl/ENABLED_LANGUAGES.html) global property.

Enabling languages in subdirectories rather than at the top level can have some obscure problems. I don’t recall what those are off the top of my head, but last time I looked into what the CMake code does around this, I concluded that you really don’t want to enable a language anywhere other than the very top directory scope (i.e. the top level `CMakeLists.txt` file).

---

<div class="post-metadata">

**Author:** ![leha-bot](https://discourse.cmake.org/user_avatar/discourse.cmake.org/leha-bot/32/921_2.png) [@leha-bot](https://discourse.cmake.org/u/leha-bot)\
**Post date:** [March 13, 2025, 9:39am UTC](https://discourse.cmake.org/t/is-it-appropriate-to-use-enable-language-in-a-cmake-package-file/4335/3 "2025-03-13T09:39:06Z")

</div>

Accidentally found this topic, and want to clarify that CMake 4.1 will ban `enable_language()` in CMake package files: [https://gitlab.kitware.com/cmake/cmake/-/issues/26751](https://gitlab.kitware.com/cmake/cmake/-/issues/26751)

---

<div class="post-metadata">

**Author:** ![szhorvat](https://discourse.cmake.org/user_avatar/discourse.cmake.org/szhorvat/32/1906_2.png) [@szhorvat](https://discourse.cmake.org/u/szhorvat)\
**Post date:** [March 17, 2025, 8:22am UTC](https://discourse.cmake.org/t/is-it-appropriate-to-use-enable-language-in-a-cmake-package-file/4335/4 "2025-03-17T08:22:22Z")

</div>

Thanks for the heads up! We followed Craig’s advice and are only checking whether CXX is enabled in the package file.
