Question

I'm working on a C++ program in Xcode, and I keep getting a Mach-O error when I try to build and run the app (plus a "build failed" error message). Below is the code I'm using:

//
//  QuadSolver.cpp
//  Calculator
//
//
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
    //enter the three variables
    double a;
    double b;
    double c;
    cout << "Enter A";
    cin >> a;

    cout << "Enter B";
    cin >> b;

    cout << "Enter C";
    cin >> c;

    //calculating a discriminant
    double d;
    d = b * b - 4 * a * c;
    double x1, x2;

    if (d == 0)
    {
        x1 = x2 = -b / (2 * a);
        cout << "There's only one solution: ";
        cout << x1;
        system("PAUSE");
        return 0;
    }

    else if (d < 0)
    {
        cout << "There are no possible solutions, as the discriminant is smaller than zero";
        system("PAUSE");
        return 0;
    }
    else if (d > 0)
    {
        x1 = (-b - sqrt(d)) / (2 * a);
        x2 = (-b + sqrt(d)) / (2 * a);
        cout << "There are two solutions:";
        cout << "x1=";
        cout << x1;
        cout << "x2=";
        cout << x2;
    }
}

And the error message is something along the lines of:

ld: duplicate symbol _main in /Users/myusername/Library/Developer/Xcode/DerivedData/Calculator-cwpaasypxtqkpvfsbfjekrgrgvbq/Build/Intermediates/Calculator.build/Debug/Calculator.build/Objects-normal/x86_64/QuadSolver.o and /Users/myusername/Library/Developer/Xcode/DerivedData/Calculator-cwpaasypxtqkpvfsbfjekrgrgvbq/Build/Intermediates/Calculator.build/Debug/Calculator.build/Objects-normal/x86_64/main.o for architecture x86_64
Was it helpful?

Solution

Your main function is defined in two places. In your little program up there and in a source file named main, which probably came as part of the template. I'm not sure which template you used, but look in your project for a file named main and remove it or comment out its implementation of the main function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top