Including functions/macros with target export ?


I am able to get export(TARGET …) working and have generated working *Config.cmake file

There are some cmake macros and functions that I would like to include for the convenience of the developers using the package.

How should I go about including/deploying them so that once a developer call find_package(mypackage …), those functions/macros will be available to them ?


Do not get your export(TARGET...) command to write to *Config.cmake directly (or install(TARGETS...) for that matter). Instead, make those commands write their output to a file that your own hand-written *Config.cmake file will pull in with an include() call. Your hand-written *Config.cmake file can then define whatever functions and macros it needs to. For example:

export(TARGETS ... FILE MyProjTargets.cmake ...)

Then your hand-written MyProjectConfig.cmake might do something like this:


# Define whatever functions and macros you want here too

In the above, I’ve provided a direct answer to your question. But the use of export(TARGETS) is not all that common. Are you trying to get another build to re-use your project’s build tree directly (i.e. without installing it)? If not, then export(TARGETS) is probably not the right way to go about whatever you’re trying to do. It is much more typically to be using install(TARGETS ... EXPORT ...) and then install(EXPORT ...).

1 Like

Thank you for the insight. I will apply your recommendations.