C++ Programming with XCode 2

Discussion in 'Mac Programming' started by novicew, Jan 5, 2006.

  1. macrumors member

    novicew

    Joined:
    Jan 4, 2006
    Location:
    Hamburg
    #1
    I am pretty new to Apple and trying to write my first c++ program on XCode platform. However due to it's complexity I am kind of stuck not knowing from where to start with. I would really appreciate if someone can explain me how to do that.

    #include<iostream>
    using namespace std;
    int main()
    {
    cout <<"Hello World!\n";
    cin.get();
    }

    By the way, I had no problem running it on Terminal.

    Thank you in advance.
     
  2. macrumors 6502

    Joined:
    Apr 16, 2003
    Location:
    Boston, MA
    #2
    When you get started in XCode, you want to choose new project, then "C++ Tool" under "Command Line Utility". This will create a project that is of the type you're looking to create. main.cpp even has hello world already in it to start.
     
  3. thread starter macrumors member

    novicew

    Joined:
    Jan 4, 2006
    Location:
    Hamburg
    #3
    Hi mwpeters8182

    I managed to get that far too. My problem is to execute some of the *.cpp files I have already written on Windows. Is there a way to just open a *.cpp file, compile it and execute it in the same platform.

    Thank you for the help anyway
     
  4. macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #4
    Use gcc in terminal, like you said originally... that's the easiest way to compile simple CLI C++ programs. Xcode is designed for larger stuff.
     
  5. macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #5
    CLI - Command Line Interface - and ummm as long as you don't need a sepcific library like SDL (that one you can just use XCode to set it up and that) -the lib files- then just use the terminals CLI if you know it. Otherwise, maybe I can get Dev-C++ for Linux working or something on Mac. XCode is great though.
     
  6. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #6
    Depending on your project, you can just copy and paste the text into the main.cpp file since it's already known to be the main file. Otherwise, you should copy the files into the directory and add them to the project and then, build the project.
     
  7. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #7
    Since you have already done that, I suppose that can compile hello world application and run it, but there are some programs written on the PC that just don't compile.

    That is because there are some differences in some PC Commands: for example:
    Code:
    system("PAUSE");
    will not work, because it is a Windows specific subroutine and not C++ specific. Another command that won't work is
    Code:
    clrscr();
    This is also windows specific. There are other commands like these. OS X only supports commands that are specified in ANSI C++and not those windows specific sub-routines.
     
  8. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #8
    Many of those include C header files. I believe conio.h is one of those such header files, but it's been a long time since I did any DOS programming.

    It would be easy enough to do a pause programme but if u'r not already using a shell to execute the programme directly, I'm not sure what the output would be. Of course, C++ doesn't really offer native unbuffered input, so you would have to resort to C (fgetc using stdin would probably be a good choice) to halt execution.
     
  9. thread starter macrumors member

    novicew

    Joined:
    Jan 4, 2006
    Location:
    Hamburg
    #9
    Can you figure out what I am doing wrong here?.

    Thank you in advance
     
  10. macrumors 6502

    Joined:
    Apr 16, 2003
    Location:
    Boston, MA
    #10
    can you post the actual errors from the compiler?

    Also, in the class, I think you'll want to have width and height be private.
     
  11. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #11
    compiles fine on me. You fail to mention what kind of errors are those 6 arrors that you get...
     
  12. macrumors 68000

    Josh

    Joined:
    Mar 4, 2004
    Location:
    State College, PA
    #12
    The one thing I noticed when using XCode for C++ that was different from using the terminal (g++) or MS Visual Studio, is that XCode requires me to use:

    Code:
    std::cout << whatver is here
    Rather than just
    Code:
    cout << whatver is here
    .

    I'm sure there are plenty other differences I will notice as I go on, but just started my class last week and this is what I've noticed so far.
     
  13. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #13
    that's just the namespace definition.
    at the top of the program, use "use namespace std;" and it will not require you to do so. Perhaps you should read more about namespaces.
     
  14. macrumors 68000

    Josh

    Joined:
    Mar 4, 2004
    Location:
    State College, PA
    #14
    Thanks for the info.

    Perhaps I should :p
     
  15. macrumors 6502

    Joined:
    Jan 4, 2004
    #15
    Ok how do i compile main.cpp using the terminal? This is a c++ program.

    Oh, and how do i get it to run...
     
  16. macrumors newbie

    Joined:
    Jan 25, 2006
    #16
    type g++ <yourCppFilename.cpp> in the terminal. It should produce an application (executable file) if all is well. Just type the name of the application and it should run.
     
  17. macrumors 6502a

    MacCoaster

    Joined:
    Jul 15, 2002
    Location:
    Washington, DC / Rochester, NY / Lexington, NC
    #17
    CookieMook: close... If you don't provide the -o argument, it'll simply create the binary executable named "a.out".
    Code:
    g++ -o myprogram myprogram.cpp
    What's even better, learn makefiles. They'll take care of compilation easier than manually compiling.

    novicew: why do you have the last two lines uncommented. delete those or comment those out. like:
    Code:
    // rect area: 12
    // rectb area: 30
    and just some tip for you with your area() method, you might want to make it const like:
    Code:
    int area() const; // in the class scope
    and move the actual implementation as:
    Code:
    int CRectangle::area() const { return width * height; }
    this way you can guarantee the area() method will NOT mutate the width or height variables.

    Though I'd suggest you move the CRectangle:: stuff to a .cpp file and the class declaration in a header file. Xcode will automatically compile and link them all together.

    A final suggestion, use 'unsigned int' because neither width nor height can be negative. if you explictly try to add a negative integer the compiler will complain, however if you simply pass from input, it won't check, so you'll have to do the checking code if you use the user's input. So:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class CRectangle {
            unsigned int width, height;
        public:
            CRectangle( unsigned int w, unsigned h );
            unsigned int area() const;
    };
    
    CRectangle::CRectangle( unsigned int w, unsigned int h ) {
        width = w;
        height = h;
    }
    
    unsigned int CRectangle::area() const {
        return width * height;
    }
    
    int main( int argc, char* argv[] ) {
        CRectangle rectA( 3, 4 );
        CRectangle rectB( 5, 6 );
        
        cout << "rectA area: " << rectA.area() << endl;
        cout << "rectB area: " << rectB.area() << endl;
        
        return 0;
    }
    
    // rectA area: 12
    // rectB area: 30
    compiles great with g++ -o foo foo.cpp
     
  18. thread starter macrumors member

    novicew

    Joined:
    Jan 4, 2006
    Location:
    Hamburg
    #18
    Hi MacCoaster,

    Thank you for the valuable tips. Now it seems to be working fine.:)
     

Share This Page