链接器命令失败:重复符号_main

时间:2016-08-07 16:49:18

标签: c++ xcode

我对编程非常陌生,而且我正在研究向量的基础知识。当我运行此程序时,我当前收到错误"链接器命令失败,退出代码为1(使用-v参见调用)"

现在我已经看过这个帖子但是我跑的Xcode显然与你的其他人有很大的不同,或者我做错了什么?我想知道是否有人可以通过外行人的条款为我愚弄错误。 我也使用Xcode 7.3

#include <stdio.h>
#include <vector>  //vector support
#include <iostream>
using namespace std;

int main()
{
vector <int> vec(3,100);

cout << "Vector size: " << vec.size() << endl;
cout << "Is empty?: " << vec.empty() << endl;
cout << "First element: " << vec.at(0) << endl;

vec.pop_back();         // remove final element
cout << "Vector size: " << vec.size() << endl;
cout << "Final element: " << vec.back() << endl;

vec.clear();            // remove all elements
cout << "Vector size: " << vec.size() << endl;

vec.push_back(200);   //add an element
cout << "Vector size: " << vec.size() << endl;
cout << "First element: " << vec.front() << endl;
return 0;
}

错误讯息:

Ld /Users/Mojo/Library/Developer/Xcode/DerivedData/Project1-eglodkxixcqsglauiwgsasozdrpv/Build/Products/Debug/Project1 normal x86_64
    cd /Users/Mojo/Desktop/Project1
    export MACOSX_DEPLOYMENT_TARGET=10.11
    /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.11.sdk -L/Users/Mojo/Library/Developer/Xcode/DerivedData/Project1-eglodkxixcqsglauiwgsasozdrpv/Build/Products/Debug -F/Users/Mojo/Library/Developer/Xcode/DerivedData/Project1-eglodkxixcqsglauiwgsasozdrpv/Build/Products/Debug -filelist /Users/Mojo/Library/Developer/Xcode/DerivedData/Project1-eglodkxixcqsglauiwgsasozdrpv/Build/Intermediates/Project1.build/Debug/Project1.build/Objects-normal/x86_64/Project1.LinkFileList -mmacosx-version-min=10.11 -Xlinker -no_deduplicate -stdlib=libc++ -Xlinker -dependency_info -Xlinker /Users/Mojo/Library/Developer/Xcode/DerivedData/Project1-eglodkxixcqsglauiwgsasozdrpv/Build/Intermediates/Project1.build/Debug/Project1.build/Objects-normal/x86_64/Project1_dependency_info.dat -o /Users/Mojo/Library/Developer/Xcode/DerivedData/Project1-eglodkxixcqsglauiwgsasozdrpv/Build/Products/Debug/Project1

duplicate symbol _main in:
    /Users/Mojo/Library/Developer/Xcode/DerivedData/Project1-eglodkxixcqsglauiwgsasozdrpv/Build/Intermediates/Project1.build/Debug/Project1.build/Objects-normal/x86_64/Hello.o
    /Users/Mojo/Library/Developer/Xcode/DerivedData/Project1-eglodkxixcqsglauiwgsasozdrpv/Build/Intermediates/Project1.build/Debug/Project1.build/Objects-normal/x86_64/FirstVector.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

1 个答案:

答案 0 :(得分:1)

如果你有一个包含两个主要源文件的项目,那么两个源文件都包含main个函数。

这不起作用,这就是链接器告诉你的(“重复符号_main ...”)。您必须拆分项目,因此每个主要源文件都有单独的项目。