My Makefile Snippet

I use SnippetsEmu for my snippets in Vim. Offered for download below are my snippets for makefiles for C and C++.  The C and C++ makefiles will expand on makec and makecc, respectively. Place the file in ~/.vim/after/ftplugin/.  Make sure it is named make_snippets.vim and you have SnippetsEmu installed.

make_snippets.vim

let st = g:snip_start_tag
let et = g:snip_end_tag
let cd = g:snip_elem_delim
exec “Snippet makecc # Specify the main target\r<BS><BS>TARGET = “.st.”bin”.et.st.et.”\r# Default build type\r<BS><BS>TYPE = debug\r# Which directories contain source files\r<BS><BS>DIRS = .\r# Which libraries are linked\r<BS><BS>LIBS =\r# Dynamic libraries\r<BS><BS>DLIBS =\r\r# The next blocks change some variables depending on the build type\r<BS><BS>ifeq ($(TYPE),debug)\r<BS>LDPARAM =\rCCPARAM = -Wall -g3\rMACROS =\rendif\r\rifeq ($(TYPE),profile)\r<BS>LDPARAM = -pg /lib/libc.so.5\rCCPARAM = -Wall -pg\rMACROS = NDEBUG\rendif\r\rifeq ($(TYPE), release)\r<BS>LDPARAM = -s\rCCPARAM = -Wall -O2\rMACROS = NDEBUG\rendif\r\r# Add directories to the include and library paths\r<BS><BS>INCPATH = . $(HOME)/Development/include\rLIBPATH =\r\r# Which files to add to backups, apart from the source code\r<BS><BS>EXTRA_FILES = Makefile\r# The compiler\r<BS><BS>CXX = g++\r\r# Where to store object and dependancy files.\r<BS><BS>STORE = .make-$(TYPE)\r# Makes a list of the source (.cc) files.\r<BS><BS>SOURCE := $(foreach DIR,$(DIRS),$(wildcard $(DIR)/*.cc))\r# List of header files.\r<BS><BS>HEADERS := $(foreach DIR,$(DIRS),$(wildcard $(DIR)/*.hh))\r# Makes a list of the object files that will have to be created.\r<BS><BS>OBJECTS := $(addprefix $(STORE)/, $(SOURCE:.cc=.o))\r# Same for the .d (dependancy) files.\r<BS><BS>DFILES := $(addprefix $(STORE)/,$(SOURCE:.cc=.d))\r\r# Specify phony rules. These are rules that are not real files.\r<BS><BS>.PHONY: clean backup dirs\r\r# Main target. The @ in front of a command prevents make from displaying\r<BS><BS># it to the standard output.\r<BS><BS>$(TARGET): dirs $(OBJECTS)\r@echo \” LD $(TARGET)\”\r@$(CXX) -o $(TARGET) $(OBJECTS) $(LDPARAM) $(foreach LIBRARY, \\\r$(LIBS),-l$(LIBRARY)) $(foreach LIB,$(LIBPATH),-L$(LIB))\r\r# Rule for creating object file and .d file, the sed magic is to add\r<BS><BS># the object path at the start of the file because the files gcc\r<BS><BS># outputs assume it will be in the same dir as the source file.\r<BS><BS>$(STORE)/%.o: %.cc\r@echo \” CXX $?\”\r@$(CXX) -Wp,-MMD,$(STORE)/$*.dd $(CCPARAM) $(foreach INC,$(INCPATH),-I$(INC)) \\\r$(foreach MACRO,$(MACROS),-D$(MACRO)) -c make_snippets.vimXXlt; -o $@\r@sed -e ’1s/^\\(.*\\)$/$(subst /,\\/,$(dir $@))\\1/’ $(STORE)/$*.dd > $(STORE)/$*.d\r@rm -f $(STORE)/$*.dd\r\r# Empty rule to prevent problems when a header is deleted.\r<BS><BS>%.hh: ;\r\r# Cleans up the objects, .d files and executables.\r<BS><BS>clean:\r@echo Cleaning up\r@-rm -f $(foreach DIR,$(DIRS),$(STORE)/$(DIR)/*.d $(STORE)/$(DIR)/*.o)\r@-rm -f $(TARGET)\r\r# Backup the source files.\r<BS><BS>backup:\r@-if [ ! -e .backup ]; then mkdir .backup; fi;\r@zip .backup/backup_`date +%d-%m-%y_%H.%M`.zip $(SOURCE) $(HEADERS) $(EXTRA_FILES)\r\r# Create necessary directories\r<BS><BS>dirs:\r@-if [ ! -e $(STORE) ]; then mkdir $(STORE); fi;\r@-$(foreach DIR,$(DIRS), if [ ! -e $(STORE)/$(DIR) ]; \\\rthen mkdir $(STORE)/$(DIR); fi; )\r\r# Includes the .d files so it knows the exact dependencies for every\r<BS><BS># source.\r<BS><BS>-include $(DFILES)”
This entry was posted in Programming. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>