0

I am trying to create a makefile but facing some issues. I installed gcc compiler in Windows 7, then created one simple helloworld example. After that compiled that C file using the following command:

gcc filename.c

After this I am getting an exe file. I am calling this project in some tool for that, tool required makefie.

As I understood makefile is a text file, which tells or consist some commands how to build, run and clean the project.

So according to this I am writing a makefile:

CC=gcc

SRCS=src/hello.c

.PHONY: all
all: clean build
    @echo ========== Complete ==========

.PHONY: build
build: 
    @echo ========== Build ==========
    $(CC) hello.c

.PHONY: run
run: 
    @echo ========== Run ==========
    make

.PHONY: clean
clean:
    @echo ========== Clean ==========
    rm hello.exe

./obj:
    mkdir ./obj

While calling this simple project in tool, getting error "no rule to make target clean"

Please tell me which steps I followed those are correct for creation of makefile or not, and what mistake I am doing? How to create a makefile?

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
Asha
  • 21
  • 6
  • Why do you use DOS? It is an obsolete operating system! Can't you install [Debian](http://debian.org/) on your laptop? You could use [GNU emacs](https://www.gnu.org/software/emacs/) to create your `Makefile` (for [GNU make](https://www.gnu.org/software/make/)...). With [GCC](http://gcc.gnu.org/), please compile using `gcc -Wall -Wextra -g` then use [GDB](https://www.gnu.org/software/gdb/) to understand the behavior of your executable – Basile Starynkevitch Jan 21 '21 at 06:31
  • See also [this](https://stackoverflow.com/questions/21537265/choose-between-makefile-and-makefile-on-linux/21537332) and [that](https://stackoverflow.com/questions/8025766/makefile-auto-dependency-generation/8025816) answers. – Basile Starynkevitch Jan 21 '21 at 06:36
  • 4
    Are you really using DOS? – jwdonahue Jan 21 '21 at 06:36
  • Thank you very much for the replay, please clear me,we can't create makefile in windows 7?we need to use debin or linux only? – Asha Jan 21 '21 at 06:41
  • Are both files in the same folder? Is makefile in the right folder for the tool you are using? – radrow Jan 21 '21 at 07:29
  • Of course it is possible to use the tool "make" with a makefile to build executables on Windows. (BTW, you are still using Windows 7? For your own safety, please use an up-to-date OS.) -- Your example works for me (GCC, Win10), except that "rm" is not a Windows command. Please [edit] your question and show us how you call make and the resulting output. – the busybee Jan 21 '21 at 14:12

1 Answers1

0

In my opinion you have not got the essence of make(1):

  • Make stores in the makefiles a set of dependency rules (dependencies between files) in your build directory in order to build your project.
  • There are dependency lines, and build lines, the dependencies start in column 0 of the line, while the buid lines start with a tab char.
  • the rule lines have two parts, the file that is to be built, a colon (:), and the list of files it depends on (so that if one or more of these files is modified, the rule is applied)
  • If the rule has to be applied, then the set of build lines below the rule (until the next rule or a variable definition rule if found) is executed in order to build the file.

Example

Your file hello.c will be compiled into hello.s to create an assembler file, and then the assembly code is assembled to generate an object code hello.o. Finally, this file is linked to generate the file hello (or hello.exe, if you are in windows).

You arrange your makefile to generate all the files, in a way that if you modify e.g. the assembler file hello.s, only the assembler pass, and the linker pass is done, but not the compiling phase that should overwrite the assembler file before assembling. This can be done with this Makefile:

# this is the linking phase.  The first rule in the file is the
# default target rule, so by default, executing make will try this
# rule (but only if hello.exe was modified before hello.o)

hello.exe: hello.o
    gcc -o hello.exe hello.o

# Now, the assembling phase.  The hello.o file depends on the
# hello.s assembly code, so to assemble it we call the assembler

hello.o: hello.s
    as -o hello.o hello.s

# now, we specify the dependency from the hello.s assembler file
# from the hello.c source code file.

hello.s: hello.c
    gcc -c -S -o hello.s hello.c

Now, if it is the first time you execute make and you have only the file hello.c (and Makefile of course) the make program will generate the following sequence of commands:

$ make
gcc -c -S -o hello.s hello.c
as -o hello.o hello.s
gcc -o hello.exe hello.o
$ _

but if you later modify the file hello.s (I will touch(1) it, to change its modification date:

$ touch hello.s
$ make
as -o hello.o hello.s
gcc -o hello.exe hello.o
$ _

but if you touch hello.c, everything will be made again:

$ touch hello.c
$ make
gcc -c -S -o hello.s hello.c
as -o hello.o hello.s
gcc -o hello.exe hello.o
$ _

Make builds a dependency graph and follows it in order to build the target you have specified in the command line, so if you use make with a target, it will stop as soon as the target is built:

$ make hello.o
gcc -c -S -o hello.s hello.c
as -o hello.o hello.s
$ _

I recommend you to read a book on make. A good one is the GNU Make documentation that is online on your system as an info file: just execute:

$ info make

(and info will open a text screen to allow you to read the full documentation of make)

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31