Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Sorry in advanced if this question would be better suited on SO.

Basically, 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'.

Hope someone can help me

share|improve this question

3 Answers 3

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:

http://www.gnu.org/software/make/manual/make.html

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

share|improve this answer

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
    compile)
        my_compile_command(s)
        shift
        ;;
    *)
        echo "$0: Unknown command: $1"
        exit 1
        ;;
esac

References:

share|improve this answer
    
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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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