Mojo Mojo - 4 months ago 20
C++ Question

"Linker command failed with exit code 1"

I'm very new to programming and I'm going over the basics of vectors. I'm currently getting the error when i run this program "linker command failed with exit code 1 (use -v see invocation)"

Now i have seen posts on this but the Xcode I'm running is apparently much different than the rest of yours or I'm doing something wrong maybe? I was wondering if someone could dumb down the error for me in layman's terms.
Also I'm using 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;
}


Error message:

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)

Answer

If seems you have a single project with two main source files, where both source files contain main functions.

That will not work, and that's what the linker tells you ("duplicate symbol _main ..."). You have to split up the projects, so you have separate projects for each main source file.