Help For A Beginner?

Discussion in 'Mac Programming' started by uncle goopy, Dec 1, 2005.

  1. macrumors newbie

    Joined:
    Dec 1, 2005
    #1
    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.
     
  2. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #2
    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... :)
     
  3. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #3
    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.
     
  4. macrumors member

    Joined:
    Nov 23, 2005
    #4
    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++.

    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.
     
  5. macrumors 65816

    doucy2

    Joined:
    Jul 7, 2005
  6. thread starter macrumors newbie

    Joined:
    Dec 1, 2005
    #6
    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.
     
  7. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #7
    In stead of: g++ myCode.cpp -o "myExecutable"

    Try: g++ -o myExecutable myCode.cpp
     
  8. thread starter macrumors newbie

    Joined:
    Dec 1, 2005
    #8
    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. :eek:
     
  9. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #9
    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...?
     
  10. thread starter macrumors newbie

    Joined:
    Dec 1, 2005
    #10
    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?
     
  11. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #11
    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.
     
  12. thread starter macrumors newbie

    Joined:
    Dec 1, 2005
    #12
    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.
     
  13. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #13
    No problem, glad to help. :)

    And with the basics in place you'll pick up the rest in no time... ;)
     
  14. thread starter macrumors newbie

    Joined:
    Dec 1, 2005
    #14
    "The rest" was just what I needed next. Thanks once again.
     
  15. macrumors newbie

    Joined:
    May 20, 2004
    Location:
    Québbec
    #15
    why dont you use xcode and just compile and build ?

    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 ?
     
  16. thread starter macrumors newbie

    Joined:
    Dec 1, 2005
    #16
    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?
     
  17. Moderator emeritus

    Mitthrawnuruodo

    Joined:
    Mar 10, 2004
    Location:
    Bergen, Norway
    #17
    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... ;)
     

Share This Page