E
E
Egorus2014-06-03 10:19:53
C++ / C#
Egorus, 2014-06-03 10:19:53

Why doesn't C++ code compile on Mac OS?

Good afternoon.
Started learning C++ programming on my MacBook Air, OS X 10.9.3, but it's just a pain. The MacBook does not want to compile anything, and due to my little experience I cannot understand what is the reason. Google refuses to help either.
Even if I take the code from the examples in the book or on the sites, it does not want to compile.
For example, I studied c++ compound assignment operators, copied from the example:

#include <iostream>
using namespace std;
int main() {
    int a = 3, b = 6, c = 10, d = 0xAAAA, e = 0x5555;
    
    a += b;      // a is 9
    b %= a;      // b is 6
    c >>= 1;      // c is 5
    d |= e;      // Bitwise--d is 0xFFFF
    
    cout  << "a = 3, b = 6, c = 10, d = 0xAAAA, e = 0x5555" << endl
    << "a += b yields " << a << endl
    << "b %= a yields " << b << endl
    << "c >>= 1 yields " << c << endl
    << "d |= e yields " << hex << d << endl;
}

I press compile, and Xcode immediately gives me an error -
0df423893f2040d7aebbf126c37465b0.png
Error text:
duplicate symbol _main in:
    /Users/Egor/Library/Developer/Xcode/DerivedData/Обучение-flagdgvimqnhjweohxodemobrqre/Build/Intermediates/Обучение.build/Debug/Обучение.build/Objects-normal/x86_64/main.o
    /Users/Egor/Library/Developer/Xcode/DerivedData/Обучение-flagdgvimqnhjweohxodemobrqre/Build/Intermediates/Обучение.build/Debug/Обучение.build/Objects-normal/x86_64/Sostavnie.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Don't throw your slippers at the kettle :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
EXL, 2014-06-03
@Egorus

You apparently created one project and put all the examples in it. The linker, seeing that the main() function is duplicated several times, gives you a natural error. A typical rookie mistake. You need to go to the concept: for each example - a separate project.
And I also advise you not to use Cyrillic characters in the paths to the directories with the code:
Better to replace with

/Users/Egor/Library/Developer/Xcode/DerivedData/Training-flagdgvimqnhjweohxodemobrqre/Build/Intermediates/Training.build/Debug/Training.build/Objects-normal/x86_64/main.o</build>

M
Mikhail Alekseev, 2014-06-03
@Fandorin

During the linking process, you have two definitions of the main function (they can be in different files), look at the linking and compilation settings. Something extra is being picked up.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question