[ale] [OT] Any Eclipse and C users on the list

Ryan Curtin ryan at igglybob.com
Fri Feb 1 12:58:46 EST 2013


On Fri, Feb 01, 2013 at 10:26:21AM -0500, Michael Potter wrote:
> Here is my question that I cannot find documented in CMake...
> 
> Is there a way to generate the dependencies from the source?
> 
> Specifically, if I have source that looks like this:
> 
> in dilbert.c:
> #include "src/alice.h"
> #include "src/wally.h"
> 
> Right now I have a hand coded bash script that generates
> obj/dilbert.o: src/dilbert.c src/alice.h src/wally.h
>    cc -o $@ src/dilbert.c
> 
> That way all I need to do is maintain the source and the tedious part
> of maintaining the makefile is done for me.   The bash script is
> sophisticated enough to handle recursion and ignoring some includes.
> As slick as it is, I want to get of this system and move to main
> stream scheme.
> 
> How does cmake eliminate that part of the makefile creation?

In general CMake does not do this.  You can set it up in different ways,
but the way I generally set up a project is to put either a
CMakeFiles.txt in each directory, or one CMakeFiles.txt somewhere, and a
command like

add_executable(the_program
  src/alice.h
  src/alice.c
  src/wally.h
  src/wally.c
  src/dilbert.c
  ...
)

but this does mean that every file you make in the project, you have to
add it to the list of compiled files.

I think that what you want to do is say something like
"add_executable(the_program main.c)" and then CMake looks through all
the #includes of main.c (recursively) to generate a list of files to be
compiled.

That is a complex problem and while it can be solved with CMake it would
be... somewhat backhanded.  You could write a script which would look
through each file in a list for #include lines using regexes, and then
somehow pruned that list to only those #includes which were relevant
(i.e. filter out #include <stdio.h> and similar).  Then you could call
that script again recursively with the new list.

There are a few gotchas in that idea, but it sounds like you already
have this system basically implemented in bash.  Converting it to a
CMake script is within the realm of possibility, if that's what you want
to do.  I've not seen a CMake project that does this, but if it works
and it's well-documented and maintainable, I don't see any problems with
it.

-- 
Ryan Curtin       | "Think!  What to do!"
ryan at igglybob.com |   - The Master


More information about the Ale mailing list