I am a novice user of makefile, I have created rules(run1, run2 etc) to execute c and c++ objects separately(separate compilation is successful c/c++, also need to execute them separately) but it seems not working as expected, could anyone tell were it went wrong and how to correct it
TARGET_EXEC ?=
BUILD_DIR ?= .
SRC_DIRS ?= .
SRCS := $(shell find $(SRC_DIRS) -name *.cpp -or -name *.c)
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
INC_DIRS := $(shell find $(SRC_DIRS) -type d)
INC_FLAGS := $(addprefix -I,$(INC_DIRS))
CC = gcc
CXX = g++
CFLAGS = -g -Wall
CPPFLAGS = -g -lws2_32 -Wno-write-strings
.PHONY: all run1 run2 run3 clean
all: $(BUILD_DIR)/$(TARGET_EXEC)
$(BUILD_DIR)/$(TARGET_EXEC): $(OBJS)
@echo "Build completed"
# c source
$(BUILD_DIR)/%.c.o: %.c
@echo "Compiling C sources"
@$(CC) $(CFLAGS) $< -o $(basename $(<F))
@echo "Compiled successfully"
# c++ source
$(BUILD_DIR)/%.cpp.o: %.cpp
@echo "Compiling C++ sources"
@$(CXX) -o $(basename $(<F)) $(CXXFLAGS) $< $(CPPFLAGS)
@echo "Compiled successfully"
clean:
@echo "Cleaning"
@$(RM) -r *.exe
@echo "Cleaned up"
run1: $(OBJS)
$(OBJS)=$(SRCS)
$(SRC_DIRS)/%.c.o: %.c
@echo "from run1"
./$(basename $(<F)) ${ARGS}
run2: $(OBJS)
$(OBJS)=$(SRCS)
$(SRC_DIRS)/%.cpp.o: %.cpp
@echo "from run2"
./$(basename $(<F)) ${ARGS}
command used: make run1
output:
from run1
-> execution
from run2
-> execution
Expected output
from run1
--> execution of exe created
Moreover I am trying to pass arguments which I have referred from link to pass on to make run. eg:
make ARG="-p TCP -n 127.0.0.1 -e 10006 -l 1" run2
also let me know if this can be done without any issues.