Skip to content

Instantly share code, notes, and snippets.

@thomasjpfan
Created January 4, 2014 17:39
Show Gist options
  • Save thomasjpfan/8257897 to your computer and use it in GitHub Desktop.
Save thomasjpfan/8257897 to your computer and use it in GitHub Desktop.
Makefile for cpp
CXX = g++
CXXFLAGS=-g -O2 -Wall -Wconversion
SOURCES=$(wildcard src/**/*.cpp src/*.cpp)
OBJECTS=$(patsubst src/%.cpp,bin/%.o,$(SOURCES))
EXECUT=build/test.exe
MAIN=src/main.cpp
LIB_SOURCES=$(filter-out $(MAIN), $(SOURCES))
LIB_OBJ=$(patsubst src/%.cpp,bin/%.o,$(LIB_SOURCES))
LIB_TARGET=build/libYOURLIBRARY.a
SO_TARGET=$(patsubst %.a,%.so,$(LIB_TARGET))
exec: build $(OBJECTS)
$(CXX) $(CXXFLAGS) -o $(EXECUT) $(OBJECTS)
@./$(EXECUT)
all: build $(OBJECTS) lib dyn exec
libexec: build $(OBJECTS) lib
$(CXX) $(CXXFLAGS) -o $(EXECUT) $(MAIN) $(LIB_TARGET)
test:
gdb $(EXECUT)
bin/%.o: src/%.cpp
$(CXX) -c $(CXXFLAGS) -o $@ $<
run:
@./$(EXECUT)
lib: build $(LIB_OBJ)
ar rcs $(LIB_TARGET) $(LIB_OBJ)
ranlib $(LIB_TARGET)
dyn: build $(LIB_OBJ)
$(CXX) -shared -o $(SO_TARGET) $(LIB_OBJ)
build:
@mkdir -p build
@mkdir -p bin
clean:
rm -rf build bin $(OBJECTS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment