I've created a small project with the following file structure:
.
+---include
+---StringUtils.h
+---Utils.h
+---src
+---StringUtils.cpp
+---Utils.h
+---bin
+---libutils.so
Makefile
bin/libutils.so is built with make
(both the folder and the .so are created within the makefile), and Makefile looks as follows:
.DEFAULT_GOAL := target
LIB_NAME := log
SRC_DIR := src
INC_DIR := include
BIN_DIR := bin
INSTALL_INC_DIR := /usr/local/include/$(LIB_NAME)
INSTALL_LIB_DIR := /usr/local/lib
SOURCES := $(addprefix $(SRC_DIR)/,*.cpp)
OBJ_NAME := $(addsuffix $(addprefix $(LIB_NAME),.so),lib)
TARGET_OBJ := $(BIN_DIR)/$(OBJ_NAME)
TARGET_INCS := $(INC_DIR)/*.h
DBG := 0
INC_FLAGS := $(addsuffix $(INC_DIR),-I)
CXX_VER_FLAG := -std=c++17
DBG_FLAG := -ggdb3
CXXFLAGS := $(CXX_VER_FLAG) $(INC_FLAGS)
CXXFLAGS += -shared -fPIC
ifeq ($(DBG),1)
CXXFLAGS := -ggdb3 $(CXXFLAGS)
endif
.PHONY: all target clean install uninstall reinstall
all: target install
target: $(TARGET_OBJ)
$(TARGET_OBJ): $(BIN_DIR)/%.so: $(SOURCES)
mkdir -p $(BIN_DIR)
$(CXX) $(CXXFLAGS) $^ -o $@
clean:
rm -rf $(BIN_DIR)
install: target
sudo install -d $(INSTALL_LIB_DIR)
sudo install -d $(INSTALL_INC_DIR)
sudo install -m644 $(TARGET_OBJ) $(INSTALL_LIB_DIR)
sudo install -m644 $(TARGET_INCS) $(INSTALL_INC_DIR)
sudo ldconfig
uninstall:
sudo $(RM) $(INSTALL_LIB_DIR)/$(OBJ_NAME)
sudo $(RM) -r $(INSTALL_INC_DIR)
sudo ldconfig
reinstall: uninstall install
My issue is that when inside the project folder, the make
autocomplete also displays the bin/
folder as a possible target:
....../libs/libutils$ make
all bin/ clean install reinstall target uninstall
I suppose the solution would be to remove $(BIN_DIR)
from TARGET_OBJ := $(BIN_DIR)/$(OBJ_NAME)
, but I'm not exactly sure how to make that work.
Also, any constructive criticism regarding the entire Makefile is much appreciated.