using terminal for c/c++/java

Discussion in 'Mac Programming' started by vong, Apr 22, 2010.

  1. vong macrumors 6502a

    vong

    Joined:
    Jan 31, 2010
    #1
    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:
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    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
     
  3. foshizzle macrumors regular

    Joined:
    Oct 17, 2007
    #3
    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.
     
  4. mac2x macrumors 65816

    Joined:
    Sep 19, 2009
    #4
    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. :)
     
  5. vong thread starter macrumors 6502a

    vong

    Joined:
    Jan 31, 2010
    #5
    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
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    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.
     
  7. vong thread starter macrumors 6502a

    vong

    Joined:
    Jan 31, 2010
    #7
    okay i tried to register the first day you guys told me to register at:

    http://developer.apple.com/programs/register/

    i put in my info (using my apple ID) i press next it says its down still. anyone know if i am doing something wrong, is this whole thing down <_<
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
  9. vong thread starter macrumors 6502a

    vong

    Joined:
    Jan 31, 2010
    #9
  10. mfram macrumors 65816

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #10
    XCode is also on the Snow Leopard install DVD. You don't have to download it if you don't want to.
     
  11. vong thread starter macrumors 6502a

    vong

    Joined:
    Jan 31, 2010
    #11
    ^ 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!
     
  12. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #12
    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
     
  13. vong thread starter macrumors 6502a

    vong

    Joined:
    Jan 31, 2010
    #13
    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??

    [​IMG]

    (here is the simple code):

    [​IMG]

    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?

    [​IMG]
     
  14. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #14
    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?
     
  15. vong thread starter macrumors 6502a

    vong

    Joined:
    Jan 31, 2010
    #15
    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? :'(
     
  16. sammich macrumors 601

    sammich

    Joined:
    Sep 26, 2006
    Location:
    Sarcasmville.
    #16
    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"
     
  17. vong thread starter macrumors 6502a

    vong

    Joined:
    Jan 31, 2010
    #17

    ah okay, thanks thanks. how about the xCODE lol. what am i doing wrong? :(
     
  18. vong thread starter macrumors 6502a

    vong

    Joined:
    Jan 31, 2010
    #18
    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
     
  19. notjustjay macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #19
    If there's a space in the name, use quotes.

    cd "Spring 2010 Quarter"

    To back up, use ".." (two periods)

    cd ..
     
  20. sammich macrumors 601

    sammich

    Joined:
    Sep 26, 2006
    Location:
    Sarcasmville.
    #20
    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.
     
  21. vong thread starter macrumors 6502a

    vong

    Joined:
    Jan 31, 2010
    #21
    ah okay thanks every1. hah all of these small little information is very helpful. and i bookmarked that link too. thanks! :D
     
  22. kny3twalker macrumors 65816

    kny3twalker

    Joined:
    Oct 25, 2009
    #22
    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)
    {
    }
     
  23. sammich macrumors 601

    sammich

    Joined:
    Sep 26, 2006
    Location:
    Sarcasmville.
    #23
    ^ 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.
     
  24. kny3twalker macrumors 65816

    kny3twalker

    Joined:
    Oct 25, 2009
    #24
    I did read it.

    no one explained why it did not work when the op tried to run ./lawn.c
     
  25. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #25
    Actually I believe I did answer.
     

Share This Page