1

I have an external dependency (headers and shared libraries as well as version.txt file in a known install location) for my project. This dependency has no built-in support for CMake find_package(). After reading the find_package() docs and this SO answer, my understanding is that I should use module mode for find_package(). In the Find*.cmake cmake directory, my guess is that it should:

  1. Setup and verify paths to the header and shared libraries
  2. Read in the version file
  3. Define a project using the version file text
  4. Add an interface library target (${PROJECT_NAME})
  5. Add an alias to the interface library using a namespace (${MY_NAMESPACE})
  6. Add a library target for each shared library path
  7. Set the imported path property for each shared library
  8. Link the shared libraries to the interface library target
  9. Link the include directory to the interface library target
  10. export(TARGETS ${PROJECT_NAME} NAMESPACE ${MY_NAMESPACE}::)
  11. export(PACKAGE ${PROJECT_NAME})
  12. Profit?

Am I doing this right and/or taking the right approach?

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
Sean McVeigh
  • 569
  • 3
  • 18
  • On Stack Overflow we tend to resolve specific **problems**, **one per question post**. While your question have a real value, it doesn't perfectly fit to the Stack Overflow: it actually has **12** (well, 11) **questions** of type "if step XXX is needed". I would suggest you just to follow the steps you listed. Would you face with a problem, then you may ask about it here as about **specific** problem. Otherwise you may ask on [codereview.se] about whether you resulted code is good or not. – Tsyvarev Sep 27 '19 at 17:43
  • From me: drop 10 and 11 steps (`export` is for create files usable by `find_package`, but you are already in this mode). Also, you may initially create INTERFACE target with proper namespace, so further alias for it isn't needed. – Tsyvarev Sep 27 '19 at 17:46
  • @Tsyvarev Thanks for the tip. I'm new to CMake, so I wasn't even sure if I was doing it the right way. Based on your comments, I take it I was, but 10 & 11 unnecessary. It was hard for me as a newbie to figure out what to do from full 9-yard examples like https://github.com/nlohmann/json – Sean McVeigh Sep 27 '19 at 17:56
  • That project creates script for **CONFIG** mode of `find_package`. This is quite different from the **MODULE** mode you want to implement. – Tsyvarev Sep 27 '19 at 18:00

0 Answers0