苹果Mach O连接器错误

Apple Mach O Linker error?

本文关键字:错误 连接器 Mach 苹果      更新时间:2023-10-16
#include <iostream>
#include <cctype>
#include <string>
using namespace std;


int main()
{
    string text;
    string numbers;
    string characters;
    string spaces;
    getline(cin, text);
    int i;
    for (i=0; i<text.length(); i++)
    {
        if (isalpha(text[i]))
        {
            characters += text[i];
        }
        else if (isdigit(text[i]))
        {
            numbers +=text[i];
        }
        else
        {
            spaces +=text[i];
        }
    }
    cout<<"Numbers in the string: "<<numbers<<endl
    <<"Alphabets in the string: "<<characters<<endl
    <<"Spaces in the string: "<<spaces.length()<<endl<<endl;

    string searchstr;
    string replacestr;
    double n = text.length();
    cout<<"Enter a word to search: "<<endl;
    getline(cin, searchstr);
    cout<<"Enter a replacement word: "<<endl;
    getline (cin, replacestr);
    double position = text.find(searchstr, 0);
    text.replace (position, n, replacestr);
    cout<<"Replaced string is: "<<endl
    <<text<<endl<<endl;

    return 0;
}

我不知道这是怎么回事。这与正在使用的库有关吗?

Error:
Ld "/Users/andy/Library/Developer/Xcode/DerivedData/Project_4-fgfiiphpwijunsfidkthbsqbxgvb/Build/Products/Debug/Project 4" normal x86_64
    cd "/Users/andy/Desktop/EECS138/Project 4"
    setenv MACOSX_DEPLOYMENT_TARGET 10.8
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -L/Users/andy/Library/Developer/Xcode/DerivedData/Project_4-fgfiiphpwijunsfidkthbsqbxgvb/Build/Products/Debug -F/Users/andy/Library/Developer/Xcode/DerivedData/Project_4-fgfiiphpwijunsfidkthbsqbxgvb/Build/Products/Debug -filelist "/Users/andy/Library/Developer/Xcode/DerivedData/Project_4-fgfiiphpwijunsfidkthbsqbxgvb/Build/Intermediates/Project 4.build/Debug/Project 4.build/Objects-normal/x86_64/Project 4.LinkFileList" -mmacosx-version-min=10.8 -stdlib=libc++ -o "/Users/andy/Library/Developer/Xcode/DerivedData/Project_4-fgfiiphpwijunsfidkthbsqbxgvb/Build/Products/Debug/Project 4"
duplicate symbol _main in:
    /Users/andy/Library/Developer/Xcode/DerivedData/Project_4-fgfiiphpwijunsfidkthbsqbxgvb/Build/Intermediates/Project 4.build/Debug/Project 4.build/Objects-normal/x86_64/main.o
    /Users/andy/Library/Developer/Xcode/DerivedData/Project_4-fgfiiphpwijunsfidkthbsqbxgvb/Build/Intermediates/Project 4.build/Debug/Project 4.build/Objects-normal/x86_64/File.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation

不知道为什么Xcode不能构建/运行程序,它在CodeBlocks中工作得很好。有什么帮助,为什么它给我这个错误?

从您的错误中,似乎您有一个文件main和另一个文件File都定义了main()函数。

main()是程序的入口点。不能为程序的开始定义两个不同的函数