Simple makefile for C ++

I'm having trouble assigning programming to a CS class. We got to know makefiles this week, but it was barely touched on. The goal is to write a program to calculate the average and sum of user-supplied doublings. The program works fine with g ++, but I am having problems with makefiles.

I am working with 3 cpp files: main.cpp, average.cpp, summation.cpp. And 2 header files: average.h and summation.h

Here is one make file I tried:

CXX = g++
CXXFLAGS = -std=c++0x -Wall -pedantic-errors -g

SRCS = summation.cpp average.cpp main.cpp 
OBJS = ${SRCS:.cpp=.o}
HEADERS = summation.h average.h

MAIN = myprog

all: ${MAIN}
    @echo   Simple compilter named myprog has been compiled

${MAIN}: ${OBJS} ${HEADERS}
    ${CXX} ${CXXFLAGS} ${OBJS} -o ${MAIN} ${OBJS}

.cpp.o:
    ${CXX} ${CXXFLAGS} -c $< -o $@

clean:
    ${RM} ${PROGS} ${OBJS} *.o *~

      

Error: main.cpp: 31: undefined reference to avg(double*, int)' main.cpp:32: undefined reference to

sum (double *, int) '

If I run the command g ++ main.cpp summation.cpp average.cpp -o main Then it will compile without any problem.

I'm not sure if there is something wrong with me, or if I am missing something. Looking around I found a lot of information, but it looks like it was over my head.

+3


source to share


1 answer


I think you have two problems.

Ordering first SRCS = summation.cpp average.cpp main.cpp

. This causes .o

g ++ to be provided in order summation.o average.o main.o

, which raises an undefined error. You want to provide them in order main.o summation.o average.o

, so the SRCS should be SRCS = main.cpp summation.cpp average.cpp

.

Second, on the line:



${CXX} ${CXXFLAGS} ${OBJS} -o ${MAIN} ${OBJS}

      

The second is ${OBJS}

not needed.
Try the following:

CXX = g++
CXXFLAGS = -std=c++0x -Wall -pedantic-errors -g

SRCS =  main.cpp summation.cpp average.cpp
OBJS = ${SRCS:.cpp=.o}
HEADERS = summation.h average.h

MAIN = myprog

all: ${MAIN}
        @echo   Simple compilter named myprog has been compiled

${MAIN}: ${OBJS}
        ${CXX} ${CXXFLAGS} ${OBJS} -o ${MAIN}

.cpp.o:
        ${CXX} ${CXXFLAGS} -c $< -o $@

clean:
        ${RM} ${PROGS} ${OBJS} *.o *~.

      

+5


source







All Articles