0

In my project i have directory structure /libs and /src. I want to create makefile where main has dependency in /libs. ie. header file is in the /libs. I have used the following

#Compiler to use
CC=gcc
#Compiler flags to use
CFLAGS=-c -Wall

BUILD = ./build
SRC = ./src
LIBS = ./libs

SOURCES = $(SRC)/main.c $(SRC)/SmallComponent1.c $(SRC)/SmallComponent2.c $(LIBS)/ExtLib.c
OBJECTS = $(SOURCES: .c=.o)
EXECUTABLE = $(BUILD)/appl.exe
all: $(OBJECTS) $(EXECUTABLE)

$(BUILD):
    mkdir $(BUILD)

$(EXECUTABLE) : $(OBJECTS)
    $(CC) $(OBJECTS) -o $@ 
.c.o: 
    $(CC) $(CFLAGS) -I$(LIBS) $< -o $@





phony: clean
clean:
    rm $(BUILD)/*.o $(BUILD)/appl.exe

Questions are: how can i create /build if not exist? and how to include external .h in the $(CC) $(CFLAGS) -I$(LIBS) $< -o $@, because -I$(LIBS) does not work and i got following error ./src/main.c:4:20: fatal error: ExtLib.h: No such file or directory

Iftikhar
  • 667
  • 4
  • 10
  • 17

1 Answers1

1

To create the build directory before it is needed use an order-only prerequisite (OOP) and use the -p option of mkdir to avoid getting an error if it exists already (it should not be needed because of the OOP but it is always better to use this option in Makefiles):

$(BUILD):
    mkdir -p $@

$(EXECUTABLE) : $(OBJECTS) | $(BUILD)
    $(CC) $(OBJECTS) -o $@

For your other problem we need some more information; as you describe it it is not reproducible. Try maybe to provide an MCVE.

Renaud Pacalet
  • 25,260
  • 3
  • 34
  • 51