0

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.

Vlad
  • 519
  • 4
  • 13
  • make doesn't provide command autocompletion facilities. That is some kind of add-on for your shell (bash or zsh or whatever). You'll have to find out where it comes from and ask the people involved with creating it, how it works / how it determines what targets to suggest for completion. – MadScientist Oct 26 '21 at 13:09
  • I don't have much comment on your makefile. I do wonder why you are using the very complicated `$(addsuffix $(addprefix $(LIB_NAME),.so),lib)` instead of just writing `lib$(LIB_NAME).so` ? – MadScientist Oct 26 '21 at 13:10
  • That is true, I've used this complicated syntax mostly for learning purposes. – Vlad Oct 26 '21 at 13:44

0 Answers0