0

Here are my 2 source files:

main.c:

#include <stdio.h>
#include "part2.c"

extern int var1;
extern int array1[];

int main()
{
    var1 = 4;
    array1[0] = 2;
    array1[1] = 4;
    array1[2] = 5;
    array1[3] = 7;

    display();

    printf("---------------");

    printf("Var1: %d", var1);
    printf("array elements:");

    int x;
    for(x = 0;x < 4;++x)
        printf("%d: %d", x, array1[x]);

    return 0;
}

part2.c

#include <stdio.h>

int var1;
int array1[4];

void display(void);

void display(void)
{
    printf("Var1: %d", var1);
    printf("array elements:");

    int x;
    for(x = 0;x < 4;++x)
        printf("%d: %d", x, array1[x]);
}

When i try to compile the program this is what i get:

Ld /Users/John/Library/Developer/Xcode/DerivedData/Test-blxrdmnozbbrbwhcekmouessaprf/Build/Products/Debug/Test normal x86_64 cd /Users/John/Xcode/Test setenv MACOSX_DEPLOYMENT_TARGET 10.7 /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.7.sdk -L/Users/John/Library/Developer/Xcode/DerivedData/Test-blxrdmnozbbrbwhcekmouessaprf/Build/Products/Debug -F/Users/John/Library/Developer/Xcode/DerivedData/Test-blxrdmnozbbrbwhcekmouessaprf/Build/Products/Debug -filelist /Users/John/Library/Developer/Xcode/DerivedData/Test-blxrdmnozbbrbwhcekmouessaprf/Build/Intermediates/Test.build/Debug/Test.build/Objects-normal/x86_64/Test.LinkFileList -mmacosx-version-min=10.7 -o /Users/John/Library/Developer/Xcode/DerivedData/Test-blxrdmnozbbrbwhcekmouessaprf/Build/Products/Debug/Test

ld: duplicate symbol _display in /Users/John/Library/Developer/Xcode/DerivedData/Test-blxrdmnozbbrbwhcekmouessaprf/Build/Intermediates/Test.build/Debug/Test.build/Objects-normal/x86_64/part2.o and /Users/John/Library/Developer/Xcode/DerivedData/Test-blxrdmnozbbrbwhcekmouessaprf/Build/Intermediates/Test.build/Debug/Test.build/Objects-normal/x86_64/main.o for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

I am using Xcode and both files are inside of a C project called Test

What is causing the error and how do i fix it?

2
  • 4
    First, learn how to use header files. Commented Jun 28, 2012 at 21:12
  • I've bolded the only part of the error message that is probably relevant. Commented Jun 28, 2012 at 21:13

1 Answer 1

5

Remove "#include "part2.c"" and change it to "#include "part2.h"

And move your function declaration of:

void display(void);

from the top of your "part.c" file into a file named "part2.h", which should be included in (or put at the top of) both .c files.

Sign up to request clarification or add additional context in comments.

2 Comments

And make sure to pass into both .c files to the compiler.
In general, you really shouldn't be including .c files -- just #include foo.h and then link with the .c.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.