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
muru
  • 207,228
Casey
  • 371

4 Answers4

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
muru
  • 207,228
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
muru
  • 207,228
Casey
  • 371
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.

muru
  • 207,228
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
ubfan1
  • 19,049