Making my first makefile

Hi,

I'm trying to create a makefile, but I have some confusion.

Here is my makefile…

CXX = g++

DEBUG = -g

TARGET = ??????

CFLAGS = -c -Wall $(DEBUG)

all: $(TARGET)

$(TARGET): main.o
$(CXX) $(CFLAGS) main.cpp

clean:
rm main.o

So, say I just wanted to compile a simple main.cpp to print hello world, no header files. What would my TARGET be? I tried 'file', saved it, exited, then typed 'make' into the command line and it said no target specified and no makefile found.

*edit*
Are there any issues with the rest of my makefile? Other than the tabbing. I couldn't get the spacing to work right on here.
*****
Thank you.
Last edited on
It'd be whatever your program's name is, i.e. whatever you'd pass to -o as part of manual compilation.

-Albatross
Thank you. It just goes right into the program file with main.cpp right?
It's been a while since I last wrote a makefile, so I might have gotten something in the following mixed up.

Your $(TARGET): main.o rule would be used to link main.o (and whatever objects you have in the future) into a single executable. Similarly, you need a main.o: main.cpp rule (or a general rule for converting .cpp files into .o files). What you currently have is a rule that Make thinks is supposed to convert main.o into your target... but that's not what happens.

Also, clean should delete all non-configuration files created by Make, not just main.o.

I hope that answers your question (or at least gives you an idea of what to do).

-Albatross
Last edited on
I've updated my makefile.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

CXX = g++

DEBUG = -g

TARGET = main

CFLAGS = -c -Wall $(DEBUG)

all: $(TARGET)

$(TARGET): main.o
        $(CXX) $(CFLAGS) main.cpp

clean:
        rm main.o


It will successfully create the main.o, but I am still missing my executable. I'm about to look at furry guy's link.
GNU Make has a whole bunch of defaults. You can see them with make -p

If you have one file, say hello.cpp, you don't need a makefile. To build it, type:
 
make hello


You don't need to specify CXX or CFLAGS.

If you have more than one file, then you need a makefile. You can use this as a minimal template.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
PROG_CXX = prog
SRCS = main.cc module2.cc module3.cc
LDADD =

CXXFLAGS = -g
CXXFLAGS = -std=c++11 -pedantic -I/usr/local/include
CXXFLAGS = -Wall -Wextra -Wno-unused-parameter

all: $(PROG_CXX)

clean:
        - rm $(PROG_CXX) $(SRCS:.cc=.o)

$(PROG_CXX): $(SRCS:.cc=.o)
        $(LINK.cc) -o $@ $^ $(LDADD)
Topic archived. No new replies allowed.