[Makefile]How can I deal with multi-dirs?

M

M.Liang Liu

I have a project with the following dirs:
---------------------------------------------------------------------------------------
+src
|-proj0
|-program1
|-program2
|-proj1
|-program1
|-program2
|-program3
|-program4
|-proj3
|-program1
|-program2
|-program3
....
|-proj9
-Makefile
+include
-README
+lib
|-lib1
|-lib2
---------------------------------------------------------------------------------------
There are 10 project-dirs here in src, named proj0-proj9. Each project
has several program_dirs which containing some .c files
All .h files are under include in the top dir, the same as src.

I would like to write a makefile, which can help me compile all the .c
files and generate an executable file for each *program*. Considering
there are several independent programs in each project, the work
should be done within a single makefile at the top dir.

How can I deal all the programs under each project in a single top-
level makefile?

Thanks!
 
A

Antoninus Twink

I have a project with the following dirs:
---------------------------------------------------------------------------------------
+src
|-proj0
|-program1
|-program2
|-proj1
|-program1
|-program2
|-program3
|-program4
|-proj3
|-program1
|-program2
|-program3
....
|-proj9
-Makefile
+include
-README
+lib
|-lib1
|-lib2
---------------------------------------------------------------------------------------
There are 10 project-dirs here in src, named proj0-proj9. Each project
has several program_dirs which containing some .c files
All .h files are under include in the top dir, the same as src.

I would like to write a makefile, which can help me compile all the .c
files and generate an executable file for each *program*. Considering
there are several independent programs in each project, the work
should be done within a single makefile at the top dir.

How can I deal all the programs under each project in a single top-
level makefile?

Which part of the section of your make program's documentation on
recursive use of make didn't you understand?
 
F

fnegroni

Which part of the section of your make program's documentation on
recursive use of make didn't you understand?

I think this is OT. comp.unix.programmer might be a better place to
ask this question.

I believe recursive makefiles have serious flaws and should be
avoided.

In this case, if each project is standalone, each needs a Makefile and
then you might want a script to run all the makefiles in sequence.

If the project are interlinked, the one makefile would be enough, just
use relative paths in your dependencies.

Also, a better source code file structuring also helps: I would place
all source files together in the same directory. After all, if they
are interrelated, what would be the advantage in splitting them about?

But that's my opinion. Yours may differ.
 
R

Rui Maciel

fnegroni said:
In this case, if each project is standalone, each needs a Makefile and
then you might want a script to run all the makefiles in sequence.

I do believe that the best way is to simply rely on an automatic makefile
generator. For example, automake does a wonderful job at this multi-dir
project stuff.

But yes, it's OT.


Rui Maciel
 
R

Rui Maciel

fnegroni said:
In this case, if each project is standalone, each needs a Makefile and
then you might want a script to run all the makefiles in sequence.

I do believe that the best way is to simply rely on an automatic makefile
generator. For example, automake does a wonderful job at this multi-dir
project stuff.

But yes, it's OT.


Rui Maciel
 
A

Antoninus Twink

I think this is OT. comp.unix.programmer might be a better place to
ask this question.

That seems unlikely, given that the OP appears to be posting from a
Windows box.
I believe recursive makefiles have serious flaws and should be
avoided.

I believe you've read some stupid polemic about recursive Makefiles
having serious flaws, and have bought into it hook line and sinker.
In this case, if each project is standalone, each needs a Makefile and
then you might want a script to run all the makefiles in sequence.

A "script", eh? But definitely not a top-level Makefile?
If the project are interlinked, the one makefile would be enough, just
use relative paths in your dependencies.

That would be one way, but then you end up with one incredibly complex
Makefile. Why not split the Makefile, for exactly the same reason that
you separate out the source files into different directories?
Also, a better source code file structuring also helps: I would place
all source files together in the same directory. After all, if they
are interrelated, what would be the advantage in splitting them about?

But that's my opinion. Yours may differ.

It sounds like the OP's project consists of one or more libraries, and
then some number of independent executables that link against these
libraries. Separating the source files for each executable into its own
directory seems perfectly reasonable to me.
 
F

fnegroni

I believe you've read some stupid polemic about recursive Makefiles
having serious flaws, and have bought into it hook line and sinker.

Maybe.


A "script", eh? But definitely not a top-level Makefile?

A Makefile to do what a script can do perfectly well? Why?
How does a top level makefile improve the situation. Surely it is the
individual makefile that knows whether a target needs recompiling or
not. Why would a top level Makefile do a better job than a script?
It sounds like the OP's project consists of one or more libraries, and
then some number of independent executables that link against these
libraries. Separating the source files for each executable into its own
directory seems perfectly reasonable to me.

Sure, but then why having recursive makefiles? If each project is
standalone, whether a library or executable, each Makefile is self
contained. Certainly not split.
If the different sources for the *same* executable were split across
directories, then a recursive makefile would save the use of relative
paths, but that's where IMHO it would be better to consolidate all
source directories for the same tool into one.
If one module is shared across two executable, shouldn't that be a
static library? I thought that's what they were meant for.
 
M

Mingliang Liu

That seems unlikely, given that the OP appears to be posting from a
Windows box.


I believe you've read some stupid polemic about recursive Makefiles
having serious flaws, and have bought into it hook line and sinker.


A "script", eh? But definitely not a top-level Makefile?


That would be one way, but then you end up with one incredibly complex
Makefile. Why not split the Makefile, for exactly the same reason that
you separate out the source files into different directories?



It sounds like the OP's project consists of one or more libraries, and
then some number of independent executables that link against these
libraries. Separating the source files for each executable into its own
directory seems perfectly reasonable to me.

=======================Just see here!===============================

Hi, thanks for ur reply.

I am, u have seen, a freshman to Makefile and have read some
documentation without enough experience.
And I'm wondering how can I do a job gracefully if all the programs of
each project are almost independent?
Namelly, i can write a makefile for each program (so it should work
with recursive Makefiles).But the question is that I would like to
avoid copy the same Makefile for each program --- suppose my teacher
tell me that the gcc should have a O3 optimazition and then I should
modify every Makefile one by one.

Any hints?

Thanks!
 
A

Antoninus Twink

=======================Just see here!===============================

Hi, thanks for ur reply.

I am, u have seen, a freshman to Makefile and have read some
documentation without enough experience.
And I'm wondering how can I do a job gracefully if all the programs of
each project are almost independent?
Namelly, i can write a makefile for each program (so it should work
with recursive Makefiles).But the question is that I would like to
avoid copy the same Makefile for each program --- suppose my teacher
tell me that the gcc should have a O3 optimazition and then I should
modify every Makefile one by one.

Any hints?

Your version of make will have built-in implicit rules: for example, GNU
make will create x.o from x.c using the command

$(CC) -c $(CPPFLAGS) $(CFLAGS) x.c

So the right thing to do is add any flags you want (like -O3) to the
CFLAGS variable in make.

If you're running make recursively, you'll need to read the
documentation to find out how to pass on your variables' values to
sub-makes. In GNU make again, you'd do something like

export CFLAGS=-O3
 
F

Flash Gordon

Mingliang Liu wrote, On 01/04/08 18:28:

Hi, thanks for ur reply.

Please start by not using txt spk. The word you wanted is "your" not
"ur" and these stupid contractions make it far harder to to read
especially for those to whom English is not a first language (and there
are a number of such people here).
I am, u have seen, a freshman to Makefile and have read some
documentation without enough experience.

Any hints?

Yes, ask somewhere that makefiles are topical rather than in comp.lang.c
as you have already been advised. comp.unix.programmer may be one such
place, but you should check the group before posting to find out if it
is topical. Checking does not merely involve seeing if someone has
posted on the topic, but also checking the rest of the thread to find
out if the poster was told it was off topic! Also check the FAQ for the
group which you should be able to find using Google.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,754
Messages
2,569,525
Members
44,997
Latest member
mileyka

Latest Threads

Top