PDA

View Full Version : Help For A Beginner?




uncle goopy
Dec 1, 2005, 05:16 PM
Hello. I'm a fairly experienced programmer, but only in VisualBasic/RealBasic. I have a C program that I need to run. I've learned enough C that I can muddle through the code and more or less see what it's doing, but I haven't been able to get it to run in XCode, and I see from another thread here that I should probably be using a command-line compiler and the terminal. I googled GCC, but couldn't even figure out which file was the C compiler, or how to download it, let alone run it, let alone use it to run the program in question.

The author of the program informs me that he used a command-line compiler in Linux called g++. I saw a file named "g++" at the GCC site, so I'm guessing that might be a direction to pursue. But the idea of spending hours fumbling around in the dark is pretty depressing, so I'm hoping someone here will have the time to point me in the right direction. To recap: I have a C program, along with some headers and includes plus some stub files, everything I need (according to the author) to run the program. I know I'm supposed to gather these files together in the same directory as the compiler, and then type the magic words, which in linux/g++ are said to be

g++ -o programname programname.cpp

I'm guessing that -o is an instruction to the compiler and that the other two terms are the names of the source code and the eventual compiled program. If I could get GCC downloaded and running, then hopefully I could use the GCC manuals etc to eventually get everything working. As for XCode, I've read through the XCode manuals and done the beginner tutorials, but when I try to use XCode to run this program, it compiles successfully, but it appears to just compile the boilerplate that XCode installs when you choose "New Project," not the actual code from the program itself.

It's a pretty dull request, I know, but can anyone help get me up to speed on getting XCode or GCC (or any better alternative) up and running to the point where I can compile and run this program on Mac 10.3? Thanks in advance.



Mitthrawnuruodo
Dec 1, 2005, 05:37 PM
Exactly the same on a Mac.

No need for any downloading: gcc (and g++ for C++ programs) are already included with Mac OS X, and if you open Terminal.app (in /Applications/Utilities/) you can (more or less) follow the Linux instructions. Mac OS X is based on FreeBSD, a "cousin" of Linux, and you can access all command line tools using the Terminal... :)

robbieduncan
Dec 1, 2005, 05:37 PM
Install the Apple Developer tools (download the most recent version from developer.apple.com) (it sounds like you have done this).

This installs not only XCode but the command line tools including gcc (of which g++ is a part). This can be run from the Terminal (it's in Utilities).

The -o in the command above tells the compiler the name of the object file (the output) to produce from all the input files. So it will compile the cpp file into the named executable.

Jordan72
Dec 1, 2005, 07:39 PM
Check out my post here for first timer steps to compiling c code with Xcode and gcc in Terminal: http://forums.macrumors.com/showthread.php?t=163260

You mentioned c as the language you are using, but in your question you used g++.

g++ is a program that calls GCC with the default language set to C++, and automatically specifies linking against the C++ library.

If you are using c, then use the "gcc" program with the .c extenstion at the end of your c file name. If you do have a c++ file, I can't answer syntax specific question because I don't know that language.

doucy2
Dec 1, 2005, 07:44 PM
why is your thread posted twice??

uncle goopy
Dec 1, 2005, 08:06 PM
Thank you all. I now have gcc in a terminal window.

Jordan72 -- I tried both the procedures you recommended.

In GCC, when I enter

g++ myCode.cpp -o "myExecutable"

I get a "No such file or directory" message, followed by a "no input files" message, which I assume is just a function of the first message. The file myCode.cpp, along with all the headers, is in the same directory as the terminal. I've tried using explicit file paths for both myCode and myExecutable, but no difference. (BTW, you were right, of course, about my confusing c with c++)


In XCode, I get a successful compile, but when I run it, I get the "hello world" message first, and then underneath that it says "myExecutable has exited with status 0."

Can you tell offhand what I'm doing wrong?


*EDIT -- Have no idea why the thread got posted a second time. I'm sure I didn't double-post. Might have happened somehow during registration. Tried to delete the copy, but didn't find such an option. Sorry about that.

Mitthrawnuruodo
Dec 1, 2005, 08:17 PM
In stead of: g++ myCode.cpp -o "myExecutable"

Try: g++ -o myExecutable myCode.cpp

uncle goopy
Dec 1, 2005, 08:29 PM
Right, that's what I tried first, since that's the syntax the author used. Got the same result, though. Didn't want to complain about that until I'd worked through all the possibilities. Figured it's irresponsible to just try to get everyone else to do everything for you.

But then Jordan's post gave me the opportunity to beg some more, so I took it. :o

Mitthrawnuruodo
Dec 1, 2005, 08:33 PM
To start with the basics (excuse me if it's too basic ;)):

In Terminal, have you navigated yourself to the directory/folder where the cpp files are (cd path/to/folder), before you try to compile...?

uncle goopy
Dec 1, 2005, 08:42 PM
When I try to cd to a given directory, I get the same message (no such directory), although I had assumed that if you're in the same directory as the terminal, or if you use an explicit file path, you wouldn't have to worry about that. Looking at your example, though, I wonder if I'm using the wrong syntax. I used what I would use in Basic to navigate to a directory, which in this case is

system:users:myUserName:desktop:myDirectory:myCode.cpp

where the source code is in a folder called myDirectory on the desktop.

Just to check, I loaded the source code into an editor that displays the entire file path, and it was as above. Am I using incorrect syntax?

Mitthrawnuruodo
Dec 1, 2005, 08:53 PM
It's:

cd ~/Desktop/myDirectory

where ~/ is your home folder, i.e. /Users/yourshortname/

just:

cd

will take you to your home folder.

Another useful command is

ls

which lists the contents of your current folder, and

pwd

which shows you the Path to the Working Directory, i.e. the folder you're in at the moment.

uncle goopy
Dec 1, 2005, 09:00 PM
That did the trick, I'm in business now. Getting compile warnings, etc, which I can work through with the author. Sorry you had to teach me how to operate the terminal, that's above and beyond the call. I really appreciate the help from everyone, it saved me a lot of time and aggravation. Thanks again.

Mitthrawnuruodo
Dec 1, 2005, 09:05 PM
No problem, glad to help. :)

And with the basics in place you'll pick up the rest (http://www.ss64.com/osx/) in no time... ;)

uncle goopy
Dec 1, 2005, 09:20 PM
"The rest" was just what I needed next. Thanks once again.

pat_24
Dec 1, 2005, 10:10 PM
Why do you bother with the terminal ?

Are you doing graphical stuff ? If you're beginning in c/c++ and you are doing command line (console) programs, why do you bother with the terminal ?

uncle goopy
Dec 1, 2005, 11:53 PM
Historical accident, I guess. I got a c++ program from someone else that I want to run and maybe modify. I tried XCode, but encountered the problems described above. The program's author ran it from some kind of command-line interface, so when I couldn't make it work in XCode, I decided to try GCC. Be very happy to learn what I did wrong in XCode. Or is there a better place beside the terminal where I can say "g++ -o programName programName.cpp" or the equivalent?

Mitthrawnuruodo
Dec 2, 2005, 02:43 AM
Why do you bother with the terminal ?Most C/C++ beginner courses start with command line apps. It's very useful to learn the basics before starting with a GUI, which normally are of the more complicated parts of any application. Simple cin and cout from command or even text files are the way to go for learning the basic algorithms of programming in a new language... ;)