Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Makefile with source files in different directories

40 views
Skip to first unread message

Jinsong Zhao

unread,
Sep 2, 2012, 9:38:13 AM9/2/12
to
Hi there,

I hope to create a Makefile for compilation of a program with source
files in different directories. The source files are organized in:

project/module, project/interface, project/subroutine

The source files in second directory depends on the files in the first
one, and files in the third directory depends on the files in the first
and second one. In every directory, there are many files (more than 20),
in this situation, the use of wildcard might be convenient.

Is there any similar situation in your project? How do you organize it
using a Makefile?

Any suggestions will be greatly appreciated. Thanks in advance!

Regards,
Jinsong

dpb

unread,
Sep 2, 2012, 10:39:11 AM9/2/12
to
On 9/2/2012 8:38 AM, Jinsong Zhao wrote:
...

> Any suggestions will be greatly appreciated. Thanks in advance!
...

<http://www.gnu.org/software/make/manual/make.html>

--

Paul Anton Letnes

unread,
Sep 3, 2012, 7:53:21 AM9/3/12
to
There's a project which takes this use case seriously: CMake
(http://www.cmake.org/). With cmake, you don't even have to think about
this issue. Just:
<edit CMakeLists.txt>
mkdir build
cd build
cmake ..

Good luck
Paul


Jinsong Zhao

unread,
Sep 4, 2012, 1:25:14 AM9/4/12
to
Thank you for the link.

After several days efforts, I got a solution, however, I don't know if
it is the optimal ones.

In the top directory project/, there is a Makefile like this:

LDR = gfortran
LDOPTS =

obj = $(wildcard module/*.o)
obj += $(wildcard interface/*.o)
obj += $(wildcard subroutine/*.o)

all: app
$(LDR) $(LDOPTS) -o $< $(obj)

app:
cd module; make
cd interface; make
cd subroutine; make

mod:
cd module; make

if:
cd interface; make

sub:
cd subroutine; make

clean:
cd module; make clean
cd interface; make clean
cd subroutine; make clean
-rm MODULES/*.mod

In the subdirectory, e.g., interface, there is a Makefile like this:

FC = gfortran
FFLAGS = -g -pedantic -Wall -Wextra -fcheck=all -O0
MODULES = -J../MODULES

obj = $(patsubst %.f90, %.o, $(wildcard *.f90))

#all: libinterface.a
#
#libinterfaces.a: $(obj)
# ar ruvs $@ $^

all: $(obj)

%.o: %.f90
$(FC) $(FFLAGS) $(MODULES) -c $<

clean:
-rm *.o


Any suggestions for improvement? Thanks in advance.

Regards,
Jinsong

dpb

unread,
Sep 4, 2012, 9:24:14 AM9/4/12
to
On 9/4/2012 12:25 AM, Jinsong Zhao wrote:
> On 2012-09-02 22:39, dpb wrote:
>> On 9/2/2012 8:38 AM, Jinsong Zhao wrote:
>> ...
>>
>>> Any suggestions will be greatly appreciated. Thanks in advance!
>> ...
>>
>> <http://www.gnu.org/software/make/manual/make.html>
>>
...

> Thank you for the link.
>
> After several days efforts, I got a solution, however, I don't know if
> it is the optimal ones.
...

I'm certainly no whiz/expert on make; around here if it does the job
it's good! :)

--
0 new messages