0

A library foo is to be built either as a project of its own, or as part of a larger project bar. For the latter, I found no better solution than line (*):

$ cat foo/lib/CMakeLists.txt
...
set(foo_LIBRARY foo PARENT_SCOPE)
...

$ cat foo/CMakeLists.txt
...
add_subdirectory(lib)
set(cerf_LIBRARY ${cerf_LIBRARY} PARENT_SCOPE)  # (*)
...

$ cat bar/CMakeLists.txt
...
add_subdirectory(link-to-foo)
...

Now building bar works. But when building only foo, I get

CMake Warning (dev) at CMakeLists.txt:30 (set):
  Cannot set "foo_LIBRARY": current scope has no parent.

Aiming for zero warnings in my projects, I'd need a better solution.

Joachim W
  • 7,290
  • 5
  • 31
  • 59
  • 1
    Not sure why you say about "grandparent", but for check whether *parent* exists see e.g. this question: https://stackoverflow.com/questions/25199677/how-to-detect-if-current-scope-has-a-parent-in-cmake. – Tsyvarev Sep 29 '18 at 20:16
  • Indeed, that could be used to solve my problem, thank you. But shouldn't be there a simpler solution, e.g. by making `foo_LIBRARY` known at global scope? – Joachim W Oct 01 '18 at 07:22
  • 1
    "... making foo_LIBRARY known at global scope" - Just create CACHE variable: `set(foo_LIBRARY <...> CACHE INTERNAL "")`. After that, the variable can be dereferenced in any time. BTW, there is a question about that: https://stackoverflow.com/questions/10031953/how-to-set-the-global-variable-in-a-function-for-cmake. – Tsyvarev Oct 01 '18 at 08:05
  • Which was not in the accepted answer. Which I therefore edited. Would be nice if you could check. – Joachim W Oct 01 '18 at 10:27

0 Answers0