Running Xcode C++ program without Xcode?

Discussion in 'Mac Programming' started by ebrithil15, Nov 23, 2009.

  1. ebrithil15 macrumors newbie

    Nov 23, 2009

    I just started to teach myself to program in the C++ language and i have just finished writing my first program with an actual function but I'm wondering how i run the program without Xcode.

    So my question is basically can i run this program on my Mac (or any other Macs) without having to run it with Xcode or any other comparable compiler every single time?

    I'm using Xcode 2.5 with Mac Os X v.10.4.11 (Tiger)
    and my programming code looks like this: (don't worry about not understanding the "couts" English isn't my native language so i didn't write it in English.)

    #include <iostream>
    #include <math.h>

    using namespace std;
    int main ()
    int a;
    int b, b1;
    int c;
    int result0, result1, result2, result3, result4;
    int result00, result01, result02, result03, result04;

    cout << "Þú ert kominn í Samband við Hakkavélina" << endl;
    cout << "Þessi hakkavél getu reiknað núllpunkta annars stigs formúla á forminu: ";
    cout << "Ax^2 + Bx + C" << endl << "..." <<endl;
    cout << "Skrifið inn stærð fyrir A, og ýtið svo á Enter" << endl;
    cin >> a;
    cout << "Skrifið inn stærð fyrir B, og ýtið svo á Enter" << endl;
    cin >> b;
    cout << "Skrifið inn stærð fyrir C, og ýtið svo á Enter" << endl;
    cin >> c;

    b1 = b * -1;
    result0 = 4 * a * c;
    result1 = b*b - result0;
    result2 = sqrt (result1);
    result3 = b1 + result2;
    result4 = result3 / (2 * a);
    result00 = 4 * a * c;
    result01 = b*b - result00;
    result02 = sqrt (result01);
    result03 = b1 - result02;
    result04 = result03 / (2 * a);
    cout << "Svar:" << endl;
    cout << result4 << " eða " << result04;

    return 0;

    I know it ain't a great program but it works. Don't know if you understand it's function from this code but what it does is calculating the value of "x" in formulas like: Ax^2 + Bx + C , when the final value is supposed to be 0. Don't really know how to say that in mathematical therms in English.

    So once again: Can i run this program like any other application on Macs and then how?

    Thank you for your time.
  2. balamw Moderator


    Staff Member

    Aug 16, 2005
    New England
    Unless I am misunderstanding the question, this is exactly the purpose of the compiler. It converts the C++ code you have here into an application that you can move from one Mac to the next without needing Xcode on the other Mac.

    The output from the compiler can be run on another Mac that does not have Xcode or gcc on it, but you will not be able to modify the code without Xcode or at least gcc and an editor. You will only be able to modify the inputs to the code.

    Without knowing much about the project name or otherwise how you have structured the project I can't help any more...

  3. rowsdower macrumors 6502

    Jun 2, 2009
    This is called solving a quadratic equation.
  4. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    There are a few ways to approach this. XCode is generating a binary that can be used on your mac outside of XCode (called an IDE, integrated development environment. I assume what you want is to run your program from the terminal, independent of the XCode environment). It can also be run on other macs running the version of OS X you targeted or above. In XCode there is a "build path" for each project. It's probably easiest to just look at the folder that was created for your project. I am not at a mac, but i believe there is a folder called "build" that is the default build path for your project. There you should find the executable produced by your most recent build. You should be able to run this from the terminal, outside of XCode.

    The other tact you could take, since this is such a simple program, is to use gcc from the terminal to compile your code, which will produce a binary you can run on its own. dukebound85 touched on this, albeit very briefly. To do this, save your code in a text file (for now you could use textedit) with the .cpp extension. Open a terminal, navigate to the folder you saved this file under using 'cd'. Once you are in this folder, run:
    g++ -o myfile myfile.cpp

    where myfile is the name you gave the file containing your code. You can then run:

    to run the binary produced.

    Alternately, you could run:
    make myfile

    instead of the gcc command, and make will figure out that you want to run the c++ compiler over myfile.cpp to produce a binary called myfile.


Share This Page