Linux For Cynics One user's experiences with Linux

9Sep/130

Pull a Git Branch from Remote

In order to check out a remote git branch, first do a git pull:

git pull 

Then

git checkout -b [new_branch] > origin/[new_branch]

So for example, if your branch name is "newfeature", you'd do:

git pull
git checkout -b newfeature origin/newfeature
Tagged as: No Comments
15May/130

Fizz Buzz in Vim

The "Fizz Buzz" problem is a beginner's programming exercise. It is stated as:

Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz"

Fairly simple, you can even get vim to do it:
vim_fizz_buzz

Tagged as: , No Comments
29Mar/130

Using pkg-config to Find Library Names

If you are trying to use a library in your program, of course you need to link with it. How do you know what it's called? Use pkg-config! From the pkg-config man page

The pkg-config program is used to retrieve information about installed libraries in the system. It is typically used to compile and link against one or more libraries. Here is a typical usage scenario in a Makefile:

program: program.c
cc program.c $(pkg-config --cflags --libs gnomeui)

Here are some common usages:

List all installed libraries:

pkg-config --list-all

List what compiler flags you need for somelib:

pkg-config --cflags somelib

List what linker flags you need for somelib:

pkg-config --libs somelib

Then there's also --static if you're compiling statically.

pkg-config outputs in a format that is ready for gcc, so you can use backticks to run it and get the output when invoking gcc on the command line:

gcc program.c `pkg-config --cflags --libs zlib`

Or if you use a makefile you can capture it in variables:

LIBS := $(shell pkg-config --libs zlib)
CFLAGS := $(shell pkg-config --clfags zlib)