尝试在 Xcode 中编译时出现 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

您的main函数在两个位置定义。在您的小程序中和名为 main 的源文件中,它可能作为模板的一部分出现。我不确定您使用了哪个模板,但是在您的项目中查找名为 main 的文件并将其删除或注释掉其对 main 函数的实现。

相关内容

最新更新