8

So here I have the basic "hello world" script for Objective-C, I have Ubuntu on my laptop and want to use Ubuntu to start compiling basic ideas, extra classes, or anything worth coding for any of my current projects on the go, so that way I can code a little bit wherever I am and not always have to be on my iMac.

helloworld.m

Now my only question is how do I build and run this code in terminal? Obviously it's not that important since I'm coding for iOS and OSX so the only code that will display is anything that doesn't call on the SDK's or Cocoa scripts, so basically it should only display basic text commands such as "NSLog" or whatever, you get the just of what I'm trying to do, which is what I want. That way I can check for small errors at the very least.

I've looked around and I've found several helps for default shell scripts and such that call on the gcc directory or something of the sort from my understanding. I have created my own folder for Objective-C projects located in my documents and I had no idea if that is effecting my ability to run the file using the terminal commands that I've tried or not, but in case it is I store the projects in:

Home>Documents>Objective-C>helloworld.m

Also when running the command line:

gcc -o helloworld.m

or this

gcc -I/home/caseyerdmann/Documents/Objective-C/helloworld.m

or any version of that command line I receive this error:

gcc: fatal error: no input files
compilation terminated.

I believe I have all the compiler files installed but I am not opposed to a full tutorial, whatever gets this running. Now to clarify and gather a visual aid for you guys to try and help me better my text file is located in my documents:

filedirectory

And you can visually see the directory it is placed in. Now when I run any of the commands above that I have tried in terminal it yields this result:

terminal

It doesn't matter which command I use it is always that. I hope this helps a little bit more to provide an answer for my situation. I am not opposed to a full tutorial and having to save my file elsewhere or make any other changes by any means, in fact it'd be appreciated! I just would love to get this running.

I did some research and noticed the answers on this already are providing me with help to run the hello.m script that comes inside the GNUstep libraries, this is not what I am trying to do. However because I'm having trouble getting the default script to run I think I may have another problem as well, I'm going to continue researching to see if I can get that script to run, in the mean time any help is appreciated!

UPDATE: I have tried a new command and yielded a new result, though it was also unsuccessful in compiling I think it may help answer others to answer this. This is what I tried:

gcc -o helloworld.m -lobjc

and this was the result:

/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status
8
  • Have you installed a Objective C compiler and compiled the code?
    – edwin
    Aug 3, 2013 at 21:42
  • I have GNU Step installed but I'm not sure how to use it to compile the code, I only have it written in gedit and saved in objective-c format as helloworld.m in the directory listed.
    – Casey
    Aug 3, 2013 at 22:10
  • The -o option is to specify the name of the executable to be produced (the default is a.out). You should write gcc helloworld.m -o HelloWorld -lobjc where HelloWorld is the name of the executable.
    – edwin
    Aug 6, 2013 at 21:47
  • Oh okay, I understand now, however it still did not work. Maybe it's an error with GNUstep? I read something about having to make a shell script because gcc can't recognize where the file is? If so how to I do this? Or is that incorrect?
    – Casey
    Aug 6, 2013 at 21:59
  • Please provide the output the command I put in the previous comment, also make sure you are on the right directory and your file is called helloworld.m
    – edwin
    Aug 6, 2013 at 22:01

4 Answers 4

7

In Ubuntu 14.04 LTS, go ahead and install GNU objective-c compiler:

sudo apt-get install gobjc  

Verify the version:

gcc --version

must say something like:

gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2
....

Install GNUStep development libraries (equivalent to Cocoa on macosx):

sudo apt-get install gnustep-devel

It should work and compile with:

gcc HelloWorld.m `gnustep-config --objc-flags` `gnustep-config --base-libs` -o HelloWorld

You should run with:

./HelloWorld
1
  • Thanks. This procedure worked for me.
    – Puspam
    Feb 28, 2020 at 7:02
4

I did a little research and found a solution that works for now. I'll probably post a new question to see if there is a way to shorten this. But this successfully compiles my code. Thank you to everyone who provided some help, I definitely learned some new things.

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 \
    -D_NATIVE_OBJC_EXCEPTIONS
1
  • Thanks for the command. You still need -lobjc
    – Viet
    Nov 1, 2016 at 8:40
3

You're missing the name for executable file. So, the command should be like:

gcc -o hello helloworld.m

After compilation, type

./hello

-o hello specifies the name of the output file you wish to create once the source is compiled. (In this case the output file name is hello.) This will create an executable file (named hello) which you, then, can run directly from the terminal. If you don't use -o option while compiling, the name of the executable file will be a.out by default. Now to execute that program, you need to type

./hello

The above commands assume you are already in the location of the source files, but both the source file and target output file may also be specified as a directory. For example:

gcc -o ~/Projects/hello ~/Desktop/hello.c

will compile a C source file located on your desktop and place the executable binary in a Projects folder in your home directory. To run this executable, type;

./Projects/hello

Or you can also first go to the directory where the source file is located and run above commands. You do not need specify pathnames if you're in the same directory where your source file is.

I'm wondering why your file extension is .m instead of .c.

2
  • That makes sense, however it did not work. I don't quite understand what that is calling for though, I understand GCC is my compiler/program I'm using to run my code in termianl, but what directory is "-o" calling on and why is "hello" needed before helloworld.m when it is not anywhere in my file or related to it. And while the executable file name being needed makes sense, I don't actually have one It's just a text file typed in gedit saved in objective-c format. I'll try and update my question to be more descriptive of my problem to see if that yields a better result.
    – Casey
    Aug 6, 2013 at 20:36
  • man gcc will output the manual pages for gcc. I suggest you do some reading. "-o foobar" simply says output a file named foobar, instead of the default, which is a.out.
    – ubfan1
    Aug 6, 2013 at 21:41
0
gcc -o hello hello.m

should produce the objective c executable if the compiler is installed. If not, install it:

sudo apt-get install gobjc
3
  • The result of running the first command, which i tweaked to use the correct file name of my project, ended in this result: gcc: fatal error: no input files compilation terminated. I also checked to see if gobjc was installed by running the second command, and it is in fact installed. Am I missing anything else?
    – Casey
    Aug 5, 2013 at 1:45
  • Sounds like you used the wrong file name. Did you get the case correct and don't have any nasty things like spaces in the name?
    – ubfan1
    Aug 5, 2013 at 4:55
  • well I used your exact command at first but then I realized that my file name isn't hello.m so I entered this exactly: "gcc -o helloworld.m" However, my file is located in my documents, I feel like there’s a line I'm missing to help locate the file in my documents I just don't know what the command is.
    – Casey
    Aug 6, 2013 at 19:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .