Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Now I don't know if that is exactly what it is called, so please forgive my bad terminology and correct me if I'm wrong, but I'm looking for a way to shorten this bit of code:

gcc -o hello hello.m \

-I `gnustep-config --variable=GNUSTEP_SYSTEM_HEADERS` \

-L `gnustep-config --variable=GNUSTEP_SYSTEM_LIBRARIES` \
-lgnustep-base -fconstant-string-class=NSConstantString \


Now to clarify, this code allows me to execute or build/run an objective-c program that I build. It took me awhile to actually find it as I was having errors with compiling objective-c in Ubuntu previously, but this code works. Now I don't mind typing it in every time if I absolutely have too, but I feel like there is an easier way.

So is there anyway I can shove this into a text file and save it as gcc execute or something and make it where I only have to type one line like:

./gcc execute

to run it and then just ./-name of obj-c file

and it would run my program?

Now I'm not looking for that form of method exactly, it's just an example, is there anyway to do anything like that?

It would make things much more convenient, any help is always appreciated!

share|improve this question

1 Answer 1

up vote 3 down vote accepted

To answer part of what was asked:

First of all I used 'myDemoScript' rather than 'gcc' because it gets confusing when you later invoke the gnu c compiler (GCC) from a script with the same name.

1) First a basic way of doing it without checking the file name:

vim myDemoScript    
i (insert mode on)
#!/usr/bin/env bash
gcc -o hello hello.m  etc etc etc
ESC :wq

chmod +x myDemoScript (make the script executable).
Afterwards you can just use ./myscript.

2) If you use bash (or similar shells) you can use $0, $1.

$0 will contain the name of the program (e.g. in my case "myDemoScript".)
$1 will contain the first argument.

So If you invoke "./myDemoScript hello" then inside the script $1 will contain "hello"

#$/usr/bin/env bash
echo started the script $0 with $# arguments
echo going to work on the file $1
echo compiling $1.c to produce the output file $1
gcc -o $1 $1.c

(For more details google on "bash getopts")

3) To answer how I would do it:

For compilation jobs: use make.

Make is overkill for compiling a single c file to an executable, but it is a good tool to learn and you can use it for so much more. It is also very useful should you ever get a project with multiple files. Especially in the case of many files or files which take long to compile.

vim Makefile
i (insert mode on)

<TAB> gcc -o hello hello.m -I `gnustep-config \
       --variable=GNUSTEP_SYSTEM_HEADERS` \
      -L `gnustep-config --variable=GNUSTEP_SYSTEM_LIBRARIES` \
      -lgnustep-base -fconstant-string-class=NSConstantString \

<TAB> gcc -g -o hello hello.m -I `gnustep-config  \
      --variable=GNUSTEP_SYSTEM_HEADERS` \
      -L `gnustep-config --variable=GNUSTEP_SYSTEM_LIBRARIES` \
      -lgnustep-base -fconstant-string-class=NSConstantString \

<TAB> rm hello

ESC :wq (write and quit)

With this file in the same folder you can just issue `make hello'.
Make will then search for the label hello in the Makefile.
It will check if it needs to build any dependencies first (none are used in or example), and then execute your compile command.

I added the label *debug_hello* to demonstrate other options: E.g. building a debug version of the same program by adding the -g flag to gcc.

The clean is another nice example. Partially because it shows non-GCC functionality. Partially because it is very nice to have a way to clean things when you project grows to include dozens of files.

Make can do much much more than what I just showed, but that is getting rather off-topic. I recommend some googling if you are curious.

share|improve this answer
Now I've started writing it and I'm in the section where I input my data, and I have put it in, how do I 'save' and exit to run it? This is the data I have inserted into the input area: #!/usr/bin/env bash -o hello hello.m \ -I gnustep-config --variable=GNUSTEP_SYSTEM_HEADERS \ -L gnustep-config --variable=GNUSTEP_SYSTEM_LIBRARIES \ -lgnustep-base -fconstant-string-class=NSConstantString \ -D_NATIVE_OBJC_EXCEPTIONS ESC wq –  Casey Aug 8 '13 at 19:12
The exit vim, press the escape key to leave insert mode. Then type :wq (colon to enter command mode, w to write, q to quit). I noticed I left the : out of the answer. I will fix that. (Alternatively, any editor should do, I just used vim since it is my default editor. Vi(m) is very powerful, but it also has a steep learning curve. –  Hennes Aug 8 '13 at 19:25
Thanks! I'm still fooling around with it some, but I'm pretty sure you answered my question! –  Casey Aug 8 '13 at 19:32

Your Answer


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.