I try to use add_subdirectory if I’m referencing a CMakeLists.txt and use include when I’m pulling in a *.cmake file. There is a difference between include and add_subdirectory, so it would depend on your use case which would be better.
As an aside, be aware of the pitfalls of file(GLOB) for defining source files. See file glob documentation. Be sure to understand the tradeoffs when trying to use it that way.
In this situation where you aren’t looking to create another library, you can just use the target defined in the parent CMakeLists.txt, the executable MyTarget in this case. As long as the target has been defined, you can add additional sources to it with target_sources. So no, there’s no local target you have to worry about.
You likely could define a separate target to contain the sources if you wanted, but I would imagine that’s only interesting in situations where you want to apply those sources to multiple targets. This is likely more complicated than what you’re looking for, but leverages the same target_sources command.
If instead you are looking to organize files and are using a generator that supports filters, such as Visual Studio, you can always use a source_group to group stuff in other_src into a separate filter.
Thanks @dnglaze . This should help me a little in accepting what the “normal” way would be for doing what we’re talking about.
I used to use Visual Studio 2010 (and earlier) all the time so yes, the concept of a filter (or “folder” as I preferred to call them) made sense under the main source tree. So source_group may be worth reading about.
It’s actually or the IDF ESP32 environment that I’m wanting to understand CMake with. IDF has a few concepts of it’s own (they call them “components”) but I figured it would be best that I understand vanilla CMake first - especially when I see it being used increasingly in all aspects of work and hobbies that I’m exposed to these days.
beware: the answer of @dnglaze has just a little bug: the ${other_SRC} is missing an “S” at the end, it should be: ${other_SRCS} to match the variable declaration on the previous line.