Makefile - issues including directory

101 views Asked by At

I've been working on a Makefile but having two issues. The application directory contains autotools files for a Yocto build but for development I'd like to do both x86 & ARM builds locally via Makefile.

The project has the following directory structure (edited down for clarity):

├── autogen.sh
├── build
├── configure.ac
├── COPYING
├── database_dump
├── external
│   ├── Makefile.am
│   ├── sqlite3
│   │   ├── sqlite3.c
│   │   ├── sqlite3ext.h
│   │   └── sqlite3.h
│   └── sqlite3pp
│       ├── headeronly_src
│       │   ├── sqlite3ppext.h
│       │   ├── sqlite3ppext.ipp
│       │   ├── sqlite3pp.h
│       │   └── sqlite3pp.ipp
│       ├── README.md
│       ├── src
│       │   ├── sqlite3pp.cpp
│       │   ├── sqlite3ppext.cpp
│       │   ├── sqlite3ppext.h
│       │   └── sqlite3pp.h
│       └── test
│           ├── testaggregate.cpp
│           └── testselect.cpp
├── LICENSE
├── m4
│   ├── ax_cxx_compile_stdcxx.m4
│   └── ax_pthread.m4
├── Makefile.am
├── README.md
├── scripts
│   └── Makefile
├── src
│   ├── database.cpp
│   ├── database.h
│   ├── errorMsgs.h
│   ├── languages.h
│   ├── log.h
│   ├── main.cpp
│   ├── mainWindow.cpp
│   ├── mainWindow.h
│   └──pages.cpp

As you can see my Makefile is in /scripts. I'd like to keep it here to prevent conflicting with the autotools build.

Edit: I realise its strange to use both Autotools & Makefile but there is a method to my madness:

  1. Autotools used by Yocto to build the application in an image
  2. Use Makefile to make quick standalone compilations for both x86 & ARM (the application runs on both architectures).

The Makefile is currently:

TARGETS := applicationName
BUILD_DIR := ../build
SRC_DIR := ../src
CXXFLAGS :=-std=c++14 $(shell pkg-config --cflags --libs)
LDFLAGS :=$(shell pkg-config --cflags --libs) -lgpiod -pthread -lsqlite3

# Tried adding directory using vpath:
# vpath %.h ../external/sqlite3pp/headeronly_src/

SRCS := $(shell find $(SRC_DIR) -name *.cpp -or -name *.c -or -name *.s)
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
DEPS := $(OBJS:.o=.d)

INC_DIRS := $(shell find $(SRC_DIR) -type d)
INC_DIRS := $(INC_DIRS) ./external/sqlite3pp/headeronly_src/
INC_FLAGS := $(addprefix -I,$(INC_DIRS))

#echo $(INC_FLAGS)

CPPFLAGS :=$(INC_FLAGS) -MMD -MP

$(BUILD_DIR)/$(TARGETS): $(OBJS)
    $(CC) $(OBJS) -o $@ $(LDFLAGS)

# assembly
$(BUILD_DIR)/%.s.o: %.s
    mkdir -p $(dir $@)
    $(AS) $(ASFLAGS) -c $< -o $@

# c source
$(BUILD_DIR)/%.c.o: %.c
    mkdir -p $(dir $@)
    $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@

# cpp source
$(BUILD_DIR)/%.cpp.o: %.cpp
    mkdir -p $(dir $@)
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@

.PHONY: clean
clean:
    rm $(SRC_DIR)/*.o
    rm $(SRC_DIR)/*.d

-include $(DEPS)

The issues when I try running make:

  1. The build files end up in /src instead of /build
  2. I get the error:
username@username-XPS:~/Projects/sama5d4/applicationName/scripts$ make
mkdir -p ../build/../src/
g++ -I../src -I../external/sqlite3pp/headeronly_src/ -MMD -MP -std=c++14 -pthread -I/usr/local/include -I/usr/include/gstreamer-1.0 -I/usr/include/orc-0.4 -I/usr/include/gstreamer-1.0 -I/usr/include/lua5.3 -I/usr/include/plplot -I/usr/include/x86_64-linux-gnu -I/usr/include/librsvg-2.0 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/cairo -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/uuid -I/usr/include/freetype2 -I/usr/include/libpng16 -L/usr/local/lib -legt -lcairo -c ../src/dosingBoards.cpp -o ../build/../src/dosingBoards.cpp.o
mkdir -p ../build/../src/
g++ -I../src -I../external/sqlite3pp/headeronly_src/ -MMD -MP -std=c++14 -pthread -I/usr/local/include -I/usr/include/gstreamer-1.0 -I/usr/include/orc-0.4 -I/usr/include/gstreamer-1.0 -I/usr/include/lua5.3 -I/usr/include/plplot -I/usr/include/x86_64-linux-gnu -I/usr/include/librsvg-2.0 -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libmount -I/usr/include/blkid -I/usr/include/cairo -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/uuid -I/usr/include/freetype2 -I/usr/include/libpng16 -L/usr/local/lib -legt -lcairo -c ../src/main.cpp -o ../build/../src/main.cpp.o
In file included from ../src/mainWindow.h:18,
                 from ../src/main.cpp:1:
../src/database.h:14:10: fatal error: external/sqlite3pp/headeronly_src/sqlite3pp.h: No such file or directory
   14 | #include "external/sqlite3pp/headeronly_src/sqlite3pp.h"
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make: *** [Makefile:36: ../build/../src/main.cpp.o] Error 1

As you can see I tried amending the include directory (a number of variations) & using vpath but no success.

Could anyone please provide some pointers?

Thanks,

1

There are 1 answers

0
leo8382 On

I solved this by including the project directory as absolute, eg.

THIS_DIRS :=/home/me/Projects/sama5d4/thisProject

I think using the make $(CURDIR) might work as well, something like:

THIS_DIRS :=.../$(CURDIR)

But I haven't tested this solution.

The compilation is then:

%.cpp.o: %.cpp
    mkdir -p ${BUILD_DIR}
    echo "Building cpp source" $<
    $(CXX) $(CFLAGS) $(CXXFLAGS) -I$(THIS_DIRS) -c $< -o $@