0

Hello I need to create a makefile for 2 separated cpp programs that are in one directory. I have got this code, but it's not working correctly. The .o files do not get created.Thank you

OBJS = a b
EXEC = first_run second_run

#------ constant definitions

ALL_OBJ = $(OBJS:%=%.o)

all: $(EXEC)

clean:
    $(RM) $(EXEC) $(OBJS) $(ALL_OBJ); make all

CC = g++

DO_OBJS = $(CC) -cpp -o [email protected] [email protected]; touch $@
DO_EXEC = $(CC) -s -o $@ $(ALL_OBJ)

#------ now compile

$(OBJS):    $(@:%=%.o)
        $(DO_OBJS)

$(EXEC):    $(OBJS)
        $(DO_EXEC)
4
  • g++ -s -o first_run a.o b.o a.o: In function _start': (.text+0x0): multiple definition of _start' /usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o:(.text+0x0): first defined here a.o:(.rodata+0x0): multiple definition of _fp_hw' /usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o:(.rodata+0x0): first defined here a.o: In function _fini': (.fini+0x0): multiple definition of `_fini' /usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crti.o:(.fini+0x0): first defined here Commented Oct 15, 2012 at 19:11
  • What are you trying to achieve? How do a & b relate to first_run and second_run? Is your first program supposed to be an executable called first_run? Or an executable called a? Or what? Commented Oct 15, 2012 at 19:13
  • I want to create a.o and b.o from one make file. first_run is for a and second for b. Commented Oct 15, 2012 at 19:15
  • You only want a.o and b.o? You don't want to create an executable? Commented Oct 15, 2012 at 19:17

3 Answers 3

3

There are a couple of problems with your file, but the major problem seems to be that you try to link both source files to a single executable. You have to list each program and its dependencies on its own.

Try instead this simple Makefile:

SOURCES = a.cpp b.cpp
OBJECTS = $(SOURCES:%.cpp=%.o)
TARGETS = first_run second_run

LD = g++
CXX = g++
CXXFLAGS = -Wall

all: $(TARGETS)

# Special rule that tells `make` that the `clean` target isn't really
# a file that can be made
.PHONY: clean

clean:
    -rm -f $(OBJECTS)
    -rm -f $(TARGETS)

# The target `first_run` depends on the `a.o` object file
# It's this rule that links the first program
first_run: a.o
    $(LD) -o $@ $<

# The target `second_run` depends on the `b.o` object file
# It's this rule that links the second program
second_run: b.o
    $(LD) -o $@ $<

# Tells `make` that each file ending in `.o` depends on a similar
# named file but ending in `.cpp`
# It's this rule that makes the object files    
.o.cpp:
Sign up to request clarification or add additional context in comments.

4 Comments

@NatashaLevesque Forgot to add commands for linking, please see my modified answer.
makefile:16: *** missing separator. Stop.
hm... still the same error makefile:16: *** missing separator. Stop.
it works. changed the -rm -f $(OBJECTS) -rm -f $(TARGETS) to \rm -f $(OBJECTS) \rm -f $(TARGETS)
2

KISS:

all: first_run second_run

clean:
    rm -f first_run second_run

first_run: a.c
    $(LINK.cc) $^ $(LOADLIBES) $(LDLIBS) -o $@

second_run: b.c
    $(LINK.cc) $^ $(LOADLIBES) $(LDLIBS) -o $@

Comments

0

I suggest this Makefile:

EXEC = first_run second_run
OBJS_FIRST = a.o
OBJS_SECOND = b.o

all: $(EXEC)

first_run: $(OBJS_FIRST)
    $(CXX) -o $@ $(OBJS_FIRST)

second_run: $(OBJS_SECOND)
    $(CXX) -o $@ $(OBJS_SECOND)

You don't need to define the object build, since make already knows, how to do that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.