8

I am currently failing to write a good makefile and don't know the reason why.. -.-

This is my main.c:

#include <windows.h>
#include <stdio.h>

int main(int argc, char *argv[])
{ 
   printf("MEEEEEP");
   return (0);
}

This is my makefile:

# make SYSTEM= OS= ENVIRONMENT=
# Binaries to use
ifeq ($(ENVIRONMENT),MINGW)
  CXX   = i686-pc-mingw32-g++
else
  CXX   = g++
endif
REMOVE  = rm -vf

RC      = windres
EXE     = .exe

#############################################################
# Info

ifeq ($(CXX),g++)
INFO_CXX = g++ -dumpversion; g++ -dumpmachine
endif

#############################################################
# Flags

DEBUG = -DDEBUG -g
OPTIMIZATION = -O2 #-Winline -finline-functions

CFLAGS = -Wall -Wextra -W -static $(DEBUG) $(OPTIMIZATION) -D$(SYSTEM) -D$(OS) -D$(ENVIRONMENT) $(PRGFLAGS)

ifeq ($(SYSTEM),I686)
  CFLAGS   += -m32

  ifeq ($(OS),WIN32)
    CFLAGS += -D_WIN32 
  endif

  ifeq ($(ENVIRONMENT),MINGW)
    CFLAGS += -fexceptions 
  endif
endif

 LFLAGS    = 

#############################################################
# Files

CFILES      = main.c
OBJS        = ${CFILES:.c=.o}

#############################################################
# Include

INCLUDES      = -I.

#############################################################
# Library

LIBRARIES     = 

#############################################################
# Targets
.PHONY: all
all:    
    @echo == Standard build: make SYSTEM=I686 OS=WIN32 ENVIRONMENT=MINGW
    @echo
    @echo 
    make SYSTEM=I686 OS=WIN32 ENVIRONMENT=MINGW gyro

#############################################################
# Implicit rules and filename extensions... 
.SUFFIXES: .h .o .c

.c.o:     %.h
      @echo Compiling $< for $(SYSTEM) $(OS) $(ENVIRONMENT) ...
      @echo MEEP
      $(CXX) $(CFLAGS) $(INCLUDES) -c $< -o $@
      @echo MEEP2

#############################################################
# Target rules
gyro: $(OBJS)
      @echo Building software for $(SYSTEM) ...
      @echo
      $(CXX) $(CFLAGS) $(LFLAGS) -o $@$(EXE) $(OBJS) $(LIBRARIES)

#############################################################
# Clean
.PHONY: clean
clean:
    $(REMOVE) $(OBJS)


#############################################################
# Info
.PHONY: info
info:
    @echo 
    @echo Information about C++ Compiler/Linker:
    @echo 
    $(INFO_CXX)

When i type in make gyro, i receive the output:

Compiling main.c for Windows_NT ...
MEEP
g++ -Wall -Wextra -W -static -DDEBUG -g -O2  -D -DWindows_NT -D  -I. -c main.c -o     main.o
makeNew.mak:83: recipe for target `main.o' failed
make: *** [main.o] Error 1

But Line number 83 is behind .c.o: %.h. And i don’t understand why. Does anyone have a solution for me?

Uyghur Lives Matter
  • 18,820
  • 42
  • 108
  • 144
Aureon
  • 387
  • 3
  • 4
  • 15
  • 1) have you tried simplifying this makefile? 2) Have you tried running that command from the command line? – Beta Aug 13 '12 at 13:46
  • I did but it wasnt enough. I got this from a co-worker in order to use this for serial port programming but have no acceptable experience with makefiles (i just used "make all", because some other made the makefiles). I normally want a makefile like this: compile all my c-files in this folder and build an executable called gyro from it.. thats all i need, but i may need a break.. it feels like it wont get into my head. :-( – Aureon Aug 13 '12 at 15:12
  • This will take a few iterations. Try this from the command line: `g++ -Wall -c main.c -o main.o`. If it works, we can build up. – Beta Aug 13 '12 at 16:00
  • Hey Beta! Thanks for your reply! Neither `g++ -Wall -c main.c -o main.o` nor `i686-pc-mingw32-g++ -Wall -c main.c -o main.o` works. I get no response from the command line. – Aureon Aug 13 '12 at 16:10
  • Re your PS: you'll get much better response if you ask separate questions as separate questions. Although I'm not sure how good a fit "point me at a good tutorial" is for Stack Exchange forums. – me_and Aug 14 '12 at 10:02

1 Answers1

5

You see the two empty -D entries in the g++ command line? They're causing the problem. You must have values in the -D items e.g. -DWIN32

if you're insistent on using something like -D$(SYSTEM) -D$(ENVIRONMENT) then you can use something like:

SYSTEM ?= generic
ENVIRONMENT ?= generic

in the makefile which gives them default values.

Your output looks to be missing the all important output:

<command-line>:0:1: error: macro names must be identifiers
<command-line>:0:1: error: macro names must be identifiers

just to clarify, what actually got sent to g++ was -D -DWindows_NT, i.e. define a preprocessor macro called -DWindows_NT; which is of course not a valid identifier (similarly for -D -I.)

Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122
  • Hi Petesh, thanks for your reply! I get this message now: `MEEP i686-pc-mingw32-g++ -Wall -Wextra -W -static -DDEBUG -O2 -DI686 -DWIN32 -DMINGW -m32 -D_WIN32 -fexceptions -I. -c main.c -o main.o Makefile:102: recipe for target `main.o' failed make[1]: *** [main.o] Error 1 make[1]: Leaving directory `/cygdrive/c/project' Makefile:85: recipe for target `all' failed make: *** [all] Error 2` – Aureon Aug 13 '12 at 15:15
  • Hi, It's very difficult to read command output in a comment; please add it to the question in a better format; but again; you seem to be missing an important part of the error output, which makes debugging this incredibly difficult. At a guess, `i686-pc-mingw32-g++` is not in the path. On my system, it's called `mingw32-g++` - the `i686-pc-` prefix is not present – Anya Shenanigans Aug 13 '12 at 15:26
  • Ok! Well i tried to look if its an error, but on our systems there is no mingw32-g++ installed (i tried to open its manual per `man mingw32-g++` but got "no manual"-message). Is there a way to simplify the makefile? What do you think? Btw: thanks again for your time! :-) – Aureon Aug 13 '12 at 15:46
  • I was using a separately downloaded mingw environment, which is why I was seeing different requirements (i.e. your `i686-pc-`... version is correct. I would try running: `i686-pc-mingw32-g++ -Wall -Wextra -W -static -DDEBUG -O2 -DI686 -DWIN32 -DMINGW -m32 -D_WIN32 -fexceptions -I. -c main.c -o main.o` from a cygwin shell prompt in the `/cygdrive/c/projects` directory and checking what the error is in that case. If you're not seeing an error and the compile is failing then you have a serious environment problem – Anya Shenanigans Aug 13 '12 at 18:29
  • I am not seeing any errors. And there is no compiling. I get NO status messages -.- Isnt there a way to simplify the makefile to make it work? I just need a working Makefile which builds my existing project c-files into an executable (because i am experimenting with serial port programming) – Aureon Aug 14 '12 at 10:35