Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kashmoney2006

macrumors regular
Original poster
Dec 12, 2007
232
0
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.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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.
 

yeroen

macrumors 6502a
Mar 8, 2007
944
2
Cambridge, MA
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.
 

ledd

macrumors newbie
Dec 26, 2006
23
0
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++.
 

kashmoney2006

macrumors regular
Original poster
Dec 12, 2007
232
0
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??
 

titaniumdecoy

macrumors member
Oct 13, 2005
86
0
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
 

kashmoney2006

macrumors regular
Original poster
Dec 12, 2007
232
0
You need to escape the space in the file path:

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

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$
 

kashmoney2006

macrumors regular
Original poster
Dec 12, 2007
232
0
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

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?
 

titaniumdecoy

macrumors member
Oct 13, 2005
86
0
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.)
 

kashmoney2006

macrumors regular
Original poster
Dec 12, 2007
232
0
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.)

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?
 

titaniumdecoy

macrumors member
Oct 13, 2005
86
0
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?
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).
 

kashmoney2006

macrumors regular
Original poster
Dec 12, 2007
232
0
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).


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
 

kashmoney2006

macrumors regular
Original poster
Dec 12, 2007
232
0
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++.

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
 

ChrisBrightwell

macrumors 68020
Apr 5, 2004
2,294
0
Huntsville, AL
> 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.)

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.
 

kashmoney2006

macrumors regular
Original poster
Dec 12, 2007
232
0
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.

seems like terminal can do all sorts of things that i never knew about. Any other things i should know about terminal?
 

yeroen

macrumors 6502a
Mar 8, 2007
944
2
Cambridge, MA
Yeah all i would know is some java. Since the two classes ive taken so far were in java. So it says the book is for Tiger. If im running 10.5 Leopard it would still apply right?

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.
 

kashmoney2006

macrumors regular
Original poster
Dec 12, 2007
232
0
And I'm assuming Java on a Windows platform.

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?
 

yeroen

macrumors 6502a
Mar 8, 2007
944
2
Cambridge, MA
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.