For each source file (C and C++ in this case) in a directory I need a generic rule that will compile the source file into a binary whose name is the basename of the sourcefile with the architecture appended.
My makefile is:
CC = gcc
CPP = g++
CFLAGS = -std=c89 -std=c99 -fpermissive
CPPFLAGS = -std=c++17 -fpermissive
BINDIR = ./bin
LIBS = -lm
ARCH = $(shell uname -m)
# Populate two variables with the source files
SRCS=$(wildcard *.c)
CPP_SRCS=$(wildcard *.cpp)
# Create a list of the target files for the .C sources
OBJS := $(patsubst %.c, $(BINDIR)/%-$(ARCH), $(SRCS))
# Add a list of the target files for the .cpp sources
OBJS += $(patsubst %.cpp, $(BINDIR)/%-$(ARCH), $(CPP_SRCS))
all: $(OBJS)
# Rule to compile C++ sources into the appropriately named target
$(BINDIR)/% : %.cpp Makefile
$(CPP) -o $@ $< $(CPPFLAGS)
# Rule to compile C sources into the appropriately named target
$(BINDIR)/% : %.c Makefile
$(CC) -o $@ $< $(CFLAGS) $(LIBS)```
This results in an error: `make: *** No rule to make target `bin/file1-arm64', needed by `all'. Stop.`
New contributor
David Kovar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.