C/C++ Help

Discussion in 'Mac Programming' started by kashmoney2006, Dec 15, 2007.

  1. kashmoney2006 macrumors regular

    Joined:
    Dec 12, 2007
    #1
    So I wanted to learn how to code in C/C++. I have taken a couple classes on programming in java, but nothing in C/C++. The question I have is what should I use to write/compile/run my C code? For java i currently use Eclipse, although I have Xcode. I just dont use Xcode because it is a little too much for the programming that I do right now. So is Xcode a good IDE to get myself started in C/C++, or would you suggest something else? And if anybody has any other suggestions and/or help to share, feel free to let me know about it. Thanks.
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Xcode is probably easiest to get started. Go to File > New Project, then select C++ Tool under Command Line Utility (Xcode 3). Then you can click Build and Go in the toolbar and it should compile and run. From there you can start adding code to the default template.
     
  3. yeroen macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #3
    If you want to stick with using an IDE, then yes Xcode is the one to use.

    TextMate is a good choice if you just want an editor.

    Call me old-school, but unless I'm doing GUI layout, I prefer to work in the terminal with vim/emacs, make, and svn for configuration management.

    The terminal gets in my way quite a bit less than a complex IDE.
     
  4. kashmoney2006 thread starter macrumors regular

    Joined:
    Dec 12, 2007
    #4
    Im not sure how terminal works. Does it work with java code? could i just copy and paste java code into terminal and run it?
     
  5. ledd macrumors newbie

    Joined:
    Dec 26, 2006
    #5
    Almost. If you have java installed you can simply open up a text editor such as "Text Edit" write your java code and save as lets say hello.java.
    Then in terminal go to the location of where the .java file is and type
    javac hello.java

    This will then compile your java code and create the .class files then you can run it by typing
    java hello

    In terms of an editor, eMacs and Vim are great.

    I advise learning c/c++ with terminal and emacs. Xcode is great but I would use it later when you have the hang of c/c++.
     
  6. kashmoney2006 thread starter macrumors regular

    Joined:
    Dec 12, 2007
    #6
    So heres what i got when trying to run a program in terminal:


    Last login: Sun Dec 16 00:01:47 on ttys000
    prakash-suryas-computer:~ prakashsurya$ javac /Users/prakashsurya/Documents/CS 171/WelcomeApp.java
    javac: invalid flag: /Users/prakashsurya/Documents/CS
    Usage: javac <options> <source files>
    where possible options include:
    -g Generate all debugging info
    -g:none Generate no debugging info
    -g:{lines,vars,source} Generate only some debugging info
    -nowarn Generate no warnings
    -verbose Output messages about what the compiler is doing
    -deprecation Output source locations where deprecated APIs are used
    -classpath <path> Specify where to find user class files
    -cp <path> Specify where to find user class files
    -sourcepath <path> Specify where to find input source files
    -bootclasspath <path> Override location of bootstrap class files
    -extdirs <dirs> Override location of installed extensions
    -endorseddirs <dirs> Override location of endorsed standards path
    -d <directory> Specify where to place generated class files
    -encoding <encoding> Specify character encoding used by source files
    -source <release> Provide source compatibility with specified release
    -target <release> Generate class files for specific VM version
    -version Version information
    -help Print a synopsis of standard options
    -X Print a synopsis of nonstandard options
    -J<flag> Pass <flag> directly to the runtime system

    prakash-suryas-computer:~ prakashsurya$ java WelcomeApp
    Exception in thread "main" java.lang.NoClassDefFoundError: WelcomeApp
    prakash-suryas-computer:~ prakashsurya$




    And here is the code for the program:


    /**
    * The WelcomeApp class implents a simple application
    * the prints a welcome message to the standard output
    */
    public class WelcomeApp {

    // The WelcomeApp class consists of a single method,
    // namely yhe application entry method called 'main'.

    public static void main (String[] args) {

    // Invoke a system-provided method to
    // print a String argument to standard output.

    System.out.print("Welcome to ");
    System.out.println("Java Programming!");
    }
    }


    What am I doing wrong??
     
  7. titaniumdecoy macrumors member

    Joined:
    Oct 13, 2005
    #7
    The space in the file path is giving you problems. It would be easiest to cd into the directory first:

    > cd /Users/prakashsurya/Documents/CS\ 171/
    > javac WelcomeApp.java
    > java WelcomeApp
     
  8. kashmoney2006 thread starter macrumors regular

    Joined:
    Dec 12, 2007
    #8
    Ok so it looks as though it compiles now but gives an error when executed????
    Here:


    Last login: Sun Dec 16 00:14:24 on ttys000
    prakash-suryas-computer:~ prakashsurya$ javac /Users/prakashsurya/Documents/CS\ 171/WelcomeApp.java
    prakash-suryas-computer:~ prakashsurya$ java WelcomeApp
    Exception in thread "main" java.lang.NoClassDefFoundError: WelcomeApp
    prakash-suryas-computer:~ prakashsurya$
     
  9. kashmoney2006 thread starter macrumors regular

    Joined:
    Dec 12, 2007
    #9
    That worked thanks. so what does the first line "cd /Users/prakashsurya/Documents/CS\ 171/" do? Why does that work but not what i was trying before?
     
  10. titaniumdecoy macrumors member

    Joined:
    Oct 13, 2005
    #10
    Run "pwd" in the Terminal to see your current working directory. That is where javac and java look for files. If you are not working in the directory containing the files your Java files, you have to prefix each with the relative/absolute file path. Which is to say, you could instead call:

    > javac /Users/prakashsurya/Documents/CS\ 171/WelcomeApp.java
    > java /Users/prakashsurya/Documents/CS\ 171/WelcomeApp

    (Also note the backslashes in the file paths to escape spaces.)
     
  11. kashmoney2006 thread starter macrumors regular

    Joined:
    Dec 12, 2007
    #11
    So the "cd" sets the directory to look from im assuming? And whenever there is a space in a directory you must add a "\" before it?
     
  12. titaniumdecoy macrumors member

    Joined:
    Oct 13, 2005
    #12
    Pretty much. :) Although it's easier to just avoid using spaces when possible.

    Your command prompt actually tells you what directory you are currently in right after the colon; ~ (tilde) represents your home directory (/Users/prakashsurya/ in your case).
     
  13. kashmoney2006 thread starter macrumors regular

    Joined:
    Dec 12, 2007
    #13

    Thanks Alot. I had no idea that I could have been running all these simple java apps right from terminal like that. Thanks for your help. :D
     
  14. kashmoney2006 thread starter macrumors regular

    Joined:
    Dec 12, 2007
    #14
    do these run on Mac OS 10.5??? I was trying to get eMacs but was unsuccessful. A link to a download page might be helpful. thanks
     
  15. ChrisBrightwell macrumors 68020

    ChrisBrightwell

    Joined:
    Apr 5, 2004
    Location:
    Huntsville, AL
    #15
    You can use quote marks for the same effect.

    Code:
    cd /Users/prakashsurya/Documents/"CS 171"/
    You can also type the initial "cd", then drag a folder from Finder into the terminal window. It will automagically type in the rest.
     
  16. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #16
    Just type emacs in the terminal. It is installed by default.

    Personally I prefer to use nano. For that, you guessed it, type nano in terminal.
     
  17. kashmoney2006 thread starter macrumors regular

    Joined:
    Dec 12, 2007
    #17
    seems like terminal can do all sorts of things that i never knew about. Any other things i should know about terminal?
     
  18. yeroen macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #18
  19. kashmoney2006 thread starter macrumors regular

    Joined:
    Dec 12, 2007
    #19
  20. titaniumdecoy macrumors member

    Joined:
    Oct 13, 2005
    #20
    Yep. Although there is no need to get a mac-specific book on Unix.
     
  21. yeroen macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #21
    And I'm assuming Java on a Windows platform. For the most part, the BSD UNIX layer of Mac OS X hasn't changed from 10.4 to 10.5, as far as the user is concerned (and this is Unix 101 after all).

    And any unix book/resource should do, but I quoted that book because it may help explain where UNIX fits in with Mac OS X, and may ease the transition.
     
  22. kashmoney2006 thread starter macrumors regular

    Joined:
    Dec 12, 2007
    #22
    the first was using dr. java and the second that i just completed was using the Eclipse IDE. I used my MacBook Pro for both classes. So if I wanted to begin learning a new language, you would suggest UNIX i take it?
     
  23. yeroen macrumors 6502a

    yeroen

    Joined:
    Mar 8, 2007
    Location:
    Cambridge, MA
    #23
    UNIX isn't a language. It's an operating system (or rather family of operating systems). Linux, BSD, Solaris, Mac OS X, these are all 'flavors' of UNIX.

    If you want to get the most out of Mac OS X, you'll have to learn it's UNIX component.
     
  24. kashmoney2006 thread starter macrumors regular

    Joined:
    Dec 12, 2007
    #24
    i guess ill put that book on my christmas list.:)
     

Share This Page