-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
66 lines (53 loc) · 1.65 KB
/
Makefile
File metadata and controls
66 lines (53 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# Compiler settings
CC = gcc
CXX = g++
AR = ar
CFLAGS = -Wall -Iinclude -static -g
CXXFLAGS = -Wall -Iinclude -static -g
LDFLAGS = -lenet
# Directories
SRC_DIR = src
EXAMPLES_DIR = examples
BUILD_DIR = build
OBJ_DIR = $(BUILD_DIR)/obj
LIB_NAME = libsmpn.a
INCLUDE_DIR = include
# Installation directories (can be overridden)
PREFIX ?= /usr/local
INCLUDE_INSTALL_DIR ?= $(PREFIX)/include
LIB_INSTALL_DIR ?= $(PREFIX)/lib
# Find all source files
C_SRCS = $(wildcard $(SRC_DIR)/*.c)
OBJS = $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(C_SRCS))
CPP_SRCS = $(wildcard $(EXAMPLES_DIR)/*.cpp)
CPP_OBJS = $(patsubst $(EXAMPLES_DIR)/%.cpp,$(BUILD_DIR)/%.o,$(CPP_SRCS))
EXE_FILES = $(patsubst $(EXAMPLES_DIR)/%.cpp,$(EXAMPLES_DIR)/%,$(CPP_SRCS))
# Default target
all: $(BUILD_DIR)/$(LIB_NAME) $(EXE_FILES)
# Create build directories
$(OBJ_DIR):
mkdir -p $(OBJ_DIR)
$(BUILD_DIR):
mkdir -p $(BUILD_DIR)
# Compile C files into object files
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR)
$(CC) $(CFLAGS) -c $< -o $@
# Build static library
$(BUILD_DIR)/$(LIB_NAME): $(OBJS) | $(BUILD_DIR)
$(AR) rcs $@ $(OBJS)
# Compile C++ examples
$(EXAMPLES_DIR)/%: $(EXAMPLES_DIR)/%.cpp $(BUILD_DIR)/$(LIB_NAME)
$(CXX) $(CXXFLAGS) $< -L$(BUILD_DIR) -lmpn $(LDFLAGS) -o $@
# Clean
.PHONY: clean
clean:
rm -rf $(BUILD_DIR)/*.a $(OBJ_DIR)/*.o $(EXAMPLES_DIR)/*
# Install target
.PHONY: install
install: all
mkdir -p $(LIB_INSTALL_DIR)
mkdir -p $(INCLUDE_INSTALL_DIR)
cp $(BUILD_DIR)/$(LIB_NAME) $(LIB_INSTALL_DIR)/
cp -r $(INCLUDE_DIR)/* $(INCLUDE_INSTALL_DIR)/
# Allow custom include/lib directories for compilation
# Usage example: make CFLAGS="-Ipath\to\headers" LDFLAGS="-Lpath\to\libs -lmylib"