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

标签 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

最佳答案

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

关于c++ - 尝试在 Xcode 中编译时出现 Mach-O 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10900742/

相关文章:

ios - 如何锁定 UITextField 输入以在 SWIFT 上只接受从 A 到 Z 的一个字母

xcode - 如何检查 AVPlayer 是否有视频或只有音频?

ios - 使用 iOS 实现单元测试

c++ - 将 cmake 编译器从 clang 更改为 g++ for mac

java - 为什么PowerShell会显示 'Cannot find symbol'错误,但是VS Code Java Process Console可以很好地运行它?

C 中指针变量的冲突类型(错误)

c++ - NULL 指针的取消引用是否也等于 NULL?

c++ - 派生类在基类中被删除时是否会有隐式复制构造函数或赋值运算符?

Python (rospy) 到 C++ (roscpp) struct.unpack

c++ - 如何将 .c 和 .h 文件存储在包含路径中,与当前项目的源代码分开