I'm getting used to using the command line interface (linux etc..) and I come across commands like:

git [command]

.. e.g.

git push

I have a project (developed in C++) and wondered if it is possible to be able create custom made commands, for example:

(assume my project is called 'MyProject'):

MyProject compile

Which would then execute the gcc command to compile?

Note: I know it is possible to create your own commands, like "compile" but I want to create commands that have a 'Reference' and then the 'Command'.


An easier way to do this than shell scripting, once you get the hang of it, is to use make, which you have probably seen reference to before.

A dead simple makefile might look like this:

myproject: myproject.cpp
    g++ myproject.cpp -o myproject

By default make chooses the first target if one is not specified, since there is only one in this case, it will do what you see there. The target is the label before the colon (myproject); after the colon are prerequisite targets. If those targets are not listed, they are assumed to be files. In this case, if myproject.cpp has not changed since the output of the myproject target did (the "myproject" binary), make does nothing. If it has, it recompiles.

The use value of make becomes clearer as things get more complicated:

flags = -Wall -g -O2

myproject: myproject.h myproject.cpp somepart.o
    g++ $(flags) somepart.o myproject.cpp -o myproject

somepart.o: somepart.h somepart.cpp
    g++ $(flags) -c somepart.cpp -o somepart.o

The .h files are prereqs since if you change one, you want the parts requiring it recompiled. So now you can do make somepart.o to just compile the object, or you can do make which will use the first target (myproject) which requires an up-to-date "somepart.o", so if that is not available, it will make all that too.

Doing that with a bash script is not so easy, which is probably why make came to be. Unfortunately, "getting the hang of it" can take a bit of time. l0b0 already linked to this:


Beware first that make is whitespace sensitive especially with regard to using actual tabs and not spaces.


For this you would usually use make compile, but you can easily roll your own by creating a script called MyProject which checks $1 for a preset list of subcommands:

case "$1" in
        echo "$0: Unknown command: $1"
        exit 1


  • Thank you for this. Is there any specific tutorial which can explain the process further? That you know of – Phorce Jan 29 '13 at 14:16

Better keep the standard commands. I.e., make compiles your project, and so on. Easier for collaborators to pick up, much easier to set up, less random stuff to remember (or to set up on the next machine/project). Most projects you'll work on won't be yours, and keeping that machinery as a patch over them will be a lot of hassle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.