Sunday, November 16, 2003

MinGW (basic makefile)

First off, make sure you have the GNU Tools for Win32, MinGW, and MSYS installed and working.

Then, create the following makefile in your project folder (note, any indented lines must be done using the TAB character, not spaces):


############################################
# MAKEFILE FOR main.exe -- last rev 2003-11-14 TGH

# NOTES:
# - This makefile was designed for compilation using MinGW under Win32
# - Get GNU tools for Win32 (http://sourceforge.net/projects/unxutils/)
# - Get MinGW (http://www.mingw.org/)
# - Get MSYS (http://www.mingw.org/msys.shtml)
# - Make sure that the GNU tools and MinGW are in your PATH= statement
# - Start the MSYS shell, cd to your project file and type "make all" to compile

############################################
# MACRO DEFINITIONS
# (hint: "make -p" will spit out a default list for your environment)
# $@ - name of the file to be made (e.g. "main.o")
# $? - names of the changed dependents

CC=gcc
LINK=$(CC)

#COMPILE FLAGS - CFLAGS2 appears at the end of the command
CFLAGS1=
CFLAGS2=
#CFDEBUG= -Wp,-DDEBUG
CFDEBUG=

#LINK FLAGS - LFLAGS2 appears at the end of the command
LFLAGS1=
LFLAGS2=

#OBJS is the list of all object files created by the compiles
OBJS=module1.o module2.o main.o
PROGNAME=main.exe

############################################
# IMPLICIT RULES
# $< - name of the related file that caused the action (e.g. "main.c")
# $* - prefix shared by both the target and the dependent (e.g. "main")

.c.o:
$(CC) $(CFLAGS1) $(CFDEBUG) -c -o $@ $*.c $(CFLAGS2)

.o.exe:
$(LINK) $(LFLAGS1) -o $@ $? $(LFLAGS2)

############################################
# MAKE TARGETS (all, clean, install, etc)
# - all: should be the first link target within the make file

all: $(PROGNAME)

clean:
-rm $(OBJS)
-rm $(PROGNAME)

install:
@echo No action taken.

############################################
# MODULES TO BE COMPILED

module1.o: module1.c module1.h
module2.o: module2.c module2.h
main.o: main.c module1.h module2.h

############################################
# PROGRAM TO BE COMPILED (.EXE)

$(PROGNAME): $(OBJS)
$(LINK) $(LFLAGS1) -o $@ $(OBJS) $(LFLAGS2)

### END OF MAKEFILE ###


Once that's finished, and you've changed things like main.exe, module1.c to match your project, open up the MSYS window and type the following commands to compile:

UserName@MACHINENAME ~
$ pwd
/home/UserName

UserName@MACHINENAME ~
$ cd /c/dev/projects/test/src

UserName@MACHINENAME /c/dev/projects/test/src
$ make clean
rm module1.o module2.o main.o
rm main.exe

UserName@MACHINENAME /c/dev/projects/test/src
$ make all
gcc -c -o module1.o module1.c
gcc -c -o module2.o module2.c
gcc -c -o main.o main.c
gcc -o main.exe module1.o module2.o main.o

UserName@MACHINENAME /c/dev/projects/test/src
$ ls *.exe
main.exe

No comments: