Makefile: File doesn't exist when the file does exist - c

# Arquitectura
CC := arm-none-eabi-
# Compiladores
GCC :=$(CC)gcc
AS :=$(CC)as
LIBC :=$(CC)ar
# Linker
LINKER :=$(CC)ld
# Flags compilação
FLAGDEBUG :=-g
CFLAGS :=-c -mapcs #-Wall
CDEPEND :=-MM -MF
LIBFLAG := rcs
LDSCRIPT := -T
LDLIBDIR := -L
LDLIBLIBS := -l
# $# -> nome do alvo .. $^/S? lista de dependencias .. $* contêm o valor de % .. $< contêm a primeira dependencia
# Pastas
DEPDIR := Depends/
SOURCEDIR := Source/
SOURCECDIR :=$(SOURCEDIR)C/
SOURCEADIR :=$(SOURCEDIR)Assembly/
LIBDIR := Library/
HEADDIR := Header/
OBJDIR := Object/
# Dependencias
SOURCEC := $(wildcard $(SOURCECDIR)*.c)
SOURCEA := $(wildcard $(SOURCEADIR)*.S)
OBJC :=$(patsubst $(SOURCECDIR)%.c,%.o,$(SOURCEC))
OBJA :=$(patsubst $(SOURCEADIR)%.S,%.o,$(SOURCEA))
HEADER :=$(wildcard $(HEADDIR)*.h)
LIBL = $(wildcard $(LIBDIR)*.a)
#LIBL = $(wildcard $(LIBDIR)lib*.a)
DEPEND :=$(patsubst %.o,$(DEPDIR)%.d,$(OBJC))
SCRIPT := $(wildcard *.ld)
LIB =
# Ficheiros de output
FICHDEBUG := Teste.axf
FICHRELEASE := Release.axf
debug: $(FICHDEBUG)
# Executável para debug
$(FICHDEBUG):$(OBJA) $(OBJC)
#echo A efectuar a linkagem dos módulos para gerar o executável $#
#$(LINKER) $(OBJA) $(OBJC) $(LDSCRIPT) $(SCRIPT) -o $#
# Compilar ficheiros .S e .c
%.o:$(SOURCEADIR)%.S
#echo A compilar $# a partir de $*.S
#$(AS) $(FLAGDEBUG) $< -o $#
%.o:$(SOURCECDIR)%.c
#echo A compilar $# a partir de $*.c
$(GCC) $(CDEPEND) $(patsubst %.o,$(DEPDIR)%.d,$#) $<
$(GCC) $(FLAGDEBUG) $(CFLAGS) $< -o $#
-include $(DEPEND)
release: $(FICHRELEASE)
$(FICHRELEASE): $(OBJA) $(OBJC)
#echo A efectuar a linkagem dos módulos para gerar o executável $#
#$(LINKER) $(OBJA) main.o $(LDSCRIPT) $(SCRIPT) -o $# $(LDLIBDIR) $(LIBDIR) $(LDLIBLIBS)
# Gerar bibliotecas necessárias
gerarLib:$(HEADER)
#echo $(HEADER)
$(HEADDIR)%.h: %.o
#echo $# $*
#$(eval LIB = $(patsubst $(HEADDIR)%.h,$(LIBDIR)lib%.a,$#))
$(LIBC) $(LIBFLAG) $(LIB) $(patsubst $(LIBDIR)lib%.a,%.o,$(LIB))
.PHONY: clean
clean:
# rm -rf $(DEPDIR)*.d $(SOURCEDIR)*/*~ *~ *.o $(LIBDIR)*.a *.axf *.o
So heres the makefile I'm using. As it is it generates the code as its supposed ant it updates correctly when any file changes(.h or .c), the issue is the dependencies generated do not have the proper object path set(I've read its a bug in gcc when it writes to file the dependencies) so I thought I'd make a script that simply adds the path to it and it would work and I'd be able to put my object files where they belong(Object folder) however every time I try to run make after the initial compilation(which works fine btw) it gives me an erro saying one of the header files doesn't exist(which is untrue since the file is there and I can access it).
Anyone have an idea of whats happening?(to change between the "modes" I simply write $(OBJDIR) before every %.o and *.o)

This rule looks suspicious:
$(HEADDIR)%.h: %.o
#echo $# $*
#$(eval LIB = $(patsubst $(HEADDIR)%.h,$(LIBDIR)lib%.a,$#))
$(LIBC) $(LIBFLAG) $(LIB) $(patsubst $(LIBDIR)lib%.a,%.o,$(LIB))
It claims to be creating .h files from .o files, but that seems unlikely, given the rules.

Related

dyld: Library not loaded when Linked in makefile

Hi I know there is some other posts with the same title, but they are not in the same context. So: I have a library named mlx and the library file is named libmlx.dylib: when I try to compile it with a simple main using gcc main.c libmlx.dylib it compiles well and the lib is working, but when I try to compile it with my project using my makefile the compilation doesn't throw any error but when I launch the program I get this error message:
dyld: Library not loaded: libmlx.dylib
Referenced from: /Users/leo/Documents/42-cursus/so_long/./so_long
Reason: image not found
[1] 8313 abort ./so_long
Here is my makefile:
SHELL = /bin/sh
NAME = so_long
.SUFFIXES = .c .o .h .dylib
SRCDIR = src
INCDIR = inc
LIBDIR = lib
OBJDIR = .obj
SRC = $(addsuffix $(word 1, $(.SUFFIXES)),\
$(addprefix exception/,\
exception\
bad_alloc\
invalid_arguments\
invalid_map\
runtime_error)\
$(addprefix parsing/,\
get_map)\
$(addprefix rendering/,\
render_map)\
$(addprefix utils/,\
init_image\
make_color)\
$(addprefix cleaning/,\
mlx_clear)\
$(addprefix get_next_line/,\
get_next_line\
get_next_line_utils)\
main)
INC = $(addsuffix $(word 3, $(.SUFFIXES)),\
get_next_line\
mlx\
exception\
so_long)
LIB = mlx\
ft
OBJ = $(SRC:$(word 1, $(.SUFFIXES))=$(word 2, $(.SUFFIXES)))
CC = gcc
CFLAGS = -Wall -Wextra -Werror -I $(INCDIR)
LCFLAGS = $(addprefix -L, $(LIBDIR)) $(addprefix -l, $(lib))
#### COLORS ####
KNRM = \x1B[0m
KRED = \x1B[31m
KGRN = \x1B[32m
KYEL = \x1B[33m
KBLU = \x1B[34m
KMAG = \x1B[35m
KCYN = \x1B[36m
KWHT = \x1B[37m
######################
all: $(OBJDIR) $(NAME)
#printf "$(KGRN)\`$(NAME)\` is up to date.\n$(KNRM)"
$(OBJDIR):
#printf "$(KYEL)➤ "
mkdir -p $#/exception $#/parsing $#/rendering $#/utils $#/cleaning $#/get_next_line
#printf "$(KNRM)"
$(NAME): $(addprefix $(OBJDIR)/, $(OBJ))
#printf "$(KCYN)[ Linking ]\n➤ "
$(CC) $(CFLAGS) $^ -o $# $(LCFLAGS)
#printf "$(KNRM)"
$(OBJDIR)/%$(word 2, $(.SUFFIXES)): $(SRCDIR)/%$(word 1, $(.SUFFIXES)) $(addprefix $(INCDIR)/, $(INC))
#printf "$(KMAG)[ Compiling ]\n➤ "
$(CC) $(CFLAGS) -c $< -o $#
#printf "$(KNRM)"
clean:
#printf "$(KRED)➤ "
rm -rf $(OBJDIR)
#printf "$(KNRM)"
fclean: clean
#printf "$(KRED)➤ "
rm -f $(NAME)
#printf "$(KNRM)"
re: fclean all
I'm linking it with the flags -L $(LIBDIR) -lmlx. What did I do wrong ?
I found that for dynamic libraries you need to have the .dylib in the same directory as your target, I let the post in case some people face the same problem.

How to compile a static library using makefile

I want to create a static library using Makefile in linux.
I want to know if this makefile true or not ?
I'm open to other ideas. Thanks all.
NAME= libamqp.a
CC := gcc
CFLAGS := -c -Wall -g
SRCDIR := src
OBJDIR := obj
#definition des fichiers headers
INCLUDES := -I/usr/local/include/ -I/home/tvi/projets/RabbitMQ/libamqp/dlo -I/home/tvi/projets/RabbitMQ/rabbitmq-c/librabbitmq
#definition des librairies
LFLAGS := -L/usr/local/lib64
LDFLAGS :=
LIBS := -L. -lrabbitmq
SRCS_RAW := amqp_connexion.c amqp_consommateur.c amqp_deconnexion.c amqp_producteur.c amqp_outils.c
SRCS := $(addprefix $(SRCDIR)/,$(SRCS_RAW))
OBJS := $(addprefix $(OBJDIR)/,$(SRCS_RAW:.c=.o))
.PHONY: all
all: $(NAME)
#echo "$(MAKE) : Tout est généré"
$(NAME):$(OBJS)
$(CC) $(CFLAGS) $(SRCS) $(INCLUDES) $(LFLAGS) $(LIBS)
ar rcs $(NAME) $(OBJS)
ranlib $(NAME)
$(OBJS): $(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR)
$(CC) $(CFLAGS) -c $< -o $# $(INCLUDES)
.PHONY: clean
clean:
$(RM) *~ $(NAME)
$(RM) -r $(OBJDIR)
NAME= libamqp.a
CC := gcc
CFLAGS := -Wall -g
SRCDIR := src
OBJDIR := obj
#definition des fichiers headers
INCLUDES := -I/usr/local/include/ -I/home/tvi/projets/RabbitMQ/libamqp/dlo -I/home/tvi/projets/RabbitMQ/rabbitmq-c/librabbitmq
#definition des librairies
LFLAGS := -L/usr/local/lib64
LDFLAGS :=
LIBS := -L. -lrabbitmq
SRCS_RAW := amqp_connexion.c amqp_consommateur.c amqp_deconnexion.c amqp_producteur.c amqp_outils.c
SRCS := $(addprefix $(SRCDIR)/,$(SRCS_RAW))
OBJS := $(addprefix $(OBJDIR)/,$(SRCS_RAW:.c=.o))
#$(info DEBUG: SRCS=$(SRCS))
#$(info DEBUG: OBJS=$(OBJS))
#$(info DEBUG: EXEC=$(EXEC))
.PHONY: all
all: $(NAME)
#echo "$(MAKE) : Tout est genere"
$(NAME):$(OBJS)
ar rcs $(NAME) $(OBJS)
ranlib $(NAME)
#règle pour créer un répertoire d'objets s'il n'existe pas
$(OBJDIR):
mkdir $(OBJDIR)
#définir une règle implicite pour créer des objets dans leur propre répertoire
#(note - ordre uniquement la dépendance sur le répertoire d'objets)
#$< :premier_dependance $#:cible
$(OBJS): $(OBJDIR)/%.o: $(SRCDIR)/%.c | $(OBJDIR)
$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $#
.PHONY: clean
clean:
$(RM) *~ $(NAME)
$(RM) -r $(OBJDIR)

clang makefile "ar: no archive members specified"

I refactor some C code and I try to compile it as a lib on clang on mac instead of gcc on linux.
First of all if I just run make command I got an error at the first file compile:
+++ Compiling [sys_msg.c]
clang: error: unsupported argument '-adhlns=../../src/sys_msg.lst' to option 'Wa,'
So I remove the -Wa,-adhlns=$(<:.c=.lst) option that create this error.
After that everything seems to compile but the assembly of the last .a file fail, and I don't understand why.
Here is my make output :
+++ Creation of [../../hal/stub/obj]
+++ Dependencies of [hal.c]
+++ Dependencies of [target.c]
+++ Dependencies of [robus.c]
+++ Dependencies of [reception.c]
+++ Dependencies of [sys_msg.c]
+++ Compiling [sys_msg.c]
+++ Compiling [reception.c]
+++ Compiling [robus.c]
+++ Compiling [target.c]
+++ Compiling [hal.c]
ar -rv ../../hal/stub/libstub.a
ar: no archive members specified
usage: ar -d [-TLsv] archive file ...
ar -m [-TLsv] archive file ...
ar -m [-abiTLsv] position archive file ...
ar -p [-TLsv] archive [file ...]
ar -q [-cTLsv] archive file ...
ar -r [-cuTLsv] archive file ...
ar -r [-abciuTLsv] position archive file ...
ar -t [-TLsv] archive [file ...]
ar -x [-ouTLsv] archive [file ...]
make: *** [../../hal/stub/libstub.a] Error 1
Here is my makefile:
# make all = Make software and program
# make clean = Clean out built project files.
# make program = Download the hex file to the device, using avrdude. Please
# customize the avrdude settings below first!
# make docs = compile with doxygen the code documentation
# Maximum I2C speed (HZ)
SCLFREQ = 400000
#-------------------------------------------------------------------------------
# Tools
#-------------------------------------------------------------------------------
# Set DEBUG variable for once if not coming from command line
ifndef DEBUG
DEBUG = 0
endif
# Tool suffix when cross-compiling
CROSS_COMPILE ?=
# Compilation tools
CC = $(CROSS_COMPILE)gcc
AR = $(CROSS_COMPILE)ar
SIZE = $(CROSS_COMPILE)size
STRIP = $(CROSS_COMPILE)strip
OBJCOPY = $(CROSS_COMPILE)objcopy
OBJDUMP = $(CROSS_COMPILE)objdump
SIZE = $(CROSS_COMPILE)size
GDB = $(CROSS_COMPILE)gdb
NM = $(CROSS_COMPILE)nm
ROOT_PATH = ../..
HAL_PATH = $(ROOT_PATH)/hal/$(BOARD)
DOC_PATH = $(ROOT_PATH)/../extra/Docs
# Doxygen configuration file name
DOXYFILE = $(ROOT_PATH)/../extra/.Doxyfile
OBJ_PATH = $(HAL_PATH)/obj
OUTPUT_NAME = lib$(BOARD)
OUTPUT_FILE_PATH = $(HAL_PATH)/$(OUTPUT_NAME).a
#|---------------------------------------------------------------------------------------|
#| Source files |
#|---------------------------------------------------------------------------------------|
include ../sources.mk
#|---------------------------------------------------------------------------------------|
#| Extract file names and path |
#|---------------------------------------------------------------------------------------|
PROJ_ASRCS = $(filter %.s,$(foreach file,$(SOURCES),$(file)))
PROJ_ASRCS += $(filter %.S,$(foreach file,$(SOURCES),$(file)))
PROJ_CSRCS = $(filter %.c,$(foreach file,$(SOURCES),$(file)))
PROJ_CPPSRCS = $(filter %.cpp,$(foreach file,$(SOURCES),$(file)))
#|---------------------------------------------------------------------------------------|
#| Set important path variables |
#|---------------------------------------------------------------------------------------|
VPATH = $(foreach path,$(sort $(foreach file,$(SOURCES),$(dir $(file)))),$(path) :)
INC_PATH = $(INCLUDES)
LIB_PATH = -L$(dir $(RESOURCES_LINKER))
#|---------------------------------------------------------------------------------------|
#| Options for compiler binaries |
#|---------------------------------------------------------------------------------------|
COMMON_FLAGS = \
-g -O$(OPT) \
-funsigned-char -fpack-struct -fshort-enums \
-Wall -Wstrict-prototypes \
-DMCU=$(MCU) \
-DMAINCLOCK=$(MAINCLOCK) \
-DSCLFREQ=$(SCLFREQ) \
$(INCLUDES)
ifeq ($(CROSS_COMPILE),avr-)
COMMON_FLAGS += -mmcu=$(MCU)
ASFLAGS = -mmcu=$(MCU)
endif
CFLAGS += $(COMMON_FLAGS) -std=gnu99
CPPFLAGS = $(COMMON_FLAGS) -std=gnu++11 -fno-rtti -fno-exceptions
ASFLAGS += -Wa,-adhlns=$(<:.S=.lst),-gstabs -I. -x assembler-with-cpp
LDFLAGS = -Wl,-Map=$(BOARD).map,--cref
#|---------------------------------------------------------------------------------------|
#| Define targets |
#|---------------------------------------------------------------------------------------|
#AOBJS += $(patsubst %.S,%.o,$(PROJ_ASRCS))
AOBJS = $(patsubst %.s,%.o,$(addprefix $(OBJ_PATH)/, $(notdir $(PROJ_ASRCS))))
COBJS = $(patsubst %.c,%.o,$(addprefix $(OBJ_PATH)/, $(notdir $(PROJ_CSRCS))))
CPPOBJS = $(patsubst %.cpp,%.o,$(addprefix $(OBJ_PATH)/, $(notdir $(PROJ_CPPSRCS))))
.PHONY: all clean print_info packaging
all: $(OUTPUT_FILE_PATH)
print_info:
#echo DEFAULT_GOAL ---------------------------------------------------------------------------------
#echo $(.DEFAULT_GOAL)
#echo VPATH ---------------------------------------------------------------------------------
#echo $(VPATH)
#echo SOURCES -------------------------------------------------------------------------------
#echo $(SOURCES)
# #echo PROJ_ASRCS ----------------------------------------------------------------------------
# #echo $(PROJ_ASRCS)
# #echo AOBJS ---------------------------------------------------------------------------------
# #echo $(AOBJS)
#echo PROJ_CSRCS ----------------------------------------------------------------------------
#echo $(PROJ_CSRCS)
#echo COBJS ---------------------------------------------------------------------------------
#echo $(COBJS)
#echo PROJ_CPPSRCS --------------------------------------------------------------------------
#echo $(PROJ_CPPSRCS)
#echo CPPOBJS -------------------------------------------------------------------------------
#echo $(CPPOBJS)
#echo ---------------------------------------------------------------------------------------
#echo $(CURDIR)
#echo $(OUTPUT_FILE_PATH)
#echo ---------------------------------------------------------------------------------------
$(OUTPUT_FILE_PATH): $(OBJ_PATH) ../rules.mk ../sources.mk $(HAL_PATH)/Makefile $(AOBJS) $(COBJS) $(CPPOBJS)
$(AR) -rv $(OUTPUT_FILE_PATH) $(AOBJS)
$(AR) -rv $(OUTPUT_FILE_PATH) $(COBJS)
$(AR) -rv $(OUTPUT_FILE_PATH) $(CPPOBJS)
$(NM) $(OUTPUT_FILE_PATH) > $(HAL_PATH)/$(OUTPUT_NAME)_symbols.txt
#|---------------------------------------------------------------------------------------|
#| Compile or assemble |
#|---------------------------------------------------------------------------------------|
$(AOBJS): $(OBJ_PATH)/%.o: %.s
#echo +++ Assembling [$(notdir $<)]
#$(AS) $(AFLAGS) $< -o $#
$(AOBJS): $(OBJ_PATH)/%.o: %.S
#echo +++ Assembling [$(notdir $<)]
#$(AS) $(AFLAGS) $< -o $#
$(COBJS): $(OBJ_PATH)/%.o: %.c
#echo +++ Compiling [$(notdir $<)]
#$(CC) $(CFLAGS) -c $< -o $#
$(CPPOBJS): $(OBJ_PATH)/%.o: %.cpp
#echo +++ Compiling [$(notdir $<)]
#$(CC) $(CPPFLAGS) -c $< -o $#
#|---------------------------------------------------------------------------------------|
#| Output folder |
#|---------------------------------------------------------------------------------------|
$(OBJ_PATH):
#echo +++ Creation of [$#]
#-mkdir $(OBJ_PATH)
#|---------------------------------------------------------------------------------------|
#| Cleanup |
#|---------------------------------------------------------------------------------------|
clean:
-rm -f $(OBJ_PATH)/* $(OBJ_PATH)/*.*
-rmdir $(OBJ_PATH)
-rm -f $(OUTPUT_FILE_PATH)
-rm -f $(HAL_PATH)/$(OUTPUT_NAME)_symbols.txt
-rm -rf $(DOC_PATH)
#|---------------------------------------------------------------------------------------|
#| Dependencies |
#|---------------------------------------------------------------------------------------|
$(OBJ_PATH)/%.d: %.s $(OBJ_PATH)
#echo +++ Dependencies of [$(notdir $<)]
#$(CC) $(AFLAGS) -MM -c $< -MT $(basename $#).o -o $#
$(OBJ_PATH)/%.d: %.S $(OBJ_PATH)
#echo +++ Dependencies of [$(notdir $<)]
#$(CC) $(AFLAGS) -MM -c $< -MT $(basename $#).o -o $#
$(OBJ_PATH)/%.d: %.c $(OBJ_PATH)
#echo +++ Dependencies of [$(notdir $<)]
#$(CC) $(CFLAGS) -MM -c $< -MT $(basename $#).o -o $#
$(OBJ_PATH)/%.d: %.cpp $(OBJ_PATH)
#echo +++ Dependencies of [$(notdir $<)]
#$(CC) $(CPPFLAGS) -MM -c $< -MT $(basename $#).o -o $#
#|---------------------------------------------------------------------------------------|
#| Include dependencies, if existing |
#| Little trick to avoid dependencies build for some rules when useless |
#| CAUTION: this won't work as expected with 'make clean all' |
#|---------------------------------------------------------------------------------------|
DEP_EXCLUDE_RULES := clean print_info
ifeq (,$(findstring $(MAKECMDGOALS), $(DEP_EXCLUDE_RULES)))
-include $(AOBJS:%.o=%.d)
-include $(COBJS:%.o=%.d)
-include $(CPPOBJS:%.o=%.d)
endif
#|---------------------------------------------------------------------------------------|
#| Module packaging for Arduino IDE Board Manager |
#|---------------------------------------------------------------------------------------|
packaging: $(OUTPUT_FILE_PATH)
docs: ( cat $(DOXYFILE) ; echo "OUTPUT_DIRECTORY = $(DOC_PATH)" ; echo "INPUT = $(DOC_SOURCES)" ) | doxygen -
# doxygen $(DOXYFILE)
%.d:
Why clang raise me this?
Thank's to #MadScientist
I understand why I have this error, if I print things on my make I got :
$make print_info
DEFAULT_GOAL ---------------------------------------------------------------------------------
all
VPATH ---------------------------------------------------------------------------------
../../hal/stub/ : ../../src/ :
SOURCES -------------------------------------------------------------------------------
../../src/sys_msg.c ../../src/reception.c ../../src/robus.c ../../src/target.c ../../hal/stub/hal.c
PROJ_ASRCS ----------------------------------------------------------------------------
AOBJS ---------------------------------------------------------------------------------
PROJ_CSRCS ----------------------------------------------------------------------------
../../src/sys_msg.c ../../src/reception.c ../../src/robus.c ../../src/target.c ../../hal/stub/hal.c
COBJS ---------------------------------------------------------------------------------
../../hal/stub/obj/sys_msg.o ../../hal/stub/obj/reception.o ../../hal/stub/obj/robus.o ../../hal/stub/obj/target.o ../../hal/stub/obj/hal.o
PROJ_CPPSRCS --------------------------------------------------------------------------
CPPOBJS -------------------------------------------------------------------------------
---------------------------------------------------------------------------------------
/Users/nico/Documents/pollen/Robus/robus/hal/stub
../../hal/stub/libstub.a
---------------------------------------------------------------------------------------
My mistake come for the for this part of my makefile :
PROJ_ASRCS = $(filter %.s,$(foreach file,$(SOURCES),$(file)))
PROJ_ASRCS += $(filter %.S,$(foreach file,$(SOURCES),$(file)))
I don't want to include all .o because I do multiple compilation, I just want to list the needed files.
I have the same compilation process than ExperimentalCore-sam project.
How can I do that?
I can only assume that the variable AOBJS is empty. Following back along where it's created, I can only assume that means that there are no .s or .S files listed in your SOURCES variable.
Why don't you run a single invocation of $(AR) and just use the $^ automatic variable and filter out .o files, rather than three different invocations of the $(AR) command?
$(AR) -rv $(OUTPUT_FILE_PATH) $(filter %.o,$^)
EDIT To be clear I'm suggesting you change your output target rule to this:
$(OUTPUT_FILE_PATH): $(OBJ_PATH) ../rules.mk ../sources.mk $(HAL_PATH)/Makefile $(AOBJS) $(COBJS) $(CPPOBJS)
$(AR) -rv $(OUTPUT_FILE_PATH) $(filter %.o,$^)
$(NM) $(OUTPUT_FILE_PATH) > $(HAL_PATH)/$(OUTPUT_NAME)_symbols.txt
Or, if you wanted to be more idiomatically correct:
$(OUTPUT_FILE_PATH): $(OBJ_PATH) ../rules.mk ../sources.mk $(HAL_PATH)/Makefile $(AOBJS) $(COBJS) $(CPPOBJS)
$(AR) -rv $# $(filter %.o,$^)
$(NM) $# > $(HAL_PATH)/$(OUTPUT_NAME)_symbols.txt
As above I'd definitely remove $(OBJ_PATH) from the prerequisites list.
I understand the difference between GCC and Clang.
With GCC if you run an ar command with no archive members specified, he just skip the step.
Clang raise an error in this case and stop the build.
To patch it I just check if my list is empty or not :
$(OUTPUT_FILE_PATH): $(OBJ_PATH) ../rules.mk ../sources.mk $(HAL_PATH)/Makefile $(AOBJS) $(COBJS) $(CPPOBJS)
ifneq ($(strip $(AOBJS)),)
$(AR) -rv $(OUTPUT_FILE_PATH) $(AOBJS)
endif
ifneq ($(strip $(COBJS)),)
$(AR) -rv $(OUTPUT_FILE_PATH) $(COBJS)
endif
ifneq ($(strip $(CPPOBJS)),)
$(AR) -rv $(OUTPUT_FILE_PATH) $(CPPOBJS)
endif

Make error: make (e=2): The system cannot find the file specified

I am trying to compile a c project on a new windows PC, but when I run make I get:
D:\Eclipse_Workspace\project>make
##make -f makefile -C ./ -e cleanobj
process_begin: CreateProcess(NULL, ##make -f makefile -C ./ -e cleanobj, ...) failed.
make (e=2): The system cannot find the file specified.
make: *** [all] Fejl 2
On my other PC the project is compiling just fine.
Also "Fejl 2" is danish for "Error 2", have no idea why that is in danish when the rest is english.
EDIT:
Here is the makefile (it's rather long)
PROJECT_NAME := ble_app_hrs_s110_pca10028
export OUTPUT_FILENAME
#MAKEFILE_NAME := $(CURDIR)/$(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
MAKEFILE_NAME := $(MAKEFILE_LIST)
MAKEFILE_DIR := $(dir $(MAKEFILE_NAME) )
TEMPLATE_PATH = nrf51_sdk/toolchain/gcc
ifeq ($(OS),Windows_NT)
include $(TEMPLATE_PATH)/Makefile.windows
else
include $(TEMPLATE_PATH)/Makefile.posix
endif
MK := mkdir
RM := rm -rf
#echo suspend
ifeq ("$(VERBOSE)","1")
NO_ECHO :=
else
NO_ECHO := #
endif
# Toolchain commands
CC := "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-gcc"
AS := "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-as"
AR := "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-ar" -r
LD := "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-ld"
NM := "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-nm"
OBJDUMP := "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-objdump"
OBJCOPY := "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-objcopy"
SIZE := "$(GNU_INSTALL_ROOT)/bin/$(GNU_PREFIX)-size"
#function for removing duplicates in a list
remduplicates = $(strip $(if $1,$(firstword $1) $(call remduplicates,$(filter-out $(firstword $1),$1))))
#source common to all targets
C_SOURCE_FILES += \
nrf51_sdk/drivers_nrf/rtc/nrf_drv_rtc.c \
Drivers/NRF51_BLEDriver.c \
(..... a lot more files ....)
#assembly files common to all targets
ASM_SOURCE_FILES = nrf51_sdk/toolchain/gcc/gcc_startup_nrf51.s
#includes common to all targets
INC_PATHS = -I Application/
(..... a lot more include paths.......)
OBJECT_DIRECTORY = _build
LISTING_DIRECTORY =$(OBJECT_DIRECTORY)
OUTPUT_BINARY_DIRECTORY =$(OBJECT_DIRECTORY)
# Sorting removes duplicates
BUILD_DIRECTORIES := $(sort $(OBJECT_DIRECTORY) $(OUTPUT_BINARY_DIRECTORY) $(LISTING_DIRECTORY) )
#flags common to all targets
CFLAGS = -DSOFTDEVICE_PRESENT
CFLAGS += -DNRF51
CFLAGS += -DS110
CFLAGS += -DBOARD_PCA10028
CFLAGS += -DBLE_STACK_SUPPORT_REQD
CFLAGS += -mcpu=cortex-m0
CFLAGS += -mthumb -mabi=aapcs --std=gnu99
CFLAGS += -Wall -O0 -g3
CFLAGS += -mfloat-abi=soft
# keep every function in separate section. This will allow linker to dump unused functions
CFLAGS += -ffunction-sections -fdata-sections -fno-strict-aliasing
#CFLAGS += -flto -fno-builtin
# keep every function in separate section. This will allow linker to dump unused functions
LDFLAGS += -Xlinker -Map=$(LISTING_DIRECTORY)/$(OUTPUT_FILENAME).map
LDFLAGS += -mthumb -mabi=aapcs -L $(TEMPLATE_PATH) -T$(LINKER_SCRIPT)
LDFLAGS += -mcpu=cortex-m0
# let linker to dump unused sections
LDFLAGS += -Wl,--gc-sections
# use newlib in nano version
LDFLAGS += --specs=nano.specs -lc -lnosys
# Assembler flags
ASMFLAGS += -x assembler-with-cpp
ASMFLAGS += -DSOFTDEVICE_PRESENT
ASMFLAGS += -DNRF51
ASMFLAGS += -DS110
ASMFLAGS += -DBOARD_PCA10028
ASMFLAGS += -DBLE_STACK_SUPPORT_REQD
#default target - first one defined
#default: clean nrf51422_xxac_s110
#building all targets
all: #clean
#$(NO_ECHO)$(MAKE) -f $(MAKEFILE_NAME) -C $(MAKEFILE_DIR) -e cleanobj
$(NO_ECHO)$(MAKE) -f $(MAKEFILE_NAME) -C $(MAKEFILE_DIR) -e nrf51422_xxac_s110
#target for printing all targets
help:
#echo following targets are available:
#echo nrf51422_xxac_s110
#echo flash_softdevice
C_SOURCE_FILE_NAMES = $(notdir $(C_SOURCE_FILES))
C_PATHS = $(call remduplicates, $(dir $(C_SOURCE_FILES) ) )
C_OBJECTS = $(addprefix $(OBJECT_DIRECTORY)/, $(C_SOURCE_FILE_NAMES:.c=.o) )
ASM_SOURCE_FILE_NAMES = $(notdir $(ASM_SOURCE_FILES))
ASM_PATHS = $(call remduplicates, $(dir $(ASM_SOURCE_FILES) ))
ASM_OBJECTS = $(addprefix $(OBJECT_DIRECTORY)/, $(ASM_SOURCE_FILE_NAMES:.s=.o) )
vpath %.c $(C_PATHS)
vpath %.s $(ASM_PATHS)
OBJECTS = $(C_OBJECTS) $(ASM_OBJECTS)
nrf51422_xxac_s110: OUTPUT_FILENAME := nrf51422_xxac_s110
nrf51422_xxac_s110: LINKER_SCRIPT=ble_app_hrs_gcc_nrf51.ld
nrf51422_xxac_s110: $(BUILD_DIRECTORIES) $(OBJECTS)
#echo Linking target: $(OUTPUT_FILENAME).out
$(NO_ECHO)$(CC) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
$(NO_ECHO)$(MAKE) -f $(MAKEFILE_NAME) -C $(MAKEFILE_DIR) -e finalize
## Create build directories
$(BUILD_DIRECTORIES):
echo $(MAKEFILE_NAME)
$(MK) $#
# Create objects from C SRC files
$(OBJECT_DIRECTORY)/%.o: %.c
#echo Compiling file: $(notdir $<)
#echo arm-none-eabi-gcc $(CFLAGS) $(INC_PATHS) -c -o $# $<
$(NO_ECHO)$(CC) $(CFLAGS) $(INC_PATHS) -c -o $# $<
# Assemble files
$(OBJECT_DIRECTORY)/%.o: %.s
#echo Compiling file: $(notdir $<)
$(NO_ECHO)$(CC) $(ASMFLAGS) $(INC_PATHS) -c -o $# $<
# Link
$(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out: $(BUILD_DIRECTORIES) $(OBJECTS)
#echo Linking target: $(OUTPUT_FILENAME).out
$(NO_ECHO)$(CC) $(LDFLAGS) $(OBJECTS) $(LIBS) -o $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
## Create binary .bin file from the .out file
$(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).bin: $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
#echo Preparing: $(OUTPUT_FILENAME).bin
$(NO_ECHO)$(OBJCOPY) -O binary $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).bin
## Create binary .hex file from the .out file
$(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).hex: $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
#echo Preparing: $(OUTPUT_FILENAME).hex
$(NO_ECHO)$(OBJCOPY) -O ihex $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).hex
finalize: genbin genhex echosize
genbin:
#echo Preparing: $(OUTPUT_FILENAME).bin
$(NO_ECHO)$(OBJCOPY) -O binary $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).bin
## Create binary .hex file from the .out file
genhex:
#echo Preparing: $(OUTPUT_FILENAME).hex
$(NO_ECHO)$(OBJCOPY) -O ihex $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).hex
echosize:
-#echo ""
$(NO_ECHO)$(SIZE) $(OUTPUT_BINARY_DIRECTORY)/$(OUTPUT_FILENAME).out
-#echo ""
clean:
$(RM) $(BUILD_DIRECTORIES)
cleanobj:
$(RM) $(BUILD_DIRECTORIES)/*.o
flash: $(MAKECMDGOALS)
#echo Flashing: $(OUTPUT_BINARY_DIRECTORY)/$<.hex
nrfjprog --reset --program $(OUTPUT_BINARY_DIRECTORY)/$<.hex)
## Flash softdevice
flash_softdevice:
#echo Flashing: s110_softdevice.hex
nrfjprog --reset --program nrf51_sdk/softdevice/s110/hex/s110_softdevice.hex
In make, every line indented by a TAB character (in a "recipe context") will be passed as a command to the shell. Nothing about such a line will be interpreted by make, except for $.
In particular for you, the # character is not special to make in this context and doesn't introduce a make comment line. So in this rule:
all: #clean
#$(NO_ECHO)$(MAKE) -f $(MAKEFILE_NAME) -C $(MAKEFILE_DIR) -e cleanobj
$(NO_ECHO)$(MAKE) -f $(MAKEFILE_NAME) -C $(MAKEFILE_DIR) -e nrf51422_xxac_s110
the first line is not commented out; it's a command line that begins with a # character and make will ask the system to run that command. Clearly such a command doesn't exist on your system.
If you want to comment out recipe lines in make, you should always put the comment character at the beginning of the line not after the TAB:
all: #clean
# $(NO_ECHO)$(MAKE) -f $(MAKEFILE_NAME) -C $(MAKEFILE_DIR) -e cleanobj
$(NO_ECHO)$(MAKE) -f $(MAKEFILE_NAME) -C $(MAKEFILE_DIR) -e nrf51422_xxac_s110
Just a comment - I struggled for a very long time until I realised RM was set to rm -f and there was no way that works on Windows. Set it to del and all was fine

Compile specific source list and generate dependency

I have below file list:
fd1/foo.c
fd2/foo.c
fd2/bar.c
fd2/bar.h
I want to compile fd1/foo.c and fd2/bar.c. fd2/foo.c is an excluded one. If I use vapth to select all source files, the fd2/foo.c will be compiled. So I want to specify the source list:
SRC_C = fd1/foo.c fd2/bar.c
Since I can't use vpath, so I can only compile SRC_C something like this:
SRC_O = $(patsubst %.c,%.o,$(SRC_C))
SRC_C_LIST = $(notdir $(SRC_C))
OBJ_LIST = $(patsubst %.c,%.o,$(SRC_C_LIST) )
.PHONY: all
all: $(OBJ_LIST)
ar rvs foobar.a $(OBJ_LIST)
$(SRC_O): %.o : %.c
gcc -c $< -I inc/ -MM -MF $(patsubst .o,.d,$(notdir $#)) -o $(notdir $#)
But I also want add dependency rules. But the dependency file seems only contain object target without original folder names:
foo.o : src/foo.c inc/bar.h
How do I to use dependency file in this case? Thanks.
Solutions
Per suggestion from Joachim, I modify my solution as below. Seems work:
SRC_C = fd1/foo.c fd2/bar.c
SRC_O = $(patsubst %.c,%.o,$(SRC_C))
SRC_C_LIST = $(notdir $(SRC_C))
OBJ_LIST = $(patsubst %.c,%.o,$(SRC_C_LIST) )
SRC_D_LIST = $(patsubst %.o,%.d,$(OBJ_LIST))
.PHONY:all
all: $(SRC_O)
ar rvs foobar.a $(OBJ_LIST)
$(SRC_O): %.o : %.c
arm-none-eabi-gcc -c $< -I inc/ -o $# -MM -MT $# -MF $(notdir $(patsubst %.o,%.d,$#))
-include $(SRC_D_LIST)
Using implicit rules, if you do not mind having the .o and .d files along the corresponding .c files:
LIB := foobar.a
SRC := fd1/foo.c fd2/bar.c
OBJ := $(SRC:.c=.o)
DEP := $(OBJ:.o=.d)
CC := arm-none-eabi-gcc
ARFLAGS := rvs
CPPFLAGS := -MMD -MP -I inc
.PHONY: all
.PRECIOUS: $(OBJ)
all: $(LIB)
$(LIB): $(LIB)($(OBJ))
-include $(DEP)

Resources