Makefile doesn't compile all files when I change it - c++

I wrote makefile which seems to works fine except for one case:
When I change the makefile itself it does not recompile all files.
I've done all those steps:
make clean
make all
make all (it returns "Nothing to be done for all") - ok
change makefile (add empty line etc.)
make all (still it returns "Nothing to be done for all" - not ok at all
In my opinion, it should recompile when the makefile file was changed.
What can be improved to force makefile to recompile all files when I make modifications in makefile?
Makefile:
################################### VARIABLES ############################################
# Output settings:
BUILD_DIR := ./build
BIN_DIR := $(BUILD_DIR)/bin
TARGET_FOLDER := $(BUILD_DIR)
TARGET_NAME := app.exe
# Input settings:
SOURCE_DIR = ./
SOURCES := $(SOURCE_DIR)/main.cpp
INCLUDES := $(SOURCE_DIR)
# Compilator settings:
CC := gcc
CXX := g++
# Compilation flags:
COMMON_FLAGS := -Wall -Werror
CFLAGS := $(COMMON_FLAGS)
CXXFLAGS := $(COMMON_FLAGS)
LDFLAGS :=
# VERBOSE OPTION
VERBOSE ?= 0 # Set to zero to silent makefile output
ifeq ($(VERBOSE),1)
NO_ECHO :=
else
NO_ECHO := #
endif
############################### MAGID PART START #########################################
TARGET := $(abspath $(TARGET_FOLDER)/$(TARGET_NAME))
# Remove double slash (//) from path, it can make issue with vpath.
# Just use abspath :)
SOURCES_ABS := $(abspath $(SOURCES) )
INCLUDES_ABS := $(abspath $(INCLUDES) )
SOURCES_C := $(filter %.c, $(SOURCES_ABS) )
SOURCES_CC := $(filter %.cc, $(SOURCES_ABS) )
SOURCES_CPP := $(filter %.cpp, $(SOURCES_ABS) )
OBJECTS_LIST := $(notdir $(patsubst %.c, %.o, $(SOURCES_C) ) )
OBJECTS_LIST += $(notdir $(patsubst %.cc, %.o, $(SOURCES_CC) ) )
OBJECTS_LIST += $(notdir $(patsubst %.cpp, %.o, $(SOURCES_CPP) ) )
OBJECTS := $(addprefix $(BIN_DIR)/, $(OBJECTS_LIST) )
DEPENDENCIES := $(patsubst %.o, %.d, $(OBJECTS) )
INCLUDES_FLAG := $(addprefix -I, $(INCLUDES_ABS) )
COMMON_FLAGS += $(INCLUDES_FLAG)
COMMON_FLAGS += -MMD -MP # Generate dependency files
CFLAGS += $(COMMON_FLAGS)
CXXFLAGS += $(COMMON_FLAGS)
# Use vpath to search sources:
VPATH += $(sort $(dir $(SOURCES_C) ) )
VPATH += $(sort $(dir $(SOURCES_CC) ) )
VPATH += $(sort $(dir $(SOURCES_CPP) ) )
all: app
app: $(TARGET)
$(TARGET): $(OBJECTS) | $(BUILD_DIR)
$(info Preparing: $#)
$(NO_ECHO) $(CXX) $(CXXFLAGS) -o $# $^
$(BIN_DIR)/%.o : %.c | $(BIN_DIR)
$(info Preparing: $#)
$(NO_ECHO) $(CC) -c $(CFLAGS) $< -o $#
$(BIN_DIR)/%.o : %.cc | $(BIN_DIR)
$(info Preparing: $#)
$(NO_ECHO) $(CXX) -c $(CXXFLAGS) $< -o $#
$(BIN_DIR)/%.o : %.cpp | $(BIN_DIR)
$(info Preparing: $#)
$(NO_ECHO) $(CXX) -c $(CXXFLAGS) $< -o $#
$(BIN_DIR): |$(BUILD_DIR)
$(NO_ECHO) -mkdir $#
$(BUILD_DIR):
$(NO_ECHO) -mkdir $#
.PHONY : clean
clean:
-rm -fr $(TARGET) $(BUILD_DIR) *.o *.d
-include $(DEPENDENCIES)
main.cpp
#include <iostream>
using namespace std;
int main(void)
{
cout<<"HA HA"<<endl;
return 0;
}

Related

Wire library Visual Studio Code C++ cpp examples not working

I'm running example code from the Arduino Wire library (Master Sender, Slave Reciever), but I get an error in the Slave-Reciever code. It's having problems with the recieveEvent function.
The code works in Arduino, so why is it different in Visual Studio Code? Is there anything that I should change in structure or anything? I don't see why this would be.
// Wire Slave Receiver
// by Nicholas Zambetti <http://www.zambetti.com>
// Demonstrates use of the Wire library
// Receives data as an I2C/TWI slave device
// Refer to the "Wire Master Writer" example for use with this
// Created 29 March 2006
// This example code is in the public domain.
#include <Arduino.h>// <- I added this include
#include <Wire.h>
int led = LED_BUILTIN;
void recieveEvent(int howMany); // <- I added this
void setup(){
Wire.begin(9);
Wire.onReceive(recieveEvent); //<-This is the issue spot
Serial.begin(115200);
}
void loop()
{
delay(100);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
digitalWrite(led, HIGH); // briefly flash the LED
while(Wire.available() > 1) { // loop through all but the last
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
int x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
digitalWrite(led, LOW);
}
This is the Make File:
#******************************************************************************
# Generated by VisualTeensy V1.5.0 on 6/11/2022 at 2:32 PM
#
# Board Teensy 3.2 / 3.1
# USB Type Serial
# CPU Speed 96 MHz (overclock)
# Optimize Faster
# Keyboard Layout US English
#
# https://github.com/luni64/VisualTeensy
#******************************************************************************
SHELL := cmd.exe
export SHELL
TARGET_NAME := BASICSMasterSend
BOARD_ID := TEENSY32
MCU := mk20dx256
LIBS_SHARED_BASE := C:\Users\Sorin\OneDrive\Documents\Arduino\libraries
LIBS_SHARED := Adafruit_PWM_Servo_Driver_Library
LIBS_LOCAL_BASE := lib
LIBS_LOCAL := SPI Time Wire
CORE_BASE := C:\PROGRA~2\Arduino\hardware\teensy\avr\cores\teensy3
GCC_BASE := C:\PROGRA~2\Arduino\hardware\tools\arm\bin
UPL_PJRC_B := C:\PROGRA~2\Arduino\hardware\tools
UPL_TYCMD_B := C:\tytools096\TyTools-0.9.7-win64
#******************************************************************************
# Flags and Defines
#******************************************************************************
TIME_SYM := $(shell powershell [int][double]::Parse((Get-Date -UFormat %s)))
FLAGS_CPU := -mthumb -mcpu=cortex-m4 -fsingle-precision-constant
FLAGS_OPT := -O2
FLAGS_COM := -g -Wall -ffunction-sections -fdata-sections -nostdlib -mno-unaligned-access -MMD
FLAGS_LSP :=
FLAGS_CPP := -fno-exceptions -fpermissive -felide-constructors -std=gnu++14 -Wno-error=narrowing -fno-rtti
FLAGS_C :=
FLAGS_S := -x assembler-with-cpp
FLAGS_LD := -Wl,--print-memory-usage,--gc-sections,--relax,--defsym=__rtc_localtime=$(TIME_SYM) -T$(CORE_BASE)/mk20dx256.ld
LIBS := -larm_cortexM4l_math -lm -lstdc++
DEFINES := -D__MK20DX256__ -DTEENSYDUINO=154 -DARDUINO_TEENSY32 -DARDUINO=10813
DEFINES += -DF_CPU=96000000 -DUSB_SERIAL -DLAYOUT_US_ENGLISH
CPP_FLAGS := $(FLAGS_CPU) $(FLAGS_OPT) $(FLAGS_COM) $(DEFINES) $(FLAGS_CPP)
C_FLAGS := $(FLAGS_CPU) $(FLAGS_OPT) $(FLAGS_COM) $(DEFINES) $(FLAGS_C)
S_FLAGS := $(FLAGS_CPU) $(FLAGS_OPT) $(FLAGS_COM) $(DEFINES) $(FLAGS_S)
LD_FLAGS := $(FLAGS_CPU) $(FLAGS_OPT) $(FLAGS_LSP) $(FLAGS_LD)
AR_FLAGS := rcs
NM_FLAGS := --numeric-sort --defined-only --demangle --print-size
#******************************************************************************
# Colors
#******************************************************************************
COL_CORE := [38;2;187;206;251m
COL_LIB := [38;2;206;244;253m
COL_SRC := [38;2;100;149;237m
COL_LINK := [38;2;255;255;202m
COL_ERR := [38;2;255;159;159m
COL_OK := [38;2;179;255;179m
COL_RESET := [0m
#******************************************************************************
# Folders and Files
#******************************************************************************
USR_SRC := src
LIB_SRC := lib
CORE_SRC := $(CORE_BASE)
BIN := .vsteensy/build
USR_BIN := $(BIN)/src
CORE_BIN := $(BIN)/core
LIB_BIN := $(BIN)/lib
CORE_LIB := $(BIN)/core.a
TARGET_HEX := $(BIN)/$(TARGET_NAME).hex
TARGET_ELF := $(BIN)/$(TARGET_NAME).elf
TARGET_LST := $(BIN)/$(TARGET_NAME).lst
TARGET_SYM := $(BIN)/$(TARGET_NAME).sym
#******************************************************************************
# BINARIES
#******************************************************************************
CC := $(GCC_BASE)/arm-none-eabi-gcc
CXX := $(GCC_BASE)/arm-none-eabi-g++
AR := $(GCC_BASE)/arm-none-eabi-gcc-ar
NM := $(GCC_BASE)/arm-none-eabi-gcc-nm
SIZE := $(GCC_BASE)/arm-none-eabi-size
OBJDUMP := $(GCC_BASE)/arm-none-eabi-objdump
OBJCOPY := $(GCC_BASE)/arm-none-eabi-objcopy
UPL_PJRC := "$(UPL_PJRC_B)/teensy_post_compile" -test -file=$(TARGET_NAME) -path=$(BIN) -tools="$(UPL_PJRC_B)" -board=$(BOARD_ID) -reboot
UPL_TYCMD := $(UPL_TYCMD_B)/tyCommanderC upload $(TARGET_HEX) --autostart --wait --multi
UPL_CLICMD := $(UPL_CLICMD_B)/teensy_loader_cli -mmcu=$(MCU) -v $(TARGET_HEX)
UPL_JLINK := $(UPL_JLINK_B)/jlink -commanderscript .vsteensy/flash.jlink
#******************************************************************************
# Source and Include Files
#******************************************************************************
# Recursively create list of source and object files in USR_SRC and CORE_SRC
# and corresponding subdirectories.
# The function rwildcard is taken from http://stackoverflow.com/a/12959694)
rwildcard =$(wildcard $1$2) $(foreach d,$(wildcard $1*),$(call rwildcard,$d/,$2))
#User Sources -----------------------------------------------------------------
USR_C_FILES := $(call rwildcard,$(USR_SRC)/,*.c)
USR_CPP_FILES := $(call rwildcard,$(USR_SRC)/,*.cpp)
USR_S_FILES := $(call rwildcard,$(USR_SRC)/,*.S)
USR_OBJ := $(USR_S_FILES:$(USR_SRC)/%.S=$(USR_BIN)/%.s.o) $(USR_C_FILES:$(USR_SRC)/%.c=$(USR_BIN)/%.c.o) $(USR_CPP_FILES:$(USR_SRC)/%.cpp=$(USR_BIN)/%.cpp.o)
# Core library sources --------------------------------------------------------
CORE_CPP_FILES := $(call rwildcard,$(CORE_SRC)/,*.cpp)
CORE_C_FILES := $(call rwildcard,$(CORE_SRC)/,*.c)
CORE_S_FILES := $(call rwildcard,$(CORE_SRC)/,*.S)
CORE_OBJ := $(CORE_S_FILES:$(CORE_SRC)/%.S=$(CORE_BIN)/%.s.o) $(CORE_C_FILES:$(CORE_SRC)/%.c=$(CORE_BIN)/%.c.o) $(CORE_CPP_FILES:$(CORE_SRC)/%.cpp=$(CORE_BIN)/%.cpp.o)
# User library sources (see https://github.com/arduino/arduino/wiki/arduino-ide-1.5:-library-specification)
LIB_DIRS_SHARED := $(foreach d, $(LIBS_SHARED), $(LIBS_SHARED_BASE)/$d/ $(LIBS_SHARED_BASE)/$d/utility/) # base and /utility
LIB_DIRS_SHARED += $(foreach d, $(LIBS_SHARED), $(LIBS_SHARED_BASE)/$d/src/ $(dir $(call rwildcard,$(LIBS_SHARED_BASE)/$d/src/,*/.))) # src and all subdirs of base
LIB_DIRS_LOCAL := $(foreach d, $(LIBS_LOCAL), $(LIBS_LOCAL_BASE)/$d/ $(LIBS_LOCAL_BASE)/$d/utility/ ) # base and /utility
LIB_DIRS_LOCAL += $(foreach d, $(LIBS_LOCAL), $(LIBS_LOCAL_BASE)/$d/src/ $(dir $(call rwildcard,$(LIBS_LOCAL_BASE)/$d/src/,*/.))) # src and all subdirs of base
LIB_CPP_SHARED := $(foreach d, $(LIB_DIRS_SHARED),$(call wildcard,$d*.cpp))
LIB_C_SHARED := $(foreach d, $(LIB_DIRS_SHARED),$(call wildcard,$d*.c))
LIB_S_SHARED := $(foreach d, $(LIB_DIRS_SHARED),$(call wildcard,$d*.S))
LIB_CPP_LOCAL := $(foreach d, $(LIB_DIRS_LOCAL),$(call wildcard,$d/*.cpp))
LIB_C_LOCAL := $(foreach d, $(LIB_DIRS_LOCAL),$(call wildcard,$d/*.c))
LIB_S_LOCAL := $(foreach d, $(LIB_DIRS_LOCAL),$(call wildcard,$d/*.S))
LIB_OBJ := $(LIB_CPP_SHARED:$(LIBS_SHARED_BASE)/%.cpp=$(LIB_BIN)/%.cpp.o) $(LIB_CPP_LOCAL:$(LIBS_LOCAL_BASE)/%.cpp=$(LIB_BIN)/%.cpp.o)
LIB_OBJ += $(LIB_C_SHARED:$(LIBS_SHARED_BASE)/%.c=$(LIB_BIN)/%.c.o) $(LIB_C_LOCAL:$(LIBS_LOCAL_BASE)/%.c=$(LIB_BIN)/%.c.o)
LIB_OBJ += $(LIB_S_SHARED:$(LIBS_SHARED_BASE)/%.S=$(LIB_BIN)/%.s.o) $(LIB_S_LOCAL:$(LIBS_LOCAL_BASE)/%.S=$(LIB_BIN)/%.s.o)
# Includes -------------------------------------------------------------
INCLUDE := -I./$(USR_SRC) -I$(CORE_SRC)
INCLUDE += $(foreach d, $(LIBS_LOCAL),-I$(LIBS_LOCAL_BASE)/$d/ -I$(LIBS_LOCAL_BASE)/$d/src -I$(LIBS_LOCAL_BASE)/$d/utility/)
INCLUDE += $(foreach d, $(LIBS_SHARED), -I$(LIBS_SHARED_BASE)/$d/ -I$(LIBS_SHARED_BASE)/$d/src -I$(LIBS_SHARED_BASE)/$d/utility/)
# Generate directories --------------------------------------------------------
DIRECTORIES := $(sort $(dir $(CORE_OBJ) $(USR_OBJ) $(LIB_OBJ)))
generateDirs := $(foreach d, $(DIRECTORIES), $(shell if not exist "$d" mkdir "$d"))
#$(info dirs: $(DIRECTORIES))
#******************************************************************************
# Rules:
#******************************************************************************
.PHONY: directories all rebuild upload uploadTy uploadCLI clean cleanUser cleanCore
all: $(TARGET_LST) $(TARGET_SYM) $(TARGET_HEX)
rebuild: cleanUser all
clean: cleanUser cleanCore cleanLib
#echo $(COL_OK)cleaning done$(COL_RESET)
upload: all
#$(UPL_PJRC)
uploadTy: all
#$(UPL_TYCMD)
uploadCLI: all
#$(UPL_CLICMD)
uploadJLink: all
#$(UPL_JLINK)
# Core library ----------------------------------------------------------------
$(CORE_BIN)/%.s.o: $(CORE_SRC)/%.S
#echo $(COL_CORE)CORE [ASM] $(notdir $<) $(COL_ERR)
#"$(CC)" $(S_FLAGS) $(INCLUDE) -o $# -c $<
$(CORE_BIN)/%.c.o: $(CORE_SRC)/%.c
#echo $(COL_CORE)CORE [CC] $(notdir $<) $(COL_ERR)
#"$(CC)" $(C_FLAGS) $(INCLUDE) -o $# -c $<
$(CORE_BIN)/%.cpp.o: $(CORE_SRC)/%.cpp
#echo $(COL_CORE)CORE [CPP] $(notdir $<) $(COL_ERR)
#"$(CXX)" $(CPP_FLAGS) $(INCLUDE) -o $# -c $<
$(CORE_LIB) : $(CORE_OBJ)
#echo $(COL_LINK)CORE [AR] $# $(COL_ERR)
#$(AR) $(AR_FLAGS) $# $^
#echo $(COL_OK)Teensy core built successfully &&echo.
# Shared Libraries ------------------------------------------------------------
$(LIB_BIN)/%.s.o: $(LIBS_SHARED_BASE)/%.S
#echo $(COL_LIB)LIB [ASM] $(notdir $<) $(COL_ERR)
#"$(CC)" $(S_FLAGS) $(INCLUDE) -o $# -c $<
$(LIB_BIN)/%.cpp.o: $(LIBS_SHARED_BASE)/%.cpp
#echo $(COL_LIB)LIB [CPP] $(notdir $<) $(COL_ERR)
#"$(CXX)" $(CPP_FLAGS) $(INCLUDE) -o $# -c $<
$(LIB_BIN)/%.c.o: $(LIBS_SHARED_BASE)/%.c
#echo $(COL_LIB)LIB [CC] $(notdir $<) $(COL_ERR)
#"$(CC)" $(C_FLAGS) $(INCLUDE) -o $# -c $<
# Local Libraries -------------------------------------------------------------
$(LIB_BIN)/%.s.o: $(LIBS_LOCAL_BASE)/%.S
#echo $(COL_LIB)LIB [ASM] $(notdir $<) $(COL_ERR)
#"$(CC)" $(S_FLAGS) $(INCLUDE) -o $# -c $<
$(LIB_BIN)/%.cpp.o: $(LIBS_LOCAL_BASE)/%.cpp
#echo $(COL_LIB)LIB [CPP] $(notdir $<) $(COL_ERR)
#"$(CXX)" $(CPP_FLAGS) $(INCLUDE) -o $# -c $<
$(LIB_BIN)/%.c.o: $(LIBS_LOCAL_BASE)/%.c
#echo $(COL_LIB)LIB [CC] $(notdir $<) $(COL_ERR)
#"$(CC)" $(C_FLAGS) $(INCLUDE) -o $# -c $<
# Handle user sources ---------------------------------------------------------
$(USR_BIN)/%.s.o: $(USR_SRC)/%.S
#echo $(COL_SRC)USER [ASM] $< $(COL_ERR)
#"$(CC)" $(S_FLAGS) $(INCLUDE) -o "$#" -c $<
$(USR_BIN)/%.c.o: $(USR_SRC)/%.c
#echo $(COL_SRC)USER [CC] $(notdir $<) $(COL_ERR)
#"$(CC)" $(C_FLAGS) $(INCLUDE) -o "$#" -c $<
$(USR_BIN)/%.cpp.o: $(USR_SRC)/%.cpp
#echo $(COL_SRC)USER [CPP] $(notdir $<) $(COL_ERR)
#"$(CXX)" $(CPP_FLAGS) $(INCLUDE) -o "$#" -c $<
# Linking ---------------------------------------------------------------------
$(TARGET_ELF): $(CORE_LIB) $(LIB_OBJ) $(USR_OBJ)
#echo $(COL_LINK)
#echo [LD] $# $(COL_ERR)
#$(CC) $(LD_FLAGS) -o "$#" $(USR_OBJ) $(LIB_OBJ) $(CORE_LIB) $(LIBS)
#echo $(COL_OK)User code built and linked to libraries &&echo.
%.lst: %.elf
#echo [LST] $#
#$(OBJDUMP) -d -S --demangle --no-show-raw-insn "$<" > "$#"
#echo $(COL_OK)Sucessfully built project$(COL_RESET) &&echo.
%.sym: %.elf
#echo [SYM] $#
#$(NM) $(NM_FLAGS) "$<" > "$#"
%.hex: %.elf
#echo $(COL_LINK)[HEX] $#
#$(OBJCOPY) -O ihex -R.eeprom "$<" "$#"
# Cleaning --------------------------------------------------------------------
cleanUser:
#echo $(COL_LINK)Cleaning user binaries...$(COL_RESET)
#if exist $(USR_BIN) rd /s/q "$(USR_BIN)"
#if exist "$(TARGET_LST)" del $(subst /,\,$(TARGET_LST))
cleanCore:
#echo $(COL_LINK)Cleaning core binaries...$(COL_RESET)
#if exist $(CORE_BIN) rd /s/q "$(CORE_BIN)"
#if exist $(CORE_LIB) del $(subst /,\,$(CORE_LIB))
cleanLib:
#echo $(COL_LINK)Cleaning user library binaries...$(COL_RESET)
#if exist $(LIB_BIN) rd /s/q "$(LIB_BIN)"
# compiler generated dependency info ------------------------------------------
-include $(CORE_OBJ:.o=.d)
-include $(USR_OBJ:.o=.d)
-include $(LIB_OBJ:.o=.d)
This info may or may not not be necessary: I'm running a Teensy 3.2 on Visual Studio Code using Visual Teensy and TyCommander

How to output .o files into one specific folder?

I'm currently using MinGW64, G++, and a makefile to compile my c++ project on VSCode. I have two src directories src and src/vendor/imGui containing .cpp files that I compile. As of now, the makefile is able to compile both src folders. However, the .o files are outputted in their respective src folders. How can I make it so that every .o file generated gets sent to one specific folder/directory?
Current Makefile:
CXX = g++
CXXFLAGS := -std=c++17 -Wall -Wextra -g
LFLAGS += -LC:/mingw64/x86_64-w64-mingw32/bin
LFLAGS += -LC:/mingw64/x86_64-w64-mingw32/lib
OUTPUT := output
SRC := src
SRC += src/vendor/imGui
INCLUDE := include
LIB := lib
ifeq ($(OS),Windows_NT)
MAIN := main.exe
SOURCEDIRS := $(SRC)
INCLUDEDIRS := $(INCLUDE)
LIBDIRS := $(LIB)
FIXPATH = $(subst /,\,$1)
RM := del /q /f
MD := mkdir
else
MAIN := main
SOURCEDIRS := $(shell find $(SRC) -type d)
INCLUDEDIRS := $(shell find $(INCLUDE) -type d)
LIBDIRS := $(shell find $(LIB) -type d)
FIXPATH = $1
RM = rm -f
MD := mkdir -p
endif
INCLUDES := $(patsubst %,-I%, $(INCLUDEDIRS:%/=%))
INCLUDES += -IC:/Users/kimda/Desktop/Projects/C++/Libraries/glm
INCLUDES += -IC:/mingw64/x86_64-w64-mingw32/include
INCLUDES += -IC:/Users/kimda/Desktop/Projects/C++/OpenGL/src/vendor/imGui
LIBS := $(patsubst %,-L%, $(LIBDIRS:%/=%))
LIBS += -lglew32
LIBS += -lopengl32
LIBS += -lglfw3
LIBS += -lgdi32
LIBS += -lglu32
SOURCES := $(wildcard $(patsubst %,%/*.cpp, $(SOURCEDIRS)))
OBJECTS := $(SOURCES:.cpp=.o)
OUTPUTMAIN := $(call FIXPATH,$(OUTPUT)/$(MAIN))
all: $(OUTPUT) $(MAIN)
#echo Building complete!
$(OUTPUT):
#$(MD) $(OUTPUT)
$(MAIN): $(OBJECTS)
#$(CXX) $(CXXFLAGS) $(INCLUDES) -o $(OUTPUTMAIN) $(OBJECTS) $(LFLAGS) $(LIBS)
.cpp.o:
#$(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $#
.PHONY: clean
clean:
#$(RM) $(OUTPUTMAIN)
#$(RM) $(call FIXPATH,$(OBJECTS))
#echo Cleanup complete!
run: clean all
#./$(OUTPUTMAIN)
#echo Executing complete!
If I understand you correctly you want to send all object files to some 'build' directory.
I usually do something like this
BUILDDIR := build
[...]
OBJECTS := $(addprefix $(BUILDDIR)/,$(SOURCES:.cpp=.o))
[...]
and then change your
.cpp.o:
#$(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $#
to
$(BUILDDIR)/%.o: %.cpp
#$(MD) $(dir $#)
#$(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $#

C++ Make file issue "No rule to make target ..."

I just started working with makefile.
I am getting the error with makefile of my C++ project.
make: No rule to make target 'bin/smartCart_app', needed by all.
Below is the directory structure and files associated with it.
VBOX:~BASE$ls
build src
VBOX:~BASE$cd build
VBOX:~BASE/build$ ls
bin build Makefile.sc
VBOX:~BASE/build$ cd ../src
VBOX:~BASE/src$ls
baseStation.cpp config util //config and util has header and cpp files
Here is my Make file
CC=gcc
CPP=g++
CCFLAGS=-g -Wall -std=gnu+0x -o0
CC_LDFLAGS = -g -Wl
BUILD=./build
BIN = ./bin
SC_SRC_ROOT = ../src
SC_SRC_SUBDIRS = config util
SC_SRC_RELDIRS = $(addprefix $(strip $(SC_SRC_ROOT)), $(strip $(SC_SRC_SUBDIRS)))
SC_SRCS_ = $(shell /usr/bin/find $(SC_SRC_ROOT)/config $(SC_SRC_ROOT)/util -name "*.cpp")
SC_SRCS = $(SC_SRCS_) baseStation.cpp
SC_NEW_SRCS = $(notdir $(SC_SRCS))
SPACE :=
SPACE +=
INCLUDE = $(addprefix -I, $(SRC_RELDIRS))
VPATH = $(subst $(SPACE),:,$(SRC_RELDIRS)) $(subst $(SPACE),:,$(SC_SRC_RELDIRS))
SC_INCLUDE = $(addprefix -I, $(SC_SRC_RELDIRS))
SC_OBJS = $(SC_NEW_SRCS:%.cpp=$(BUILD)/%.o)
SC_DEPS = $(SC_OBJS:%.o=%.d)
# Ensure paths exist.
$(shell [ -d "$(BIN)" ] || mkdir -p $(BIN))
$(shell [ -d "$(BUILD)" ] || mkdir -p $(BUILD))
# Explicit rules.
.PHONY: all clean $(PHONY)
all: $(BIN)/smartCart_app
clean:
#echo "Cleaning..."
-#rm -f $(BIN)/smartCart_app $(SC_OBJS)
$(SC_OBJS): $(BUILD)/%.o: %.cpp
#echo "Compiling $(notdir $<)"
#$(CPP) $(CCFLAGS) $(INCLUDE) $(SC_INCLUDE) -MD -c -o $# $<
$(MACRO_DEPS):
-#rm -f $(patsubst %_ON.d,%_OFF.d,$#) $(patsubst %_OFF.d,%_ON.d,$#) $#
#touch $#
#$(ALL_OBJS): %.o: %.d
$(DEPS): $(BUILD)/%.d: %.cpp
#$(CC) $< -MM -MF $# $(INCLUDE)

Huge c++ project makefile by hand

I'm using CODEO (Eclipse + overlay) to build an app for the ELinOS RTOS.
Thing is, I lose the ability to auto-generate the Makefile using eclipse CDT and I have a generic Makefile alreayd generated.
Instead of typing this huge Makefile (Lots and lots of include and sources directories), I tried things with addprefix patsubst and some wildcard but that didn't work out, mainly because I suck with generic makefile !
Regarding libraries, only a few are used, so I'll write them by hand.
Here is how my code is structured.
Top directory
|
---> common ---> inc/
| src/
---> pack_AAA ---> comp_AAA ---> inc/
| | src/
| ----> comp_AAB ---> inc/
| src/
---> comp_BBB ---> inc/
| src/
---> main.cpp
I'd like to enumerate only the top folders (common, pack_AAA, comp_BBB and so on, mostly because not every TOP folder has to be compiled, my Makefile is in one of these top folder).
Below there is a variable amount of subdirectories. Leafs are ALWAYS inc/ (.hpp files) and src/ (as you might guess, .cpp files) folders.
Moreover, the amount of TOP folders won't change, so I mind declaring them, but subfolders will.
Objects files can be anywhere they want (separate top build/ directory, within src/ ...) as long as the resulting executable, let's call it HUGE_PROJECT is in the top directory
Thanks in advance !
SOKS
Here is the generated makefile that I have to start with
-include makefile.init
RM := rm -rf
PROG = HUGE_PROJECT_EXECUTABLE
MODULES := common comp_AAA comp_BBB pack_AAA pack_BBB pack_CCC
SRC_DIRS := $(addprefix ../../,$(MODULES))
#BUILD_DIR := $(addprefix ../../build/,$(MODULES))
#Original non-recursive & working example
OBJS = $(patsubst %.cpp,%.o,$(wildcard ./src/*.cpp))
DEPS = $(patsubst %.cpp,%.d,$(wildcard ./src/*.cpp))
#bad attempt
#SRCS := $(foreach sdir,$(SRC_DIRS),$(wildcard $(sdir)/*.cpp))
#OBJS := $(patsubst %.cpp,%.o,$(SRCS))
#INCLUDES := $(addprefix -I,$(SRC_DIR))
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(strip $(DEPS)),)
-include $(DEPS)
endif
endif
-include makefile.defs
# All Target
all: $(PROG).tgz
$(PROG).tgz: $(PROG).mkefs $(PROG)
#echo 'Building file: $#'
#echo 'Invoking: Embeddable file system generator'
mkefs -a -s -o"$#" -f"$<"
#echo 'Finished building: $#'
#echo ' '
$(PROG): $(OBJS) $(USER_OBJS)
#echo 'Building target: $#'
#echo 'Invoking: ELinOS C/C++ Linker'
$(CXX) -o "$(PROG)" $(SRCS) $(USER_OBJS) $(LIBS)
#echo 'Finished building target: $#'
#echo ' '
clean:
-$(RM) $(OBJS) $(DEPS) $(PROG) $(PROG).tgz
-#echo ' '
./src/%.o: ./src/%.cpp
#echo 'Building file: $<'
#echo 'Invoking: ELinOS C/C++ Compiler'
$(CXX) -I./include -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(#:%.o=%.d)" -MT"$(#:%.o=%.d)" -o "$#" "$<"
#echo 'Finished building: $<'
#echo ' '
.PHONY: all clean dependents
.SECONDARY:
-include makefile.targets
Here is the Makefile I ended up with.
I had to hide components name.
#Executables
RM := rm -rf
CXX=YOUR_CXX_COMPILER
CC=YOUR_CC_COMPILER
PROG=YOUR_PROGRAM
#Root directory of your program (all files should be in it)
ROOT_DIR := ../../
#####################################
########################################
# Define here all directories #
# containing src/ and inc/ folders #
########################################
#pack_AAA folders
#Modules containing both src/ and inc/.
AAA_MODULES := moduleA moduleB
#Modules with an include folder only + module with both
AAA_DIRS_INC := moduleD moduleEF $(AAA_MODULES)
#pack_BBB modules list
BBB_MODULES := comp_A comp_B comp_C comp_B/comp_D comp_B/comp_E
MMM_MODULES := comp_F comp_G comp_H
PPP_MODULES := . I
CCC_MODULES := J comp_K comp_L \
comp_L/comp_M \
$(addprefix pack_BBB/,$(BBB_MODULES)) \
$(addprefix pack_MMM/,$(MMM_MODULES)) \
$(addprefix comp_PPP/,$(PPP_MODULES))
#pack_DDD
DDD_MODULES := M comp_N comp_N/comp_O
#comp_EEE
EEE_MODULES := . P Q
OTHER_MODULES := R S comp_T
#All folders containing src/ subdir
SRC_MODULES := $(OTHER_MODULES) \
$(addprefix comp_EEE/,$(EEE_MODULES)) \
$(addprefix pack_DDD/,$(DDD_MODULES)) \
$(addprefix pack_AAA/,$(AAA_MODULES)) \
$(addprefix pack_CCC/, $(CCC_MODULES))
#All folders containing inc/ subdir
INCLUDE_MODULES := $(OTHER_MODULES) \
$(addprefix comp_EEE/,$(EEE_MODULES)) \
$(addprefix pack_DDD/,$(DDD_MODULES)) \
$(addprefix pack_AAA/,$(AAA_DIRS_INC)) \
$(addprefix pack_CCC/, $(CCC_MODULES))
#####################################
#WRAPPERS
ROOT_DIR_WRAPPERS := $(ROOT_DIR)../WRAPPERS/
LIST_WRAPPERS := WA WB WC WD
SRC_WRAPPERS := $(addprefix pack_Wrappers/csc_Wrapper,$(LIST_WRAPPERS))
#DRIVERS
LIST_DRIVERS := DA DB DC DD
DIR_DRIVERS := $(addprefix pack_Drivers/csc_Driver,$(LIST_DRIVERS))
SRC_DIRS_WRAPPERS := $(addsuffix /src,$(SRC_WRAPPERS))
INCLUDE_DIRS_WRAPPERS := $(addsuffix /inc,$(SRC_WRAPPERS)) \
$(addsuffix /inc,$(DIR_DRIVERS)) \
Common
#####################################
#LIB_1 INCLUDE
ROOT_DIR_LIB_1 := $(ROOT_DIR)../../ObjetsCompiles/LIB_1/
INCLUDE_DIRS_LIB_1 := dir1 dir2 dir3
#####################################
#LIB_2 INCLUDE
ROOT_DIR_LIB_2 := $(ROOT_DIR)../../ObjetsCompiles/LIB_2/include
INCLUDE_DIRS_LIB_2 := . dir1 dir2 dir3/subdir dir4
#####################################
#Add "." to find main.cpp
SRC_DIRS := $(addsuffix /src,$(SRC_MODULES)) .
INCLUDE_DIRS_SRC:= $(addsuffix /inc,$(INCLUDE_MODULES)) pack_ZZZ/comp_Without_Inc_Folder
#All include dirs, LIB_1 + LIB_2
INCLUDE_DIRS := $(addprefix -I$(ROOT_DIR),$(INCLUDE_DIRS_SRC)) \
$(addprefix -I$(ROOT_DIR_LIB_1),$(INCLUDE_DIRS_LIB_1)) \
$(addprefix -I$(ROOT_DIR_LIB_2),$(INCLUDE_DIRS_LIB_2))
INCLUDE_DIRS_CC := $(addprefix -I$(ROOT_DIR_WRAPPERS),$(INCLUDE_DIRS_WRAPPERS)) \
$(addprefix -I$(ROOT_DIR_LIB_2),$(INCLUDE_DIRS_LIB_2))
#All objects (all .cpp & all .c)
OBJS := $(foreach dir,$(SRC_DIRS), \
$(patsubst %.cpp,%.o,$(wildcard $(ROOT_DIR)$(dir)/*.cpp))) \
$(foreach dirs,$(SRC_DIRS_WRAPPERS), \
$(patsubst %.c,%.o,$(wildcard $(ROOT_DIR_WRAPPERS)$(dirs)/*.c)))
#All dependencies (same list as OBJS)
DEPS := $(foreach dir,$(SRC_DIRS), \
$(patsubst %.cpp,%.d,$(wildcard $(ROOT_DIR)$(dir)/*.cpp))) \
$(foreach dirs,$(SRC_DIRS_WRAPPERS), \
$(patsubst %.c,%.d,$(wildcard $(ROOT_DIR_WRAPPERS)$(dirs)/*.c)))
#Exclude files from build
#Win32 socket files + some misc
EXCLUDED_FILES_LIST := pack_AAA/comp_BBB/src/SocketWindowsOnLinuxForExample.cpp \
pack_BBB/comp_CCC/src/SomeOtherThingForWindows.cpp
EXCLUDED_FILES_PATH := $(addprefix $(ROOT_DIR),$(EXCLUDED_FILES_LIST))
#Remove some objects (compilation is done, but no link)
EXCLUDED_OBJS := $(patsubst %.cpp,%.o,$(EXCLUDED_FILES_PATH))
EXCLUDED_DEPS := $(patsubst %.cpp,%.d,$(EXCLUDED_FILES_PATH))
#Update OBJS & DEPS list with excluded files
OBJS := $(filter-out $(EXCLUDED_OBJS),$(OBJS))
DEPS := $(filter-out $(EXCLUDED_DEPS),$(DEPS))
#FLAGS for DEBUG and RELEASE compilation
CPPFLAGS := -D__CPPFLAG1__ -D__CPPFLAG2__
CFLAGS_D := -O0 -g2 -Wall -c -fmessage-length=0
CXXFLAGS_D := -O0 -g2 -Wall -c -fmessage-length=0 -m32 -mcpu=powerpc -Winline
CFLAGS_R := -O2 -g0 -Wall -c -fmessage-length=0
CXXFLAGS_R := -O2 -g0 -Wall -c -fmessage-length=0 -m32 -mcpu=powerpc -Winline
# L
LD_PATH := -L../../path_library_1 -L../../path_library_2
LD_LIBS := -lpthread_rt -lROHC -lpthread -lnative -lrtdm -lrt
MAP := $(PROG).map
# -s (to strip)
LD_FLAGS_D := -Wl,--wrap,pthread_create -Wl,--wrap,pthread_kill -Wl,-Map,$(MAP)
LD_FLAGS_R := -s -Wl,--wrap,pthread_create -Wl,--wrap,pthread_kill -Wl,-Map,$(MAP)
##################################################
#TARGETS
default: release
#Debug flags (-00 + -g2)
debug: CFLAGS := $(CFLAGS_D)
debug: CXXFLAGS := $(CXXFLAGS_D)
debug: LD_FLAGS := $(LD_FLAGS_D)
debug: all
#Release flags (-s + -02 + -g0)
release: CFLAGS := $(CFLAGS_R)
release: CXXFLAGS := $(CXXFLAGS_R)
release: LD_FLAGS := $(LD_FLAGS_R)
release: all
all: $(PROG)
# Tool invocations
$(PROG): $(OBJS)
#echo 'Building target: $#'
#$(CXX) $(LD_FLAGS) $(LD_PATH) -o "$(PROG)" $(OBJS) $(LD_LIBS)
#echo 'Finished building target: $#'
#echo ' '
# CXX compiler used by most of the code
%.o: %.cpp
#echo 'Building file: $<'
#$(CXX) $(CPPFLAGS) $(INCLUDE_DIRS) $(CXXFLAGS) -MMD -MP -MF"$(#:%.o=%.d)" -MT"$(#:%.o=%.d)" -o "$#" "$<"
# CC compiler used by Wrappers only
%.o: %.c
#echo 'Building file: $<'
#$(CC) $(INCLUDE_DIRS_CC) $(CFLAGS) -MMD -MP -MF"$(#:%.o=%.d)" -MT"$(#:%.o=%.d)" -o "$#" "$<"
# Other Targets
clean:
-$(RM) $(OBJS) $(DEPS) $(PROG) $(MAP)
-#echo ' '
.PHONY: all clean dependents
.SECONDARY:

How to create multiple executables using makefile from a single target

I am trying to build excutables for multiple files which are built in the same way. When i run make all the excutables should be generated. I am getting error at prerequisites part of the macro.
CXX = g++
CXX_FLAGS = -g -Wall
LD_FLAGS =
INC_DIR = -I/my/path/include
SRC_DIR = .
LIB_DIR = -L$/my/path/lib
OBJ_DIR = obj
EXE_DIR = exe
SRCS := $(foreach s_dir, $(SRC_DIR), $(wildcard $(s_dir)/*.cpp))
OBJS := $(patsubst $(SRC_DIR)/%.cpp, $(OBJ_DIR)/%.o, $(SRCS))
EXES := $(patsubst $(SRC_DIR)/%.cpp, $(EXE_DIR)/%.out, $(SRCS))
all: create_directories create_objects create_exes
create_directories:
#echo "Creating $(OBJ_DIR) and $(EXE_DIR)..."
#mkdir -p obj
#mkdir -p exe
create_objects:
$(foreach b_dir, $(OBJ_DIR), $(eval $(call build-objects, $(b_dir))))
create_exes:
$(foreach ot, $(EXE_DIR), $(eval $(call build-exes, $(ot))))
define build-objects
$1/%.o: %.cpp
$(CXX) $(CXX_FLAGS) $(INC_DIR) -MMD -MP -c $$< -o $$#
endef
define build-exes
$1/%.out:obj/%.o
$(CXX) $(LD_FLAGS) -o $# $(OBJS) $(LIB_DIR) -lmylib
endef
Is this a right way to do generate multiple exes or any other simple way?
If I'm reading this makefile right, then it's much too complicated.
First let's have a rule to build object files:
$(OBJ_DIR)/%.o: %.cpp
$(CXX) $(CXX_FLAGS) $(INC_DIR) -MMD -MP -c $< -o $#
Now if we're not sure about the existence of obj/, we could add a rule to create it, but for the moment let's just put in a failsafe (we'll come back to this later):
$(OBJ_DIR)/%.o: %.cpp
#mkdir -p $(OBJ_DIR)
$(CXX) $(CXX_FLAGS) $(INC_DIR) -MMD -MP -c $< -o $#
And a similar rule to build the executables:
$(EXE_DIR)/%.out: $(OBJ_DIR)/%.o
#mkdir -p $(EXE_DIR)
$(CXX) $(LD_FLAGS) -o $# $^ $(LIB_DIR) -lmylib
And finally (at the top) some variables, the lists of files, and the all rule:
CXX = g++
CXX_FLAGS = -g -Wall
LD_FLAGS =
INC_DIR = -I/my/path/include
SRC_DIR = .
LIB_DIR = -L$/my/path/lib
OBJ_DIR = obj
EXE_DIR = exe
SRCS := $(wildcard $(SRC_DIR)/*.cpp)
EXES := $(patsubst $(SRC_DIR)/%.cpp, $(EXE_DIR)/%.out, $(SRCS))
# Let the object files take care of themselves
all: $(EXES)
That's all you need. Once this is working perfectly, we can discuss refinements like rules for building directories.