[Bash] Sending stdout and stderr to single file

Remember: To send both the stdout and stderr messages from the console to a file do:

$ app > log.file 2>&1

If you want to send everything to a black-hole just replace log.file with /dev/null.

$ app > /dev/null 2>&1

If ever confused about the syntax, do man bash and search for REDIRECTION for more gyan

Update:
It is good idea to use tee, which lets you see the output as well as write it to a file.

$ app 2>&1 | tee file.log

Using [Flex] on OS X

If you are trying to use flex and you get the following error:

Undefined symbols for architecture x86_64:
  "_yywrap", referenced from:
      _yylex in cceJ8DuJ.o
      _input in cceJ8DuJ.o
ld: symbol(s) not found for architecture x86_64

You need to link with libfl.a. Apparently, OSX does not have libfl.a. Use libl.a instead.

$ gcc lex.yy.c -o flexout -ll

Or create a symlink

$ cd /usr/lib
$ ln -s libl.a libfl.a

Sample [Makefile]

Every time anyone asks me how to write a Makefile I need to write a sample Makefile from scratch for them. So, writing one here so that I can just give this link and explain.

# Sample Makefile
CC:= g++
LD:= g++

ROOT_DIR:= ${PWD}
SRC_DIR:= ${ROOT_DIR}/src
TARGET_DIR:= ${ROOT_DIR}/bin
INCLUDES_DIR:= ${ROOT_DIR}/include

SRC_FILES:= ${SRC_DIR}/test.cpp\
            ${SRC_DIR}/foo.cpp

INCLUDES:= -I. -I${INCLUDES_DIR}
OBJ_FILES:= $(patsubst %.cpp,%.o,$(SRC_FILES)

CFLAGS:= -c -Wall ${INCLUDES}
LDFLAGS:=

ifeq (${BUILD}, debug)
CFLAGS += -g
endif

TARGET:= ${TARGET_DIR}/testapp
all: ${TARGET}

${TARGET}: ${OBJ_FILES}
${LD} ${LDFLAGS} $^ -o $@

%.o: %.cpp
${CC} ${CFLAGS} $^ -o $@

clean:
rm -f ${TARGET} ${OBJ_FILES}

.PHONY: clean

[Makefile] Create object list automatically

From the list of source files you can automatically create the list of object files by  either using patsubst or the following handy trick

SRC_FILES:= test.cpp foo.cpp bar.cpp
OBJ_FILES:= $(SRC_FILES:%.cpp=%.o)

Using patsubst the same thing can be achieved.

OBJ_FILES:=$(patsubst %.cpp,%.o,$(SRC_FILES))



[Emacs] Adding gtags support to emacs on OSX

OS X does not have global (gtags) by default. I am a big fan of gtags on emacs and I can't function without it. Homebrew has a Mac port for global which is very easy to install.

brew install global

Once installed, you would need to find out the path to the gtags.el file for emacs. On my Mac it was installed into /usr/local/share/gtags.

Finally,  update the .emacs file with the following:

;; Enable gtags
(setq load-path (cons "/usr/local/share/gtags" load-path))
(autoload 'gtags-mode "gtags" "" t)

Voila, gtags is now working. Now I need to figure out how to enable it for Aquaemacs.

Update: No worries, Aquaemacs is able to read ~/.emacs file as well. The only thing that does not seem to work with Aquaemacs is emacsclient. Well, that is a different fire.

[Emacs] Delete marked selections and remove toolbar

In Emacs you can't delete a marked buffer directly using the [DEL] button on the keyboard. In order to do that enable the M-x delete-selection-mode

Also, if you don't like the emacs toolbar get rid of it using M-x tool-bar-mode

Set the following in your ~/.emacs file

(custom-set-variables
  '(tool-bar-mode nil)
  '(delete-selection-mode t))



[Emacs] Comment region shortcut

C-c C-c Comment a region

M-; Very handy. Comment and un-comment region

Esc-; Same as above