2
\$\begingroup\$

I've decided to write a common Makefile for Google benchmark library. You could say I could have taken generated Makefile, but it's not appropriate solution (or maybe I'm wrong?).

TARGET = libbenchmark.a

CXXFLAGS = -std=c++11 -Wall -Wextra -Wshadow -Werror -pedantic \
            -pedantic-errors -Wfloat-equal \
            -Wzero-as-null-pointer-constant -fstrict-aliasing \
            -DHAVE_STD_REGEX

LDFLAGS = -lpthread

# Define include files and directories
INCLUDES = -I./include -I./src

# Define the source files
SOURCE_FILES = src/colorprint.cc src/benchmark.cc src/commandlineflags.cc \
               src/console_reporter.cc src/csv_reporter.cc src/json_reporter.cc \
               src/log.cc src/reporter.cc src/sleep.cc src/string_util.cc \
               src/sysinfo.cc src/walltime.cc src/re_std.cc

OBJECT_FILES = $(SOURCE_FILES:.cc=.o)

CC = gcc
AR = gcc-ar

all: $(TARGET)

$(TARGET): $(OBJECT_FILES)
    $(AR) rcs $(TARGET) $(OBJECT_FILES)

%.o: %.cc 
    $(CC) $(INCLUDES) $(CXXFLAGS) -c -o $@ $<

clean:
    $(RM) src/*.o $(TARGET)

install:
    mkdir -p $(DESTDIR)/usr/lib
    cp -p ./$(TARGET) $(DESTDIR)/usr/lib
    cp -r include $(DESTDIR)/usr
\$\endgroup\$
  • \$\begingroup\$ Why do you feel a generated makefile is not appropriate? \$\endgroup\$ – Mast Aug 10 '16 at 11:08
  • 1
    \$\begingroup\$ It's a port for embedded system without CMake. \$\endgroup\$ – user1056837 Aug 10 '16 at 11:14
  • \$\begingroup\$ Ah, that makes sense. Added the relevant tag for completeness. \$\endgroup\$ – Mast Aug 10 '16 at 11:47
1
\$\begingroup\$
  • In a clean rule, consider removing $(OBJECT_FILES) instead of src/*.o

  • I don't see where LDFLAGS is ever used.

  • I understand that you are not likely to edit include files; however, to be on a safe side you may want to add auto dependency generation:

    DEP_FILES = $(SOURCE_FILES:.cc=.d)
    
    %.d: %.cc
        $(CC) $(INCLUDES) $(CXXFLAGS) -MM -MT -o $@ $<
    
    -include $(DEP_FILES)
    

    (and remove $(DEP_FILES) on clean).

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.