Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does CMake export() command defer the output file creation? Can it be avoided?

Tags:

cmake

We are including separate repositories into a common CMake superproject (which simply add_subdirectory() each repository folder, in the correct dependency order).

The different repositories all rely on export() to ouptut a XxxTargets.cmake file, such as:

# Assumes this repository provide the 'Alpha' target
install(TARGETS Alpha EXPORT AlphaTargets)

export(EXPORT $AlphaTargets
       FILE $AlphaTargets.cmake
       NAMESPACE myrepo::)

Yet, this superproject approach allowed us to observe that the export does not create the file directly. We could draw this conclusion because when the generated file is include() from another repository (added later, with a subsequent add_subdirectory()), the file does not exist yet.

  • Why is export() delaying the file creation?
  • And more importantly, is there a way to force it to actually create the file before the next add_subdirectory()?
like image 508
Ad N Avatar asked Jan 19 '26 21:01

Ad N


1 Answers

  • Why is export() delaying the file creation?

Because you are asking it to generate code for a particular export set, which isn't fully defined until the configure step ends. If it generated it immediately, then the behavior of export() and install(EXPORT) would be vexing and inconsistent.

  • And more importantly, is there a way to force it to actually create the file before the next add_subdirectory()?

The export(TARGETS) signature runs immediately as far as I know. But I also can't think of a single reason why you would need the generated exports file when working with add_subdirectory. The targets already exist, so there's no need to import them! If the names are different, create ALIAS targets as needed.

like image 53
Alex Reinking Avatar answered Jan 22 '26 15:01

Alex Reinking