尝试在Xcode中编译时出现Mach-O错误

时间:2012-06-05 16:02:18

标签: c++ xcode compiler-errors mach-o

我正在使用Xcode中的C ++程序,当我尝试构建和运行应用程序时,我不断收到Mach-O错误(加上“构建失败”错误消息)。以下是我正在使用的代码:

//
//  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;
    }
}

错误信息是:

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

1 个答案:

答案 0 :(得分:2)

您的main功能在两个地方定义。在您的小程序中以及名为main的源文件中,该文件可能是模板的一部分。我不确定您使用的是哪个模板,但在项目中查找名为main的文件并将其删除或注释掉main函数的实现。