c++ i darwin

Discussion in 'Mac Programming' started by Quboid, Sep 19, 2007.

  1. Quboid macrumors 6502

    Joined:
    Oct 16, 2006
    Location:
    everywhere
    #1
    Hello all,

    I have been using windows for all my programming (for school) for the pasat year, but now i want to keep everything on mac (I would like to install and dual boot ubuntu instead of XP), but Xcode is a pretty heavy program developement environment, and it really slows down my machine!

    I hear poeple talking about compiling code in Darwin using GCC. Is there a way to compile and run code form the terminal? that would be so great! If there's a way to do this donot be afraid to elaborate the details.

    Qbo

    PS. ALeady got the man page for g++ and c++ but i can't make much out of them.
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    A Google search would have answered this one quickly, but to get you started:

    Create your files using whatever text editor you want. Put them in the same folder.

    Open Terminal and type "cd" and press the spacebar. Then drag the folder that has all your code into Terminal. The path of the folder should be added to your input in Terminal. Press Return.

    Then you can compile code like so:

    Code:
    g++ file1.cpp -o file1
    g++ is the command line tool to compile C++ sources. If you're only doing C code, use gcc instead.

    file1.cpp is the name of your filename.

    -o file1 says to name the output file file1

    Then you can run your program:

    Code:
    ./file1
    I see dead people... :confused: :p
     
  3. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #3
    Or, it could also be said...

    "they're baaaaaack"

    ...quoting another scary movie, and, pointing out the days of homework are upon us again.

    Todd
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    No worries. We have some new recruits to help handle all the newbies :)
     
  5. Quboid thread starter macrumors 6502

    Joined:
    Oct 16, 2006
    Location:
    everywhere
    #5
    Thanks alot guys. I have a slight problem though. When ever I run a program that i wrote in the terminal, after I run it the source code gets all wierd, it turns into somehting like a binary file (I amy not be maknig sence here). But thats what happens. here a sample of how my code looks after running it.

    The wierd code doesn't upload properly on the forum. Does anybody have an idea as to what is going on?
     
  6. toddburch macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #6
    If your source is fine when you start, and it's fine after you compile, and then after you run your compiled program, your original source become corrupt... is it safe to guess that your compiled program is processing your original source file?

    If not, what does your program do? Does it work with files at all?

    Todd
     
  7. Josh macrumors 68000

    Josh

    Joined:
    Mar 4, 2004
    Location:
    State College, PA
    #7
    When you compile your code, GCC is going to create one or more object files (file.o, etc).

    Are you opening up one of these .o files when you see the strange code?

    For simple command-line programs, I think using the terminal to create and compile code is the easiest/fastest, especially for smaller, less complex programs.

    I'd also suggest learning to create and use a Makefile, which will make compiling your code, especially with multiple files, much more streamlined.
     
  8. Quboid thread starter macrumors 6502

    Joined:
    Oct 16, 2006
    Location:
    everywhere
    #8
    I was mistakenly making my source code into the excutable object. Thanks guys. I figured that part out. Thanks guys. Compiling from the terminal rocks. May be coming back to this thread as the need arrises though.
     

Share This Page