BobMorane
BobMorane

Reputation: 4296

Creating makefile for c++ static library with g++

I want to use GNU make to compile a static library. On my disc, the project is arranged this way:

.
├── makefile
├── publicAPI
│   └── Some headers (.h)
└── source
    ├── All .cpp files
    └── Some headers (.h)

I have written the following makefile, so far:

CPPC                 = gcc
STANDARD_FLAGS       = -std=c++0x
WARN_AS_ERRORS_FLAGS = -pedantic-errors\
                       -Wall\
                       -Wextra\
                       -Werror\
                       -Wconversion
DEBUG_FLAGS          = -g3
OPT_FLAGS            = -0O
NO_LINKER_FLAGS      = -c
CPPFLAGS             = $(OPT_FLAGS) $(DEBUG_FLAGS) $(NO_LINKER_FLAGS) $(STANDARD_FLAGS) \
                       $(WARN_AS_ERRORS_FLAGS)

# Source files, headers, etc.:
ROOT     = $(CURDIR)
INCLUDES = -I$(ROOT)source -I$(ROOT)publicAPI

SRCS     = ./source/AsciiColorCode.cpp\
           ./source/Color.cpp

OBJS     = AsciiColorCode.o\
           Color.o

LIBS =
MAIN = libcXbase.a # static library


all:    $(MAIN)
    @echo $(MAIN) has been compiled!

$(MAIN): $(OBJS)
    ar -r $(MAIN) $(OBJS) $(LIBS)

.cpp.o:
    $(CPPC) $(CPPFLAGS) $(INCLUDES) -c $< -o $@

depend: $(SRCS)
    makedepend $(INCLUDES) $^

When I run make all, I get the following error: make: *** No rule to make target 'AsciiColorCode.o', needed by 'libcXbase.a'. Stop. which indicates that the AsciiColorCode.o file has not been created. I can't find what is missing from this:

  1. I find the dependencies
  2. I compile .o object files from .cpp files
  3. I ar everything into a .a file.

What is missing? Also, if you have any comment on my makefile, please don't hesitate as this is my first one.

Regards

Upvotes: 1

Views: 4886

Answers (1)

Come Raczy
Come Raczy

Reputation: 1680

You should specify where the prerequisites are. This can be done by adding this line:

VPATH = source

Also, in the rule .cpp.o, the variable CPCC is not defined (probably should be CXX)

Also, OPT_FLAGS should be -O0

Upvotes: 1

Related Questions