C++ Compiler for Mac? HELP with xcode?

Discussion in 'Mac Programming' started by oxshannon, Jan 5, 2012.

  1. macrumors regular

    Joined:
    Jan 5, 2012
    #1
    So I'm totally new to the mac community (got my macbook pro for xmas) and now the semester has started again and Im registered in a first year math course -- introduction to computing with c++. now you guys can probably see where this is going.

    basically, I hadone class and my professor gave us a linkto download a c++ compiler for windows, and I emailed him asking about one for Mac, and he told me I would have to search the internet for one as he has no experience with mac. so i did some searching and determined i should download xcode? NOW im still downloading it, but im fairly certain i wont be able to work it. im not a computer programming person, im only taking this course to satisfy my math credit requirements, but i do think it is something i may enjoy once i learn it. im really confused, and my prof wrote the textbook we use himself so i dont really find it to be much help. Honestly, any help would be greatly appreciated.

    Thanks.
     
  2. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #2
    For an introductory course, you may want to skip the IDE, i.e. Xcode and use the compiler directly.

    You can skip Xcode and install the compilers from here: https://github.com/kennethreitz/osx-gcc-installer

    Can you point us to the book? This will let us know a bit more about the style.

    B
     
  3. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #3
    Having the title of the text book in question will affect the help provided here.
     
  4. thread starter macrumors regular

    Joined:
    Jan 5, 2012
    #4
    Technically I dont think it would be considered a textbook, its binded by my university and says "Math 1116 - Introduction to Computing with C++ Lecture Notes". So you're saying I don't need xcode? I'm so confused, I swear I'm not dumb but this topic is so new to me and quite confusing :(

    also, would it just be "OS X 10.7 Lion: GCC-10.7.pkg" that I download from that link?
     
  5. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #5
    Presuming you are running Lion ;)

    It really depends on how the examples are described and set up. If it is really fine tuned for Visual C++ you may find doing it on the Mac more difficult.

    B
     
  6. oxshannon, Jan 5, 2012
    Last edited by a moderator: Jan 5, 2012

    thread starter macrumors regular

    Joined:
    Jan 5, 2012
    #6
    Yes I am! I actually got something correct tonight. Lol my "homework" per se was to run a code given to us by my prof (shown below) in the compiler to make sure we can do it. so once i get this downloaded (unfortunately will take forever since I am on residence wireless) -- what should my next step be? if anyone is willing to explain that is :)

    Code:
    #include <iostream>
    
    int main( )
    {
       std::cout << "Hello world!\n";
       return 0;
    }
    


    ----------

    I suppose I could always just use the windows PCs at the library, but that is such a hassle if I could find out how to do it on my mac instead!
     
  7. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #7
    Xcode is a development environment. It includes a Text Editor (for writing source code), a C/C++ and ObjectiveC/C++ compiler, a linker and more.

    Go ahead and download Xcode. You need the all the compiler and linker no matter what. But you maybe able to get by without learning the IDE, Integrated Development Environment that Xcode also provides.

    CHeck back when you start programming you'll get plenty of help going from there.
     
  8. macrumors 603

    Joined:
    Aug 9, 2009
    #8
    As would knowing which C++ compiler the prof told the Windows users to download. It's unclear to me whether it's a command-line C++ compiler, a simple GUI/IDE, or a full-featured GUI/IDE.

    Context is important, and knowing exactly what the prof expects students to use is an important part of the context.
     
  9. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #9
    Great!

    Any chance you could give us some context for that code. This is a fairly typical first step, but it would help to see how your prof surrounds it.

    Please wrap any code snippets in CODE tags using the hash mark # button in the edit window.

    B
     
  10. thread starter macrumors regular

    Joined:
    Jan 5, 2012
    #10
    The file name was VC2005.exe, so im assuming microsoft visual c++? that's what came up when i googled it.
    im starting to think im over my head in this.

    ----------

    Um it's just the first example we were given, all it says concerning it is that the output should be Hello world! Not much else is given.
     
  11. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #11
    Don't Panic. At this level you'll be fine.

    It would have been much worse if the "Hello World!" example used a Windows specific dialog box mechanism. (Don't read this).

    B
     
  12. thread starter macrumors regular

    Joined:
    Jan 5, 2012
    #12
    Thanks. I just wish I understood the concept a bit more, like I said my textbook isnt quite a textbook, it seems its just lecture notes so it doesnt really give any context.
     
  13. macrumors Pentium

    KnightWRX

    Joined:
    Jan 28, 2009
    Location:
    Quebec, Canada
    #13
    Just copy/paste it in your favorite editor and save it with a .cpp extension. Then, it's just a matter of compiling/linking it with the GCC you installed. For C++, you'll want to use the g++ executable as so for a simple compile/link operation. This is all done under Terminal (Applications -> Utilities -> Terminal) :

    Code:
    $ pwd
    /Users/username
    $ cd dir_where_file_is_stored
    $ g++ -Wall -o test test.cpp
    $ 
    If the command just returns to the prompt like my example did, then it's because it compiled fine. You should have an executable named test (the -o parameter specifies the executable name to create). You can run it as so :

    Code:
    $ ./test
    Hello world!
    $
    You might want to learn a thing or two about Makefiles and GNU make for more complicated projects. If your code is strewn about multiple files, you have to first compile each into object code one by one (one file at a time) and then link them together manually. Makefiles and GNU make automate this process.

    EDIT : Or just get Xcode which should hide a lot of this complexity.
     
  14. Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #14
    Even for simple projects!

    I've started using
    Code:
    make test
    instead of your g++ command above from an earlier version of this thread. :p


    ----------

    But adds more things to learn (a different kind of complexity) if you aren't already familiar with an IDE.

    B
     
  15. thread starter macrumors regular

    Joined:
    Jan 5, 2012
    #15
    I'm not even sure I'll know how to run this once it's installed. I'm really lost lol. Like I said I'm completely new to mac and between xmas and now have not had a lot of time to figure things out.

    also is terminal equivalent to command prompt? im assuming
     
  16. VPrime, Jan 5, 2012
    Last edited: Jan 5, 2012

    macrumors 68000

    VPrime

    Joined:
    Dec 19, 2008
    Location:
    London Ontario
    #16
    To those saying don't use xcode do it all through terminal.. I disagree.
    Sure that makes sense as a step for someone trying to get into computer science, or programming as a future career.
    But if all you're trying to do is pass a class then I wouldn't bother with the terminal.
    In xcode it is as simple as starting a new project (first prompt when it starts up).
    Tell it you want to make a mac OSX command line application (in this case)
    Then paste the code from your book into the text window. And click the button with a "play" icon on it (build and run).
    Your results will be at the bottom in the console window.

    Will take you all of 5 minutes to get going. This way you won't fall behind with your class.
    Here is a video for Xcode 3.. The interface is slightly different, but you should be able to follow along.
    http://www.youtube.com/watch?v=ZU_MT9PwKkk

    edit:
    Xcode 4 video..
    http://www.youtube.com/watch?v=SGwS9XNal4I
     
  17. macrumors 603

    mobilehaathi

    Joined:
    Aug 19, 2008
    Location:
    The Anthropocene
    #17
    I find that an IDE only complicates things and hides important steps that a newbie really should know. Sure it streamlines a lot for the intermediate and advanced user, but I still think a beginner should start on the command line. Just MHO...
     
  18. thread starter macrumors regular

    Joined:
    Jan 5, 2012
    #18
    okay ummmm so ive downloaded that file from the link I was given up above.. but i installed it and now I have no idea where it went or whatever. how would i uninstall this? my god im in over my head
     
  19. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #19
    Breath. Breath. Breath.

    See you're still alive and nothing bad has happened so relax.
     
  20. thread starter macrumors regular

    Joined:
    Jan 5, 2012
    #20
    i just have no idea what to do now! how would i go about uninstalling this when i am done with it? and more importantly how do i USE it? :confused::confused::confused::confused:
     
  21. lloyddean, Jan 5, 2012
    Last edited: Jan 5, 2012

    macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #21
    Everyone seems to have dropped out for dinner so I guest I'll give it a go.

    Assuming you run the Xcode installer ...

    Locate and run the "TextEdit.app" in the "Applications" folder.

    Paste your code from above into the blank window and save it to whatever folder you plan to keep your Math programming projects with a name that ends with .cpp. Something along the lines of "MathAssignment1.cpp"

    Now run (launch) "Terminal.app" located in Applications/Utilities.

    The Terminal is analogous to the Windows command-line. You'll now need to tell the command-line where your "MathAssignment1.cpp" is with the 'cd' (change directory) command.

    If you saved the "MathAssignment1.cpp" to your "Desktop" that would be -

    cd ~/Desktop

    Now type -

    clang++ -Wall -o MathAssignment1 MathAssignment1.cpp

    Assuming no errors you can run the executable with -

    ./MathAssignment1

    Have fun!
     
  22. thread starter macrumors regular

    Joined:
    Jan 5, 2012
    #22
    bash: cd~/Desktop: No such file or directory

    I did save it to my desktop, and thats what it said?
     
  23. macrumors 68040

    Joined:
    Feb 2, 2008
    #23
    If you are using TextEdit don't forget to set it to plain text.
     
  24. thread starter macrumors regular

    Joined:
    Jan 5, 2012
    #24
    yep i did

    ----------

    so i think i got it? I hit enter after i typed that last line in and a new file showed up on my desktop. is that all?
     
  25. macrumors 68040

    Joined:
    Feb 2, 2008
    #25
    You need to have a space in there, cd (change directory) is the command ~/desktop is the path.

    edit
    Sounds like you managed to get it working, never mind.
     

Share This Page