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 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:
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:
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 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.