JD2775
JD2775

Reputation: 3801

How do I diagnose Xcode message “Linker command failed with exit code 1”?

I am trying to teach myself some C, and I am stuck almost right off the bat in xCode. Currently I have a folder structure that looks like this:

enter image description here

main.c looks like this:

#include <stdio.h>

int main(void)
{
    int dogs;

    printf("How many dogs do you have?\n");
    scanf("%d", &dogs);
    printf("So you have %d dog(s)! \n", dogs);

    return 0;
}

concrete.c looks like this:

#include <stdio.h>
int main(void)
{
    printf("Concrete contains gravel and cement.\n");

    return 0;
}

No matter which one I run via Product->Run I get the same error:

enter image description here

Any ideas what is causing this? Coming from Python background only so these errors and xCode in general are totally new to me

EDIT- deleting the concrete.c file fixed the issue, but that doesn't seem reasonable to me. Is that because I had 2 files with int main(void) in it?

Upvotes: 0

Views: 77

Answers (1)

Eric Postpischil
Eric Postpischil

Reputation: 222264

When you click on Xcode’s display of “Linker command failed with exit code 1”, it should show you the portion of the build log where the linker is displaying messages and exiting with code 1.

These messages include:

duplicate symbol _main in:
[Some path on your system…]/Build/Intermediates.noindex/foo.build/Debug/foo.build/Objects-normal/x86_64/foo.o
[Some path on your system…]/Build/Intermediates.noindex/foo.build/Debug/foo.build/Objects-normal/x86_64/main.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

That message “duplicate symbol _main” tells you the problem: You have two definitions of main. main is the name for the unique routine that is called to start your program, and you should have only one definition for it.

Supplement

By default, in C, function names have external linkage, meaning the same name in different translation units (source files, when used normally) refer to the same thing. So there cannot be two functions with the same name—any name, not just main—because then you have two things with the same name. You can give a function internal linkage instead of external by declaring it with static, and then that name will not conflict with the same name in other translation units.

Nonetheless, defining a static version of main may result in compiler complaints, both that main should not be declared with static and that it ought to be declared to return int, if you declared it otherwise. These stem from text in the C standard, at C 2018 5.1.2.2.1 1:

The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters: … or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared): …

Although that text restricts the declaration of main, it seems to me that it could be interpreted as referring to the main with external linkage, not to any routine that happens to be declared main. Nonetheless, it is generally bad practice to reuse well-known special-purpose names for other purposes.

Upvotes: 1

Related Questions