Makefile in Unix environment: help

  • Thread starter Thread starter nrqed
  • Start date Start date
  • Tags Tags
    Unix
Click For Summary
The discussion revolves around understanding specific lines in a Makefile used for compiling Fortran programs. The line "glue: gluon_operators.o" defines a target called "glue" that depends on the object file "gluon_operators.o," meaning it will be built if that file is updated. The line "gluon_operators.o: gluon_operators.f90 loop_functions.o $(GLOBALS)" specifies that "gluon_operators.o" depends on the source file "gluon_operators.f90" and other object files, indicating the order of compilation. The command "$(COMP) $(FLAGS) -o main_program *.o" compiles all object files into the executable "main_program," with "*.o" representing all object files in the directory. The discussion concludes with a reference link for further clarification on Makefile rules.
nrqed
Science Advisor
Messages
3,762
Reaction score
297
NEVER MIND! I finally found a good reference explaining all of this. Sorry for the wasted bandwidth!

Patrick



I have this makefile I am trying to understand.

It goes like this:

COMP = ifort
FLAGS = -autodouble etc...
GLOBALS = a bunch of .o files...


glue: gluon_operators.o
$(COMP) $(FLAGS) -o main_program *.o

gluon_operators.o: gluon_operators.f90 loop_functions.o $(GLOBALS)
$(COMP) $(FLAGS) -c gluon_operators.f90


and so on...

I understand that this is compiling stuff using the Fortran compiler with some flags.

What I am confused about are the lines with a ":", for example

glue: gluon_operators.o

I have no idea what this line does.
Also, the line


gluon_operators.o: gluon_operators.f90 loop_functions.o $(GLOBALS)


is a mystery to me. What does the ":" do?

Also, I don't understand the line with main_program. It seems to me that the -o says to output the result of the compilation to main_program, but there does not seem to be anything compiled in the first place ?! I would have expected $(COMP) $(FLAGS) program.f90 -o main_program or something similar.

Also, what does the "*.o" mean at the end of that same line? It says something about all files ending with .o but I don't quite understand the whole line. Maybe it says that anything that will be compiled next must
put together in the file main_program??


I would really appreciate help!

Patrick
 
Last edited:
Computer science news on Phys.org
For others who have the same question, I found this reference -- https://www.tutorialspoint.com/makefile/makefile_quick_guide.htm
Defining Rules in Makefile

We will now learn the rules for Makefile.

The general syntax of a Makefile target rule is −
target [target...] : [dependent ...]
[ command ...]

In the above code, the arguments in brackets are optional and ellipsis means one or more. Here, note that the tab to preface each command is required.

A simple example is given below where you define a rule to make your target hello from three other files.
hello: main.o factorial.o hello.o
$(CC) main.o factorial.o hello.o -o hello

NOTE − In this example, you would have to give rules to make all object files from the source files.

The semantics is very simple. When you say "make target", the make finds the target rule that applies; and, if any of the dependents are newer than the target, make executes the commands one at a time (after macro substitution). If any dependents have to be made, that happens first (so you have a recursion).
 
I’ve spent nearly my entire life online, and witnessed AI become integrated into our lives. It’s clear that AI is apart of us now whether we like it or not, unless your a anti tech cabin lover. AI has some form of control over your life. But what I’ve seen very recently is that people are loosing their ingenuity and deciding to use AI. I feel as if it’ll bleed into STEM which is kinda has already and, every idea or thought could become fully reliant on AI. Yeah AI makes life easier but at a...

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 34 ·
2
Replies
34
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 34 ·
2
Replies
34
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
1
Views
4K
  • · Replies 12 ·
Replies
12
Views
2K