Mac using terminal for c/c++/java

vong

macrumors 6502a
Original poster
Jan 31, 2010
838
4
recently bought the new 13" macbook pro and i wanted to start doing some editing on this bad boy. i asked my advisor on how to use terminal for the mac. (ie compile run program etc). he said to just use the universities server. it works and all but it has one small problem. if i am working on my program at home, and using my universities server, it is saved to my Z drive(hard drive that all comp sci students get to store their stuff). i find this inconientivent if it is the weekend and i have to go to my university just to get my program to my macbook. so my question is...

1) how do i use terminal to compile my programs and such without logging in to my Universities server.

2) will terminal work for when i take java courses? (read that i should download Eclipse? i tried it but it looked really confusing:confused:)

thanks,
chris:apple:
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
1
Dallas, TX
You don't need to use XCode, but register for a free ADC account and download and install it. This will get you setup with gcc, g++, javac/java etc.

You should be able to program to your heart's content, building and running from terminal.

-Lee
 

foshizzle

macrumors regular
Oct 17, 2007
240
0
Hey -

I took a java class last semester and am taking C right now. Only in the past two weeks have I started actually using XCode for C. Never opened it for Java. It is such a great idea to learn to compile in the terminal and even text-editing in the terminal with vi. You can turn on syntax highlighting for it (google it, and learn vi also) so that it's just like a regular text editor.

Download XCode so that you have all of the compilers on your machine (developer.apple.com).

I never used eclipse for java. I used vi in the terminal or a text editor (macvim, texmate, etc.) to write the code and then compiled in the terminal.

To compile java apps in the terminal, you need to use the cd command to change directories to the folder that your java file is in (not sure of your level of UNIX command-line expertise). Write back if you're having trouble with that. Once you're in the folder, just type 'javac file.java' without the quotes and hit enter. If you look at the finder a new file called file.class will appear. It has compiled. Now just type 'java file' without quotes and hit enter and the app will run.

Make sure you follow the rules of file naming in java.

For C, just use 'gcc file.c' then './a.out' both without quotes.

IF the server is a UNIX server, you can copy files back and forth in the terminal. cd to the folder with the files in it, type 'scp filename.java user@server.com:~/' hit enter type password (it won't show up) and then when you ssh into the server it'll be in the home folder. to transfer an entire folder use 'scp -r folder user@server.com:~/' and it'll be in your home folder.

Good luck - learn it this way and you'll be ahead of the curve.
 

mac2x

macrumors 65816
Sep 19, 2009
1,146
0
You don't need to use XCode, but register for a free ADC account and download and install it. This will get you setup with gcc, g++, javac/java etc.

You should be able to program to your heart's content, building and running from terminal.

-Lee
Exactly what I did. I haven't programmed in a couple months now due to other school responsibilities but I hope to do more over the summer. Xcode tools gets you all the compilers you need in one fell swoop. :)
 

vong

macrumors 6502a
Original poster
Jan 31, 2010
838
4
grr it is not letting me register <_<. if i am a student, what do i put for Company / Organization: and professional information? cannot get past the first part lol
 

chown33

Moderator
Staff member
Aug 9, 2009
8,784
5,166
vertical
grr it is not letting me register <_<. if i am a student, what do i put for Company / Organization: and professional information? cannot get past the first part lol
Put "none" or "student".

I just tried filling out an example form, and it said "We're experiencing technical difficulties please try again later" when I reached the end. I assume that's also what you saw. Taking that at face value, I would guess they are having technical problems, and you should try again later. That may even be Monday, if the support staff can't fix it this weekend.

If you saw some other error, then explain exactly what you saw.

If you absolutely can't wait, then install the version of Xcode that came on your system DVDs. It might be named something like "Xcode" or "Developer Tools", and it might be in an "Optional Installs" folder. It probably isn't the latest version, but you can replace it with the latest when ADC signup is working again.
 

chown33

Moderator
Staff member
Aug 9, 2009
8,784
5,166
vertical

vong

macrumors 6502a
Original poster
Jan 31, 2010
838
4
^ ah man, thank you. for some odd reason, i could not get it to work >_>. i think its because i didnt pay the $99 fee to register as it. but i just used my installation disk as you said and it works. kudos!
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
1
Dallas, TX
It is definitely not because you didn't pay. The free ADC account is all you need to download XCode. If you have a reasonably up-to-date version on your DVD and it got you what you needed, great, but I wouldn't throw my hands up on getting the download working so fast. Chances are you'll eventually want a newer version.

-Lee
 

vong

macrumors 6502a
Original poster
Jan 31, 2010
838
4
okay two quick questions.

1) how come in terminal, when i compile it (gcc lawn lawn.c *no errors come up*) then i try to run it (./lawn.c) it gives me these errors? it can run in Xcodes but not terminal??



(here is the simple code):



2) (trying to get used to terminal and xCodes) also, when i am using xCodes and everything is correct in xcodes, i press "build and run" then click on that terminal looking like icon, then i put in the length that i want, but it does not continue asking me for the other statements?

 

lloyddean

macrumors 65816
May 10, 2009
1,002
16
Des Moines, WA
Code:
#include <stdio.h>
#include <math.h>

int main(void)
{
    double  length_yard;
    double  width_yard;
    
    double  length_house;
    double  width_house;
    
    double  area;
    double  time;

    printf("Enter the length of the yard. ");
    scanf("%lf", &length_yard);
    printf("Enter the width of the yard. ");
    scanf("%lf", &width_yard);
    printf("Enter the length of the house. ");
    scanf("%lf", &length_house);
    printf("Enter the width of the house. ");
    scanf("%lf", &width_house);

    area = (length_yard * length_yard) - (width_yard * width_yard);

    time    = (area * .5);
    printf("The estimated time at 2 square feet per second is %2f secs to now.", time);
    
    return 0;
}
Answer 1

Given what I see in the image you're trying to execute the file that contains the soruce code not the compiled code.

Instead you should be using

./a.out


Answer 2

Are you giving it a number followed by the RETURN key?
 

vong

macrumors 6502a
Original poster
Jan 31, 2010
838
4
Code:
#include <stdio.h>
#include <math.h>

int main(void)
{
    double  length_yard;
    double  width_yard;
    
    double  length_house;
    double  width_house;
    
    double  area;
    double  time;

    printf("Enter the length of the yard. ");
    scanf("%lf", &length_yard);
    printf("Enter the width of the yard. ");
    scanf("%lf", &width_yard);
    printf("Enter the length of the house. ");
    scanf("%lf", &length_house);
    printf("Enter the width of the house. ");
    scanf("%lf", &width_house);

    area = (length_yard * length_yard) - (width_yard * width_yard);

    time    = (area * .5);
    printf("The estimated time at 2 square feet per second is %2f secs to now.", time);
    
    return 0;
}
Answer 1

Given what I see in the image you're trying to execute the file that contains the soruce code not the compiled code.

Instead you should be using

./a.out


Answer 2

Are you giving it a number followed by the RETURN key?
1) i tried a.out but it says -bash?

Code:
chris-vongs-MacBook-Pro:cse150 chris$ /a.out lawn.c
-bash: /a.out: No such file or directory
chris-vongs-MacBook-Pro:cse150 chris$ .a.out lawn.c
-bash: .a.out: command not found
chris-vongs-MacBook-Pro:cse150 chris$ /a.o
edit; okay i got it to work hah, forgot the . infront of ./a.out

2) yeah, i am pressing return but it does not prompt the next question. do you know why this is happening? :'(
 

sammich

macrumors 601
Sep 26, 2006
4,289
218
Sarcasmville.
1) i tried a.out but it says -bash?

Code:
chris-vongs-MacBook-Pro:cse150 chris$ /a.out lawn.c
-bash: /a.out: No such file or directory
chris-vongs-MacBook-Pro:cse150 chris$ .a.out lawn.c
-bash: .a.out: command not found
chris-vongs-MacBook-Pro:cse150 chris$ /a.o
2) yeah, i am pressing return but it does not prompt the next question
a.out is the default name of any executable you compile.

What you're trying to do is run your program with an argument (the words after the program name). Just run you program like this: "./a.out".

To compile your program to something meaningful: "gcc -o desired_prog_name source_file.c"
 

vong

macrumors 6502a
Original poster
Jan 31, 2010
838
4
a.out is the default name of any executable you compile.

What you're trying to do is run your program with an argument (the words after the program name). Just run you program like this: "./a.out".

To compile your program to something meaningful: "gcc -o desired_prog_name source_file.c"

ah okay, thanks thanks. how about the xCODE lol. what am i doing wrong? :(
 

vong

macrumors 6502a
Original poster
Jan 31, 2010
838
4
nevermind, i got it. pressed the "NUM keyboard side" enter. used the main one and it works (using wireless keyboard)

one last question hopefully the last one too. i got this folder named Spring 2010 Quarter and i went to terminal;

cd Documents
cd HomeWork
ls
cd Spring 2010 Quarter
no such file or directory
cd Spring_2010_Quarter
no such file or directory

i dont want to make a new folder on my desktop named work just to access the stuff in spring 2010 quarter. anyone know what i am doing wrong? lol

also also, how do i back a folder? lol like if im at HomeWork how can i go back to Documents sorry for all these questions :p
 

sammich

macrumors 601
Sep 26, 2006
4,289
218
Sarcasmville.
This is but one of the many sites on how to use the unix command line:

http://www.westwind.com/reference/OS-X/commandline/

But to quickly answer your questions: the 'Tab' key can be used to autocomplete directories, so if you want 'Spring 2010...', just type 'cd S' then press the tab key. It will autocomplete if it's the only available option. If there are more than one option, double tab will give you a list, then just type more letters until it's unique.

"cd .." takes you up a level.
 

vong

macrumors 6502a
Original poster
Jan 31, 2010
838
4
ah okay thanks every1. hah all of these small little information is very helpful. and i bookmarked that link too. thanks! :D
 

kny3twalker

macrumors 65816
Oct 25, 2009
1,241
0
okay two quick questions.

1) how come in terminal, when i compile it (gcc lawn lawn.c *no errors come up*) then i try to run it (./lawn.c) it gives me these errors? it can run in Xcodes but not terminal??
try running it like this ./lawn

NOT ./lawn.c

you are trying to run source code, not the executable there.

I always compile in the terminal, but write c programs in XCode when on my Apple.

when compiling, try

gcc -o theNameOfYourSourceCode.c theNameYouWantForTheExecutable

This way you do not have to use a.out


When writing Java programs, I use Netbeans for OSX.

Eclipse is another IDE you may want to try.

EDIT:

Also you do not need to write VOID in "int main()"

meaning

int main()
{
}

rather than

int main(void)
{
}
 

sammich

macrumors 601
Sep 26, 2006
4,289
218
Sarcasmville.
^ RTFT. Why don't people read anymore?

But at least you edited to add something.

The OP: a 'void' is nothing, so in far more cases than not, you can omit the keyword 'void' where they have in textbooks/tutorials.
 

kny3twalker

macrumors 65816
Oct 25, 2009
1,241
0
^ RTFT. Why don't people read anymore?

But at least you edited to add something.

The OP: a 'void' is nothing, so in far more cases than not, you can omit the keyword 'void' where they have in textbooks/tutorials.
I did read it.

no one explained why it did not work when the op tried to run ./lawn.c
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.